|
Simple Graphics After today's lesson you should be able to perform simple graphical operations in Java such as displaying text, colors, and geometrical shapes. Take a look at the program below, run it, and then proceed. The applet starts out by including the awt (abstract windowing toolkit) and applet libraries. There are two methods in the applet, init and paint. When an applet is first invoked, the init method is automatically invoked to perform initializations. After init is finished, the paint method is automatically invoked to display things on the window. If you would like to explicitly call the paint method in an applet, this can be done via "repaint();". The init method begins by setting the "appletHeight" and "appletWidth" variables to the height and width (in pixels) of the display window via the "getSize()" method. The background of the display window is set to "lightGray" and the drawing color is set to "red". In the paint method, the "drawLine" method draws a red line across the top of the display window. The first two arguments are the x and y coordinates of one point on the line and the second two arguments are the x and y coordinates of the other point on the line. Notice that (0, 0) is the top left corner of the display window and (appletWidth, 0) is the top right corner of the display window. Following the drawLine method, the foreground color is set to "green". Subsequent items drawn to the display window will now be drawn using green. The next three statements illustrate the "fillPolygon" method. Arrays of corresponding x and y points are declared and initialized. The "fillPolygon" method is passed these two arrays, as well as the number of points to display in a polygon. Starting with the first x and y coordinates, lines are drawn between each successive pair of points until the fourth x, y pair is reached. Now a line is drawn from the fourth (x, y) pair to the first (x, y) pair. Since the method is "fillPolygon", the interior of the polygon (in this case a square) is filled in with a green coloring. After the foreground color is changed to "blue", three rectangle methods are invoked. The first method, "drawRect", draws a rectangle starting at the x,y coordinate given by the first two arguments. The third argument specifies the width of the rectangle, the fourth argument specifies the height. The second method, "draw3dRect" is similar to the first. The first four arguments are the same, the fifth indicates whether the 3-dimensional rectangle should have a raised (true) or lowered (false) appearance. The third method, "fillRoundRect" starts off with the same first four arguments as "drawRect". The fifth argument indicates the degree of curvature along the width of the rectangle and the sixth argument indicates the degree of curvature along the height of the rectangle. The "drawOval" method uses the same four arguments as "drawRect". For this method, an oval is drawn in the box that is bounded by the rectangle specified by the arguments. The font mode in the "paint" method is now changed from its default mode via the "setFont" method. A font consists of a name (such as "Serif"), a style (such as Font.ITALIC), and a size (such as 14 point). After the font is activated, the "drawString" method places the string "Hello" at the x,y coordinate specified by the values of the second and third arguments to the method. Following the "drawString" method, the font is again changed through the "setFont" method and then the string "Everyone" is displayed via another "drawString" method. Now that you understand the code, consult the language index regarding simple graphics to learn more about these methods. import java.awt.*; import java.applet.*; public class Simple extends Applet { public void init () { appletHeight = getSize().height; appletWidth = getSize().width; setBackground(Color.lightGray); setForeground(Color.red); } public void paint (Graphics g) { g.drawLine(0, 0, appletWidth, 0); g.setColor(Color.green); int [] xPoints = {10, 20, 20, 10}; int [] yPoints = {10, 10, 20, 20}; g.fillPolygon (xPoints, yPoints, 4); g.setColor(Color.blue); g.drawRect(50, 50, 25, 25); g.draw3DRect(100, 50, 25, 25, true); g.fillRoundRect(150, 50, 25, 25, 10, 45); g.drawOval(50, 100, 25, 25); g.setFont(new Font("Serif", Font.ITALIC, 14)); g.drawString("Hello", 50, 150); g.setFont(new Font("Monospaced", Font.BOLD, 12)); g.drawString("Everyone", 50, 175); } private int appletHeight; private int appletWidth; } What You'll See: |