/*
Polygon.java - Cory J. Goldfuss - 11 Feb 2001
Program shows use of the drawPolygon method. Program assumes correct input
from the user.
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Polygon extends Applet implements ActionListener
{
private static final int SIZE = 50;
private int numPoints = 0;
private int i = -1;
private int xNumber;
private int yNumber;
private int xPlot[] = new int[SIZE];
private int yPlot[] = new int[SIZE];
public void init()
{
// Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
//symantec.itools.lang.Context.setApplet(this);
// This code is automatically generated by Visual Cafe when you add
// components to the visual environment. It instantiates and initializes
// the components. To modify the code, only use code syntax that matches
// what Visual Cafe can generate, or Visual Cafe may be unable to back
// parse your Java file into its visual environment.
//{{INIT_CONTROLS
setLayout(null);
setBackground(java.awt.Color.lightGray);
setSize(500,400);
drawBtn.setLabel("Draw Poly");
add(drawBtn);
drawBtn.setBackground(java.awt.Color.green);
drawBtn.setBounds(336,348,93,35);
drawBtn.setVisible(false);
add(xValues);
xValues.setBounds(36,360,36,27);
add(yValues);
yValues.setBounds(120,360,36,27);
xLbl.setText("X-value");
add(xLbl);
xLbl.setBounds(36,336,48,19);
yLbl.setText("Y-value");
add(yLbl);
yLbl.setBounds(120,336,48,19);
enterBtn.setLabel("Enter Points");
add(enterBtn);
enterBtn.setBackground(java.awt.Color.pink);
enterBtn.setBounds(192,348,120,34);
resetBtn.setLabel("Reset");
add(resetBtn);
resetBtn.setBackground(java.awt.Color.red);
resetBtn.setBounds(444,348,53,33);
resetBtn.setVisible(false);
//}}
xValues.addActionListener( this );
yValues.addActionListener( this );
drawBtn.addActionListener( this );
enterBtn.addActionListener( this );
resetBtn.addActionListener( this );
}
//{{DECLARE_CONTROLS
java.awt.Button drawBtn = new java.awt.Button();
java.awt.TextField xValues = new java.awt.TextField();
java.awt.TextField yValues = new java.awt.TextField();
java.awt.Label xLbl = new java.awt.Label();
java.awt.Label yLbl = new java.awt.Label();
java.awt.Button enterBtn = new java.awt.Button();
java.awt.Button resetBtn = new java.awt.Button();
//}}
public void actionPerformed( ActionEvent e ) {
if (e.getSource() == enterBtn){
drawBtn.setEnabled( true );
numPoints++;
i++;
//prevent drawing of anything other than a polygon
if( numPoints == 3){
drawBtn.setVisible( true );
}
xNumber = Integer.parseInt( xValues.getText() );
xPlot[i] = xNumber;
yNumber = Integer.parseInt( yValues.getText() );
yPlot[i] = yNumber;
xValues.setText("");
yValues.setText("");
}
if (e.getSource() == resetBtn){
xValues.setText("");
yValues.setText("");
i = -1;
numPoints = 0;
drawBtn.setVisible( false );
resetBtn.setVisible( false );
enterBtn.setEnabled( true );
//reset arrays
for (int j = 0; j < SIZE; j++ ){
xPlot[j] = 0;
yPlot[j] = 0;
}
repaint();
}
if (e.getSource() == drawBtn){
repaint();
enterBtn.setEnabled( false );
resetBtn.setVisible(true);
drawBtn.setEnabled( false );
}
}
// Make user's picture
public void paint( Graphics g ){
g.drawPolygon(xPlot, yPlot, numPoints);
}
}