Thursday 14 May 2015

Filling Shapes in Java

For each method starting with "draw," there is another method that starts with "fill." The "fill" methods have the same parameters and behave in the same way, except that they draw versions of the shapes filled with the current color.
The applet in Listing 17.11 shows how the draw and fill versions of the various commands work

Listing 17.11. Using the draw and fill commands.
import java.awt.*;
import java.applet.Applet;


public class drawshapes extends Applet
{

    public void paint(Graphics g)
    {
        int i;
        int x,y,x_o,width,height; 
        //set the x coordinates for the polygon vertices
        int x_list[] = {80,90,120,83,80};
        //set the y coordinates for the two polygons
        int y_list[] ={5,35,60,40,10}; 
        int y_list_1[] = {70,95,120,100,70};

        g.drawRect(10,5,20,20); 
        g.fillRect(10,27,20,20); 
        g.drawOval(50,5,20,20); 
        g.fillOval(50,27,20,20); 
        g.drawPolygon(x_list, y_list, 5);
        g.fillPolygon(x_list,y_list_1,5); 
        g.drawRoundRect(130,5,20,20,5,5); 
        g.fillRoundRect(130,27,20,20,10,15); 
        g.draw3DRect(160,5,20,20,true); 
        g.draw3DRect(160,27,20,20,false); 

        width = 2;
        height = 2;
        x = 10;
        x_o = 30;
        y = 50;
        //draw a set of ten regular and filled rectangles and ovals
        for(i=0;i<10;i++) {
            y += 25;
            width += 1;
            height += 2;

            g.drawRect(x,y,width,height); 
            g.drawOval(x_o,y,width,height); 
        }
    }
}


The 3D rectangles don't look all that three-dimensional. The effect is subtle, and you should experiment with it on different platforms and with different color combinations to find one that looks good.

No comments:

Post a Comment