CSC 1401 Drawing in Java 2 Reminder from

  • Slides: 15
Download presentation
CSC 1401 Drawing in Java - 2

CSC 1401 Drawing in Java - 2

Reminder from last class How do you save your modified picture? String filename =

Reminder from last class How do you save your modified picture? String filename = …; Picture steve. Picture = new Picture(filename); … // modify the picture steve. Picture. write(“modified. Picture. jpg”); Please note that by not specifying a directory, java saves the picture in the same directory as your java program

Learning Goals Understand at a conceptual and practical level How to draw more simple

Learning Goals Understand at a conceptual and practical level How to draw more simple shapes with java. awt. Graphics Arcs Rectangles 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 paint the object

Drawing Arcs Outlined Arc g. draw. Arc(top. Left. X, top. Left. Y, width, height,

Drawing 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);

Drawing Rectangles Rectangle Outlined Rectangle g. draw. Rect(top. Left. X, top. Left. Y, width,

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, arc. Height); Filled Rounded Rectangle g. fill. Round. Rect(top. Left. X, top. Left. Y, width, height, arc. Width, arc. Height);

Drawing on a Blank Picture You can make pictures from the “blank” files They

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);

Java 2 D Graphics – java. awt Newer drawing classes More object-oriented Instead of

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

How To Use Java 2 D Cast the Graphics class to Graphics 2 D

How To Use Java 2 D Cast the Graphics class to 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 g 2. set. Paint(Color. blue); 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);

Graphics 2 D inherits from Graphics Inherits basic drawing ability from Graphics Adds more

Graphics 2 D inherits from Graphics Inherits basic drawing ability from Graphics Adds more advanced drawing ability Graphics 2 D

Drawing Lines Exercise Create a new method (draw. Wide. X) for adding two wide

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

Using a Graphics Object to Copy an Image public void copy (Picture source, int

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); } Note: This method goes into our Picture class

Using Graphics 2 D to Copy an Image public void copy 2 D(Picture source,

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); }

Testing the Copy Method Picture p 1 = new Picture(File. Chooser. get. Media. Path("beach.

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(p 2, 194, 304); p 1. show();

Summary You can use the original Graphics class for simple 2 d drawing Lines,

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

Assignment Read Media Computation Chapter 7, Sections 3. 1 -3. 2

Assignment Read Media Computation Chapter 7, Sections 3. 1 -3. 2