Drawing in Java part 2 Dr Usman Saeed


















- Slides: 18

Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University Georgia Institute of Technology

Learning Goals • Understand at a conceptual and practical level – How to draw more simple shapes with java. awt. Graphics • Arcs • Rectangles – Bitmapped pictures versus vector graphics – How to use a java. awt. Graphics 2 D class • • • How to convert the Graphics class to it How to set the color How to set the paint How to set the stroke (paint brush) How to create the object to be painted How to paint the object Georgia Institute of Technology

Drawing Arcs • Arcs – Outlined Arc • g. draw. Arc(top. Left. X, top. Left. Y, width, height, start. Angle, arc. Angle); – Filled Arc • g. fill. Arc((top. Left. X, top. Left. Y, width, height, start. Angle, arc. Angle); Georgia Institute of Technology

Drawing Rectangles • Rectangle – Outlined Rectangle • g. draw. Rect(top. Left. X, top. Left. Y, width, height); – Filled Rectangle • g. fill. Rect(top. Left. X, top. Left. Y, width, height); – Outlined Rounded Rectangle • g. draw. Round. Rect(top. Left. X, top. Left. Y, width, height, arc. Width, ar c. Height); – Filled Rounded Rectangle • g. fill. Round. Rect(top. Left. X, top. Left. Y, width, height, arc. Width, arc. H eight); Georgia Institute of Technology

Drawing on a Blank Picture • You can make pictures from the “blank” files – They will have all white pixels – 640 x 480. jpg – 7 in. X 95 in. jpg • You can also create a “blank” picture with a width and height – They will also have all white pixels – Picture blank. Picture = new Picture(width, height); Georgia Institute of Technology

Draw a Picture Exercise • Create a method that will draw a simple picture – – Use at least one rectangle Use at least one polygon Use at least one oval Use at least one arc Georgia Institute of Technology

Bitmapped Versus Vector Graphics • We have been doing bitmapped graphics – Specifying the color of each pixel in the picture • We just wrote a method that drew a simple picture – Which is smaller the program or the picture? • Some applications use vector graphics which are programs that produce the picture – Used in Postscript, Flash, and Auto. CAD – Advantages: smaller, easy to change, can be scaled Georgia Institute of Technology

Precision Drawings • How would you draw a stack of filled rectangles starting from the lightest one at the bottom right and the darkest one at the top left. – With 10 pixels between each – Not easy with drawing packages Georgia Institute of Technology

Draw Filled Rectangles Method public void draw. Filled. Rectangles() { Graphics g = this. get. Graphics(); Color color = null; // loop 25 times for (int i = 25; i > 0; i--) { color = new Color(i * 10, i * 5, i); g. set. Color(color); g. fill. Rect(0, 0, i*10); } } Georgia Institute of Technology

Java 2 D Graphics – java. awt • Newer drawing classes – More object-oriented – Instead of draw. Oval() or fill. Oval() you create a Ellipse 2 D object and ask a 2 d graphics object to draw or fill it – Geometric shapes are in the java. awt. geom package • Advanced Drawing – Support for different types of brushes • Line thickness, dashed lines, etc – – Supports cubic curves and general paths Drawing of gradients and textures Move, rotate, scale and shear text and graphics Create composite images Georgia Institute of Technology

How To Use Java 2 D • Cast the Graphics class to Graphics 2 D – Graphics 2 D g 2 = (Graphics 2 D) g. Obj; • Set up the stroke if desired (type of pen) – g 2. set. Stroke(new Basic. Stroke(width. As. Float)); • Set up a Color, Gradient. Paint, or Texture. Paint – g 2. set. Paint(Color. blue); – g 2. set. Paint(blue. To. Purple. Gradient); – g 2. set. Paint(texture); • Create a geometric shape – Line 2 D line 2 D = new Line 2 D. Double(0. 0, 100. 0); • Draw the outline of a geometric shape – g 2. draw(line 2 d); • Fill a geometric shape – g 2. fill(rectangle 2 d); Georgia Institute of Technology

Graphics 2 D inherits from Graphics • Inherits basic drawing ability from Graphics • Adds more advanced drawing ability Graphics 2 D Georgia Institute of Technology

Drawing Lines Exercise • Create a new method (draw. Wide. X) for adding two wide crossed lines to a picture – Using a passed color – Using a passed line width • Set up the stroke to make the lines thicker – g 2. set. Stroke(new Basic. Stroke(width)); – Draw the lines • You can use red. Motorcycle. jpg to test. Georgia Institute of Technology

public void draw. Wide. X(Color color, float width) { // get the Graphics 2 D object Graphics graphics = this. get. Graphics(); Graphics 2 D g 2 = (Graphics 2 D) graphics; // set the color and brush width g 2. set. Paint(color); g 2. set. Stroke(new Basic. Stroke(width)); // get the max x and y values int max. X = get. Width() - 1; int max. Y = get. Height() - 1; // draw the lines g 2. draw(new Line 2 D. Double(0, 0, max. X, max. Y)); g 2. draw(new Line 2 D. Double(0, max. Y, max. X, 0)); }

Using a Graphics Object to Copy an Image public void copy(Picture source, int x, int y) { // get the graphics object Graphics g = this. get. Graphics(); // copy the image g. draw. Image(source. get. Image(), x, y, null); } Georgia Institute of Technology

Using Graphics 2 D to Copy an Image public void copy 2 D(Picture source, int x, int y) { // get the graphics object Graphics g = this. get. Graphics(); Graphics g 2 = (Graphics 2 D) g; // copy the image g 2. draw. Image(source. get. Image(), x, y, null); } Georgia Institute of Technology

Testing the Copy Method • Picture p 1 = new Picture(File. Chooser. get. Media. Path("beach. jpg")); • Picture p 2 = new Picture(File. Chooser. get. Media. Path("turtle. jpg")); • p 1. copy 2 D(p 2, 194, 304); • p 1. show(); Georgia Institute of Technology

Summary • You can use the original Graphics class for simple 2 d drawing – Lines, rectangles, ovals, polygons, strings, images • You can use the Graphics 2 D class for more advanced drawing – Width of paint brush, paint geometric objects • Graphics 2 D inherits from Graphics – Means it can do the same methods – But also adds others Georgia Institute of Technology