rendered paste body[sadam@serv16 ~/Fall05]$ cat P3/Poly.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Poly extends JFrame
{
int polyCount = 0;
int firstpoint = 0;
int lastx, lasty;
Color selectedColor;
String[] comboList = {"Red","Blue","Green","Black","Yellow"};
JComboBox combo = new JComboBox(comboList);
JMenuItem menuRed;
JMenuItem menuBlue;
JMenuItem menuGreen;
JMenuItem menuBlack;
JMenuItem menuYellow;
MyPolygon[] polygons = new MyPolygon[5];
JPanel centerPanel = new DrawPanel();
int transx, transy;
Poly()
{
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e){System.exit(0);}});
MH mh = new MH();
CH ch = new CH();
JPanel southPanel = new JPanel();
southPanel.setLayout(new BorderLayout());
centerPanel.setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Color");
JMenuItem menuRed = new JMenuItem("Red");
menuRed.addActionListener(mh);
JMenuItem menuBlue = new JMenuItem("Blue");
menuBlue.addActionListener(mh);
JMenuItem menuGreen = new JMenuItem("Green");
menuGreen.addActionListener(mh);
JMenuItem menuBlack = new JMenuItem("Black");
menuBlack.addActionListener(mh);
JMenuItem menuYellow = new JMenuItem("Yellow");
menuYellow.addActionListener(mh);
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(mh);
menu.add(menuRed);
menu.add(menuBlue);
menu.add(menuGreen);
menu.add(menuBlack);
menu.add(menuYellow);
menu.addSeparator();
menu.add(exit);
menuBar.add(menu);
setJMenuBar(menuBar);
southPanel.add(combo);
combo.add(ch);
getContentPane().add(southPanel, "South");
centerPanel.setBackground(Color.white);
getContentPane().add(centerPanel, "Center");
}
class MyPolygon extends Polygon
{
boolean isComplete = false;
Color color;
MyPolygon(Color c)
{
color = c;
}
}
class DrawPanel extends JPanel
{
DrawPanel()
{
addMouseListener(new Down());
addMouseMotionListener(new Drag());
}
}
class Drag extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
int x = e.getX(), y = e.getY();
if(e.isShiftDown())
{
for(int count = 4; count >= 0; count--)
{
if(polygons[count] != null)
{
if(polygons[count].contains(x,y))
{
polygons[count].translate(transx-x,transy-y);
}
break;
}
}
}
}
}
class Down extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
Graphics g = centerPanel.getGraphics();
int x = e.getX(), y = e.getY();
transx = x; transy = y;
if(firstpoint == 0)
polygons[polyCount] = new MyPolygon(selectedColor);
polygons[polyCount].addPoint(x,y);
if(firstpoint == 0)
firstpoint = 1;
else
g.drawLine(lastx, lasty, x, y);
lastx = x; lasty = y;
if(e.getClickCount() == 2)
{
firstpoint = 0;
g.setColor(polygons[polyCount].color);
g.fillPolygon(polygons[polyCount]);
}
}
}
class CH implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
JComboItem selection = (JComboItem)source;
String cmd = selection.getText();
if(cmd.equals("Red"))
{
selectedColor = Color.red;
combo.setSelectedIndex(0);
}
else if(cmd.equals("Blue"))
{
if(firstpoint == 0)
g.setColor(Color.blue);
combo.setSelectedIndex(1);
}
else if(cmd.equals("Green"))
{
if(firstpoint == 0)
g.setColor(Color.green);
combo.setSelectedIndex(2);
}
else if(cmd.equals("Black"))
{
if(firstpoint == 0)
g.setColor(Color.black);
combo.setSelectedIndex(3);
}
else if(cmd.equals("Yellow"))
{
if(firstpoint == 0)
g.setColor(Color.yellow);
combo.setSelectedIndex(4);
}
}
}
class MH implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Graphics g = centerPanel.getGraphics();
Object source = e.getSource();
JMenuItem selection = (JMenuItem)source;
String cmd = selection.getText();
if(cmd.equals("Red"))
{
selectedColor = Color.red;
combo.setSelectedIndex(0);
}
else if(cmd.equals("Blue"))
{
if(firstpoint == 0)
g.setColor(Color.blue);
combo.setSelectedIndex(1);
}
else if(cmd.equals("Green"))
{
if(firstpoint == 0)
g.setColor(Color.green);
combo.setSelectedIndex(2);
}
else if(cmd.equals("Black"))
{
if(firstpoint == 0)
g.setColor(Color.black);
combo.setSelectedIndex(3);
}
else if(cmd.equals("Yellow"))
{
if(firstpoint == 0)
g.setColor(Color.yellow);
combo.setSelectedIndex(4);
}
else
System.exit(0);
for(int count = 4; count >= 0; count--)
{
if(polygons[count] != null)
{
if(polygons[count].isComplete)
{
g.setColor(polygons[count].color);
g.fillPolygon(polygons[count]);
}
}
}
}
}
public static void main(String [] args)
{
JFrame poly = new Poly();
poly.setTitle("Polygons!");
poly.setSize(500,400);
poly.setVisible(true);
}
}