Credits

Friday, March 26, 2010

Other Java Polygon Sample

We can practically draw any shape. I have the sample program below that shows a big octagon as a road sign. Also, we can copy sections of our graphics area by using copyArea method.


See the program below that displays copying of the rectangle to another area. I wish to draw the  text size to be bigger to go with the octagon's size. But we can always resize all these to bigger or smaller ones.

[+/-] show/hide



import java.applet.*;
import java.awt.*;


public class GraphicsPolygons extends Applet {
int xPoints[]={200,100,100,200,300,400,400,300};
int yPoints[]={100,200,300,400,400,300,200,100};
int size=xPoints.length;
Color newColor=new Color(.80F,1F,.50F);

public void init()
{
setBackground(Color.red);

}

public void paint(Graphics g)
{


g.setColor(newColor);
g.fillPolygon(xPoints,yPoints,xPoints.length); //to fill a solid polygon
g.drawRect(200,400,100,50);
g.setColor(Color.black);
g.drawString("STOP", 250, 250);
g.copyArea(200,400,101,51,400,0);
}}

AddThis