import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawPanel extends JPanel
{
private int shape, color, xValue, yValue, length, orientation, i;
private final int CIRCLE = 0, RECTANGLE = 1, LINE = 2, RED = 0, YELLOW = 1, GREEN = 2;
private final int BLUE = 3, BLACK = 4, HORIZONTAL = 0, VERTICAL = 1, DRAW = 200;
private JLabel figure, colorLabel, location;
private JRadioButton circle, rectangle, line, red, yellow, green, blue, black;
private JCheckBox random;
private JTextField xValueField, yValueField;
private JButton draw, set, horizontal, vertical;
private JPanel panel;
private JOptionPane optionPane;
public DrawPanel()
{
panel = new JPanel();
panel.setPreferredSize(new Dimension(200, 150));
panel.setBackground(Color.gray);
figure = new JLabel ("Figure panel: ");
circle = new JRadioButton ("circle", true);
circle.setBackground(Color.cyan);
rectangle = new JRadioButton ("rectangle");
rectangle.setBackground(Color.cyan);
line = new JRadioButton ("line");
line.setBackground(Color.cyan);
ButtonGroup figureOptions = new ButtonGroup();
figureOptions.add(circle);
figureOptions.add(rectangle);
figureOptions.add(line);
colorLabel = new JLabel ("Color panel:");
red = new JRadioButton ("red", true);
red.setBackground(Color.red);
yellow = new JRadioButton ("yellow");
yellow.setBackground(Color.yellow);
green = new JRadioButton ("green");
green.setBackground(Color.green);
blue = new JRadioButton ("blue");
blue.setBackground(Color.blue);
black = new JRadioButton ("black");
black.setBackground(Color.gray);
ButtonGroup colorOptions = new ButtonGroup();
colorOptions.add(red);
colorOptions.add(yellow);
colorOptions.add(green);
colorOptions.add(blue);
colorOptions.add(black);
ShapeButtonListener sbListener = new ShapeButtonListener();
circle.addActionListener(sbListener);
rectangle.addActionListener(sbListener);
line.addActionListener(sbListener);
ColorButtonListener cbListener = new ColorButtonListener();
red.addActionListener(cbListener);
yellow.addActionListener(cbListener);
green.addActionListener(cbListener);
blue.addActionListener(cbListener);
black.addActionListener(cbListener);
location = new JLabel("Location panel:");
random = new JCheckBox("random");
random.setBackground(Color.cyan);
xValueField = new JTextField(3);
JLabel xValueLabel = new JLabel("x value:");
yValueField = new JTextField(3);
JLabel yValueLabel = new JLabel("y value:");
XYListener xyListener = new XYListener();
xValueField.addActionListener(xyListener);
yValueField.addActionListener(xyListener);
RandomListener rListener = new RandomListener();
random.addItemListener(rListener);
draw = new JButton("Draw!");
DrawListener dListener = new DrawListener();
draw.addActionListener(dListener);
set = new JButton("Set");
SetListener sListener = new SetListener();
set.addActionListener(sListener);
panel.add(circle);
panel.add(rectangle);
panel.add(line);
panel.add(red);
panel.add(yellow);
panel.add(green);
panel.add(blue);
panel.add(black);
panel.add(random);
panel.add(xValueLabel);
panel.add(xValueField);
panel.add(yValueLabel);
panel.add(yValueField);
panel.add(set);
panel.add(draw);
add(panel);
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
switch(color)
{
case RED:
page.setColor(Color.red);
break;
case YELLOW:
page.setColor(Color.yellow);
break;
case GREEN:
page.setColor(Color.green);
break;
case BLUE:
page.setColor(Color.blue);
break;
case BLACK:
page.setColor(Color.black);
break;
default:
page.setColor(Color.white);
}
switch (shape)
{
case CIRCLE:
page.fillOval(xValue, yValue + DRAW, 50, 50);
break;
case RECTANGLE:
page.fillRect(xValue, yValue + DRAW, 50, 60);
break;
case LINE:
switch(orientation)
{
case HORIZONTAL:
page.drawLine(xValue, yValue + DRAW, xValue + length, yValue + DRAW);
break;
case VERTICAL:
page.drawLine(xValue, yValue + DRAW, xValue, yValue + length + DRAW);
break;
}
break;
default:
System.out.println("You didn't select a figure!");
}
}
private class ShapeButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource() == circle)
{
shape = CIRCLE;
}
else
{
if (event.getSource() == rectangle)
{
shape = RECTANGLE;
}
else
{
shape = LINE;
optionPane = new JOptionPane();
length = Integer.parseInt(optionPane.showInputDialog("Enter the length of the line:"));
horizontal = new JButton("horizontal");
vertical = new JButton ("vertical");
Object[] orientations = {"horizontal", "vertical"};
i = optionPane.showOptionDialog(null,"Pick your orientation.",
"Line Options", optionPane.YES_NO_OPTION,
optionPane.QUESTION_MESSAGE, null, orientations, orientations[1]);
OrientButtonListener obListener = new OrientButtonListener();
horizontal.addActionListener(obListener);
vertical.addActionListener(obListener);
}
}
}
private class OrientButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (i == optionPane.YES_OPTION)
{
orientation = HORIZONTAL;
}
else
{
orientation = VERTICAL;
}
}
}
}
private class ColorButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == red)
{
color = RED;
}
else
{
if (event.getSource() == yellow)
{
color = YELLOW;
}
else
{
if (event.getSource() == green)
{
color = GREEN;
}
else
{
if (event.getSource() == blue)
{
color = BLUE;
}
else
{
color = BLACK;
}
}
}
}
}
}
private class XYListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String xText = xValueField.getText();
xValue = Integer.parseInt(xText);
String yText = yValueField.getText();
yValue = Integer.parseInt(yText);
}
}
private class RandomListener implements ItemListener
{
public void itemStateChanged(ItemEvent event)
{
if (random.isSelected())
{
xValue = (int)(Math.random() * 100) + DRAW;
yValue = (int)(Math.random() * 100)+ DRAW;
}
}
}
private class DrawListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int okay = JOptionPane.showConfirmDialog(null, "Are you sure you want to continue?");
if (okay == JOptionPane.YES_OPTION);
{
repaint();
}
}
}
private class SetListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
xValue = Integer.parseInt(xValueField.getText());
yValue = Integer.parseInt(yValueField.getText());
}
}
}