Download Lab Exercise: Drawing Ovals and Circles using Java AWT and more Lab Reports Computer Science in PDF only on Docsity! CS121/IS232 Laboratory 7 Using methods from the Graphics class to draw figures. Applets can also be used to paint figures on the screen. The drawing methods are in the Graphics class, a part of the AWT (abstract windowing toolkit). The two methods that we will use are the ones that draw ovals and fill ovals. The first draws only the perimeter of the oval, while the second fills the oval in with the current pen color. Pen color is changed by the setColor method, also in the Graphics class. Ovals are defined by the rectangle that fits them the most closely. This rectangle is given by the coordinates of its upper left hand corner and the width and height of its sides. Circles are ovals with equal widths and heights. Applet axes have the origin at the upper left hand corner of the screen with the x-axis pointed to the right and the y-axis pointing down. Distance is measured in pixels, which are very small. So a rectangle that is 10 pixels by 5 pixels is quite little. The methods to draw and fill ovals are g.drawOval (x, y, width, height) g.fillOval (x, y, width, height) It is sometimes easier to place circles and ovals on the applet screen using the center rather than the upper left hand corner. The following class has two methods that draw and fill circles given their centers and radii rather than the upper left hand corner coordinates, width and height. The methods are static. This means that they are called by the name of the class rather than an instance of the class. package zoo; /* * Carol Wolf * Date: February 12, 2007 * CS121/IS223 */ import java.awt.Graphics; /* * Methods for drawing and filling circles given their centers and radii. */ public class Circle { public static void drawCircle (Graphics g, int centerX, int centerY, int radius) { g.drawOval (centerX-radius, centerY-radius, 2*radius, 2*radius); } // drawCircle public static void fillCircle (Graphics g, int centerX, int centerY, int radius) { g.fillOval (centerX-radius, centerY-radius, 2*radius, 2*radius); } // fillCircle } // Circle Once this class has been added to the project, the methods can be used in any applet in the project. The following class uses them to draw a picture that looks something like a panda. Its only method is the paint method. The Graphics class is its only parameter. package zoo; import java.awt.*; import java.applet.Applet; /* * Carol Wolf * Date: February 12, 2007 * Picture of a panda made up of a number of circles. */ public class Figures extends Applet { public void paint (Graphics g) { // Draw the outline of the face. g.setColor (Color.black); Circle.drawCircle (g, 150, 150, 130); // Draw the eyes. Circle.fillCircle (g, 95, 150, 30); Circle.fillCircle (g, 205, 150, 30); // Draw the nose. Circle.fillCircle (g, 150, 230, 30); // Draw ears. Circle.fillCircle (g, 30, 45, 30); Circle.fillCircle (g, 270, 45, 30); } // paint }// class Figures Applet Viewer defaults to a smaller size than was needed for this picture. The default is 200 by 200 pixels. This picture needed 300 by 300. To change the size, first go into the Run menu and then select Debug.