14.7 Drawing Simple Objects


14.7 Drawing Simple Objects

The coordinate system used in drawing objects places the origin of the drawing area in its upper-left corner. Figure 14.11 shows the coordinate system to represent the position of a point in the drawing area of the container.

click to expand
Figure 14.11: Position of a point in the drawing area.

All measures involved in drawing use pixels as the basic unit. The position of a visible dot is measured in the number of pixels to the right of the origin and the number of pixels below the origin. This gives the position using the coordinates (x, y).

This section presents some simple tools and techniques for drawing graphics objects. The tools are provided by predefined classes in the AWT package.

14.7.1 General Functions for Drawing

The general technique for drawing graphics objects is to define a class that inherits class JComponent and redefine function paint. An object is created, and its reference can then be added to a frame.

Function paint is invoked automatically, so there is no need to explicitly call this function. Function paint is defined with one parameter, an object reference of class Graphics, which is a class in the AWT package. The functions for drawing lines, circles, polygons, and so on, are features of the parameter of class Graphics.

To draw a line on the drawing area, from point P1(20, 45) to point P2(75, 50), function drawLine is invoked with the coordinates of the two points as arguments. The following statement draws a line from point P1 to point P2, using the object reference, graph_obj, of class Graphics.

       call drawLine of graph_obj using 20, 45, 75, 50 

This and other drawing statements must appear in function paint. The following statement draws a rectangle whose upper-left corner is located at point (80, 75), and with 50 pixels for width and 100 pixels for height.

       call drawRect of graph_obj using 80, 75, 50, 100 

The following statements draw an arc. The arc is part of an oval that is specified by an enclosing rectangle with the upper-left corner located in (x, y), and the size given by width and height. The portion of the arc to draw is specified by the starting angle and the final angle (in degrees).

       int x = 20       int y = 50       width = 35       height = 25       startang = 0       finalang = 45       ...       call drawArc of graph_obj using                 x, y, width, height, startang, finalang 

Other drawing functions defined in class Graphics are listed in Table 14.2.

Table 14.2: Common drawing functions in class Graphics

drawOval

Draws the outline of an oval

draw2DRect

Draws a highlighted outline of a rectangle

drawPolygon

Draws a closed polygon

drawRoundRect

Draws a round-cornered rectangle

fillArc

Fills a portion of an oval with color

fillOval

Fills an oval with color

fillPolygon

Fills a polygon with color

fillRect

Fills a rectangle with color

fillRoundRect

Fills a round-cornered rectangle with color

fill3Drect

Fills a rectangle with color

14.7.2 A GUI with Circles

This section presents an example program that shows two components on a frame: a drawing area with several circles of different sizes and a button with a listener. The program consists of several classes.

Class DrawExample is the main class of the application. It creates a frame with border layout manager for placing the two components. The first component is the drawing area with several circles drawn; an object of class MydrawE is created and placed in the center position of the frame. The button is placed in the south position. The KJP code that implements class DrawExample is shown as follows.

       import all java.awt       import all javax.swing       description          This is the main class that presents an example          of drawing simple objects.    */       class DrawExample is         public          description            The main function.      */          function main is           constants             integer WIDTH = 300             integer HEIGHT = 250           objects             object dframe of class JFrame             object cpane of class Container             // object for drawing area             object mdrawing of class MydrawE             // button             object quitbutt of class JButton             object butthandler of class Bquithandler             object bordermanager of class BorderLayout           begin             create dframe of class JFrame using                   "Drawing example"             set cpane = call getContentPane of dframe             create mdrawing of class MydrawE             create quitbutt of class JButton using "Quit"             create bordermanager of class BorderLayout             call setLayout of cpane using bordermanager             create butthandler of class Bquithandler             call add of cpane using mdrawing,                    BorderLayout.CENTER             call add of cpane using quitbutt,                    BorderLayout.SOUTH             // register the listener object with the             // button object             call addActionListener of quitbutt                    using butthandler             call setSize of dframe using WIDTH, HEIGHT             call setVisible of dframe using true          endfun main       endclass DrawExample 

Class MydrawE inherits class JComponent, and the relevant function is paint. This function draws 15 circles of different sizes and at different locations. The KJP code for class MydrawE is listed as follows.

       import all java.awt       import all javax.swing       description         This class draws several ovals.  */       class MydrawE inherits JComponent is         public         description           Paint where the actual drawings are.  */         function paint parameters object gobj of              class Graphics is          constants             integer TIMES = 15 // number of circles          variables             // position in drawing area             integer x             integer y             // width and height             integer w             integer h             integer j  // loop counter          begin             set x = 50             set y = 20             set w = 170             set h = 170             set j = 0             for j = 0 to TIMES - 1 do                // draw a circle                call drawOval of gobj using x, y, w, h                add 5 to x                add 5 to y                subtract 5 from w                subtract 5 from h             endfor          endfun paint       endclass MydrawE 

Figure 14.12 shows the top part of the frame that contains the drawing area with several circles of different sizes and the bottom part of the frame that contains a single button for exiting the program when the user clicks it.

click to expand
Figure 14.12: A frame with drawing area and button.

On the CD

The KJP code that implement classes DrawExample and MydrawE are stored in the files DrawExample.kpl and MydrawE.kpl. The Java code are stored in the files DrawExample.java and MydrawE.java.

Another way to organize the program is to define a single class that inherits class JFrame and include a constructor, function main, and function paint. This is a single-class application.




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net