I have a problem with drawing a polygon in Java applet. I don’t understand what is wrong with this code. I have a class called DrawPoly that contains:
import java.awt.*;
import java.awt.geom.Point2D;
import java.applet.Applet;
import java.util.Vector;
public class DrawPoly extends Applet{
Polygon poly;
public void init(Graphics g){
Polygon poly=new Polygon();
poly.addPoint(60,100);
}
public void paint(Graphics g)
{
Graphics2D gg;
gg=(Graphics2D) g;
System.out.println("number is"+ poly.npoints);
}
}
I don’t understand why the following code doesn’t work. I would like to create a global polygon, initialize it in init, and then do some 2D transformations such as rotation in paint. What am I missing? I only added one point to see if the initializations are corect. I don’t want to initialize the polygon in paint(), because i may want to use repaint() and the polygon would be initialized every time i call repaint(). If you could please help me out. Thanks a lot.
1 Answer