Drawing Ellipses


If the ellipse flag is TRue, the user is drawing ellipses. Ellipses are a little more complex, because they can be filled in with color, texture, gradients, and so on. You can start by defining the ellipse the user wants to draw:

     if(ellipse && width != 0 && height != 0){         Ellipse2D.Double ellipse =             new Ellipse2D.Double(tempStart.x, tempStart.y,             width, height); . . . 

If the shadow flag is set to true, the user wants to draw drop shadows, which you can do like this (note that if the ellipse is filled in, which means one of the solid, shade, transparent, or texture flags will be set to true, you should draw the shadow to match, which you do with the gImage context's fill method, not the draw method):

         if(shadow){             paint = gImage.getPaint();             composite = gImage.getComposite();             gImage.setPaint(Color.black);             gImage.setComposite(AlphaComposite.getInstance                 (AlphaComposite.SRC_OVER, 0.3f));             Ellipse2D.Double ellipse2 =                 new Ellipse2D.Double(tempStart.x + 9,                 tempStart.y + 9, width, height);             if(solid || shade || transparent || texture){                 gImage.fill(ellipse2);             }             else{                 gImage.draw(ellipse2);             }             gImage.setPaint(paint);             gImage.setComposite(composite);         } . . . 

That draws the drop shadow first, if one is needed. But what about other graphics effects? For example, the user may have selected the Solid fill item in the Effects menu, which means he wants to fill the ellipse with a solid color. You can set the fill color with the Graphics2D object's setColor method, setting that color to the current drawing color like this:

         if(solid){             gImage.setPaint(color);         } . . . 

You can see what a solid-filled ellipse looks like, with a drop shadow added, in the upper-left area of Figure 4.3.

Figure 4.3. Various graphics effects with ellipses.


If the user wants to draw an ellipse with a gradient fill, you can use the Java2D GradientPaint class. You pass this class's constructor the location at which the gradient should start, the starting color, the location where the gradient should end, and the ending color. Painter uses the drawing color as the starting color and uses black as the ending color:

         if(shade){             gImage.setPaint(             new GradientPaint(                 tempStart.x, tempStart.y, color,                 tempEnd.x, tempEnd.y, Color.black));         } . . . 

You can see what a gradient-filled ellipse looks like, also with a drop shadow, in the middle of Figure 4.3. If the user wants to use a texture fill, you can use the Java2D TexturePaint class. You pass an image to use for the texture fill, and you pass a rectangle to anchor the image location.

Painter uses the tileImage object it read in from the file tile.jpg in its constructor, creating a texture fill like this:

         if(texture){             Rectangle2D.Double anchor =             new Rectangle2D.Double(0, 0, tileImage.getWidth(),             tileImage.getHeight());             TexturePaint texturePaint =                 new TexturePaint(tileImage, anchor);             gImage.setPaint(texturePaint);         } . . 

You can see what a gradient-filled ellipse looks like, with an added drop shadow, in the lower-right area of Figure 4.3.

Another graphics effect is to make figures "transparent" so that the underlying image comes through. If the user has selected the Effects menu's Transparent item, the transparent flag will be true, and the code sets the graphics context's composite to use a transparent fill:

         if(transparent){             gImage.setComposite(AlphaComposite.getInstance             (AlphaComposite.SRC_OVER,0.1f));         } . . . 

You can see what a transparent ellipse looks like in Figure 4.3; it's the largest ellipse shown there. It overlaps all the others (which are still visible through it).

All that work has set up the actual ellipse-drawing operation. If the ellipse is solid, you should use the Graphics2D fill method to draw it; otherwise, you use the draw method. And if the ellipse was drawn transparent, you also should restore the composite in the Graphics2D graphics context, like this:

         if(solid || shade || transparent || texture){             gImage.fill(ellipse);         }         else{             gImage.draw(ellipse);         }         if(transparent){             gImage.setComposite(composite);         }     } 

And that completes the drawing operations for ellipses, complete with the various effects that Painter supports.



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

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