New question. Look at the following code. I am able to draw a polygon. When I click on the 'Polygon' button, it erases the current polygon and I'm able to draw a new polygon. How can I draw a polygon, and then draw another polygon without the first one being erased? I'm thinking that the paint method maybe isn't what I'm looking for?
Thanks. See code below.
import java.awt.*;
import java.applet.Applet;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
//****************************
// class polygon
//****************************
public class polygon3 extends
Applet {
final int NUMPOINTS = 100;
Button polygonButton;
int coordx[];
int coordy[];
int numCoord = 0;
int ctr = 0;
int i = 0;
int isFinish = 0; // 0 = false, 1 = true
int isNew = 0; // 0 = false, 1 = true
int isFirst = 1; //
//**************************
// init
//**************************
public void init()
{
polygonButton = new Button ("Polygon");
add(polygonButton);
coordx = new int[NUMPOINTS];
coordy = new int[NUMPOINTS];
for (i = 0; i < NUMPOINTS; i++)
{
coordx[i] = 0;
coordy[i] = 0;
}
}//end of method init
//****************************
// action
//****************************
public boolean action(Event e, Object o)
{
if (e.target == polygonButton)
{
isFinish = 0;
isNew = 1;
isFirst = 1;
for (i = 0; i < NUMPOINTS; i++)
{
coordx[i] = 0;
coordy[i] = 0;
numCoord = 0;
}// end for i
}// end if polygonButton
repaint();
return true;
}//end of method action
//****************************
// mouseDown
//****************************
public boolean mouseDown (Event evt, int x, int y)
{
if (isNew == 0)
{
if (x-coordx[0] < 2)
{
if (y-coordy[0] < 2)
{
isFinish = 1;
}
}
}
if (isFinish == 0) {
coordx[numCoord] = x;
coordy[numCoord] = y;
numCoord++;
}
repaint();
return true;
}// end mouseDown
//****************************
// mouseDrag
//****************************
public boolean mouseDrag (Event evt, int x, int y)
{
return true;
}// end mouseDrag
//****************************
// mouseUp
//****************************
public boolean mouseUp (Event evt, int x, int y)
{
return true;
}// end mouseUp
//****************************
// paint
//****************************
public void paint(Graphics g)
{
g.setColor(Color.white);
g.drawString("test",10,10);
// if (isNew == 1) {
//Image theImage = getImage(getCodeBase(), "field.jpg");
//Graphics2D g2D = (Graphics2D) g;
//g2D.drawImage(theImage, 0, 0, this);
// }
if (isFirst == 0)
{
if (coordx[i+1] == coordx[0])
{
if (coordy[i+1] == coordy[0])
{
isFinish = 1;
}
}
}
if (isNew == 1)
{
isNew = 0;
}
String myVar = String.valueOf(numCoord);
g.drawString(myVar,25,45);
for (i = 0; i < numCoord-1; i++)
{
if (isFinish == 0) {
isFirst = 0;
g.setColor(Color.green);
g.drawLine(coordx[i],coordy[i],coordx[i+1],coordy[i+1]);
g.setColor(Color.red);
g.setColor(Color.red);
g.drawRect(coordx[i] - 1,coordy[i] -1,2,2);
}
}
if (isFinish == 1)
{
g.setColor(Color.black);
g.drawPolygon(coordx,coordy,numCoord);
}
}//end of method paint
}//end of class polygon