Drawing Shapes

The Java2D API provides a base interface java.awt.Shape that all geometric shape classes implement. The package java.awt.geom contains many useful shape classes, such as Ellipse2D (which is used for drawing circles too). The classes implementing the Shape interface can then be passed to Graphics2D methods, such as draw(Shape) and fill(Shape). The following example shows a few simple shapes being drawn in the paintComponent method. The shape in the middle should be quite familiar to most of you, and if not, then shame on you. Here is the code for DrawingShapes.java.

Code Listing 9-3: DrawingShapes.java

start example
import javax.swing.*; import java.awt.*; import java.awt.geom.*;   public class DrawingShapes extends JFrame {     public DrawingShapes()     {         super("Drawing Shapes Demo");         setDefaultCloseOperation(EXIT_ON_CLOSE);         setResizable(false);         getContentPane().setLayout(null);         getContentPane().add(new DisplayArea(new Rectangle(0, 0,                DISPLAY_WIDTH, DISPLAY_HEIGHT)));              setVisible(true);           resizeToInternalSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);     }               public void resizeToInternalSize(int internalWidth, int            internalHeight)     {         Insets insets = getInsets();         final int newWidth = internalWidth + insets.left +               insets.right;         final int newHeight = internalHeight + insets.top +               insets.bottom;              Runnable resize = new Runnable()         {             public void run()             {                 setSize(newWidth, newHeight);             }         };              if(!SwingUtilities.isEventDispatchThread())         {             try             {                 SwingUtilities.invokeAndWait(resize);             }             catch(Exception e) {}         }         else             resize.run();              validate();     }           public class DisplayArea extends JPanel     {         public DisplayArea(Rectangle bounds)         {             setLayout(null);             setBounds(bounds);             setOpaque(false);         }              public void paintComponent(Graphics g)         {             Graphics2D g2D = (Graphics2D)g;                  g2D.setColor(Color.black);             g2D.fillRect(0, 0, getWidth(), getHeight());               for(int i=0; i<shapeList.length; i++)             {                 g2D.setColor(colorList[i]);                 g2D.fill(shapeList[i]);             }         }     }            public static void main(String[] args)     {         new DrawingShapes();     }          private static Shape[] shapeList = {         new Rectangle(10, 10, 100, 100),         new Ellipse2D.Double(290, 290, 100, 100),         new Arc2D.Double(150, 150, 100, 100, 45, 270, Arc2D.PIE)        };            private static Color[] colorList = { Color.red, Color.blue,             Color.yellow };          private static final int DISPLAY_WIDTH = 400;     private static final int DISPLAY_HEIGHT = 400; } 
end example

When you run this example, you should get output similar to the following figure.

click to expand
Figure 9-4:

A further advantage of shape classes is clipping. Clipping is the ability to exclude an area from the drawing process or, alternatively, specify the boundary to which your drawing must be contained. For example, we could have specified in the last example a clipping area of the top-left quarter of the (400x400) displayable area defined by a rectangle at (0, 0) and dimension (200, 200). The effect on our drawing would be that we would see the red square completely, the yellow shape partially, and the blue circle not at all, where any drawing outside of the clipping area is not performed. This is most beneficial when you are unsure of the scope of area that your drawing routine will cover but are sure of the bounding area you want the drawing to be kept within. The Graphics object allows you to set the clipping area to any class, implementing the Shape interface and using most notably the method setClip(Shape) of the Graphics class.



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

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