Write a simple graphics editor that allows users to add a mixture of shapes (ellipses, rectangles, and lines in different colors) to a panel. Supply commands to load and save the picture.
Basically it wants a program with a buttons for New, Load, Save, Rectangle, Line, Ellipse, Red, Green, Blue, Yellow. You press a button for a shape, and then press a button for a color, and the program draws it. The location is always the same for each shape, as it creates a face.
I'm having a problem however. I click the button for my shape, and then click the color, and nothing happens. No matter what buttons I click, I get no results.
My code:
The Viewer:
import javax.swing.JFrame;
/**
* Course Number: Comp 1123.
* Assignment Number: 5
* Instructor: Rick Giles
* Question Number: 1
* Allows you to view graphics editor
* @author Thomas Symonds
* @version July 10, 08
*/
public class GraphicsViewer
{
public static void main(String[] args)
{
JFrame frame = new GraphicsEditor();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
The main class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
/**
* Course Number: Comp 1123.
* Assignment Number:
* Instructor: Rick Giles
* Question Number:
*
* @author Thomas Symonds
* @version
*/
public class GraphicsEditor extends JFrame
{
public GraphicsEditor()
{
circle = new Ellipse(100, 100);
eyes = new Line(115, 115);
box = new RectangleM(120, 170);
createNew();
// createSave();
// createLoad();
createRectangle();
createLine();
createCircle();
createRed();
createYellow();
createBlue();
createGreen();
createPanel();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public void createNew()
{
aNew = new JButton("New");
class newListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
circle = new Ellipse(0, 0);
box = new RectangleM(0, 0);
eyes = new Line(0, 0);
}
}
ActionListener listener = new newListener();
aNew.addActionListener(listener);
}
public void createRectangle()
{
rectangle = new JButton("Rectangle");
class rectangleListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
box = new RectangleM(130, 170);
x = box;
}
}
ActionListener listener2 = new rectangleListener();
rectangle.addActionListener(listener2);
}
public void createLine()
{
line = new JButton("Line");
class lineListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
eyes = new Line(115, 115);
x = eyes;
}
}
ActionListener listener3 = new lineListener();
line.addActionListener(listener3);
}
public void createCircle()
{
ellipse = new JButton("Ellipse");
class circleListener implements Graphic, ActionListener
{
public void actionPerformed(ActionEvent event)
{
circle = new Ellipse(100, 100);
x = circle;
}
public void draw(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
circle.draw(g2);
}
}
ActionListener listener4 = new circleListener();
ellipse.addActionListener(listener4);
}
public void createRed()
{
red = new JButton("Red");
class circleListener implements ActionListener, Graphic
{
public void actionPerformed(ActionEvent event)
{
if(x instanceof Ellipse)
{
face = (Ellipse)x;
}
else if(x instanceof RectangleM)
{
m = (RectangleM)x;
}
else
{
line = (Line)x;
}
}
public void draw(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
face.draw(g2);
line.draw(g2);
m.draw(g2);
}
private Ellipse face;
private Line line;
private RectangleM m;
}
ActionListener listener5 = new circleListener();
red.addActionListener(listener5);
}
public void createYellow()
{
yellow = new JButton("Yellow");
class circleListener implements ActionListener, Graphic
{
public void actionPerformed(ActionEvent event)
{
if(x instanceof Ellipse)
{
face = (Ellipse)x;
}
else if(x instanceof RectangleM)
{
m = (RectangleM)x;
}
else
{
line = (Line)x;
}
}
public void draw(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.YELLOW);
face.draw(g2);
line.draw(g2);
m.draw(g2);
}
private Ellipse face;
private Line line;
private RectangleM m;
}
ActionListener listener6 = new circleListener();
yellow.addActionListener(listener6);
}
public void createBlue()
{
blue = new JButton("Blue");
class circleListener implements ActionListener, Graphic
{
public void actionPerformed(ActionEvent event)
{
if(x instanceof Ellipse)
{
face = (Ellipse)x;
}
else if(x instanceof RectangleM)
{
m = (RectangleM)x;
}
else
{
line = (Line)x;
}
}
public void draw(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
face.draw(g2);
line.draw(g2);
m.draw(g2);
}
private Ellipse face;
private Line line;
private RectangleM m;
}
ActionListener listener7 = new circleListener();
blue.addActionListener(listener7);
}
public void createGreen()
{
green = new JButton("Green");
class circleListener implements ActionListener, Graphic
{
public void actionPerformed(ActionEvent event)
{
if(x instanceof Ellipse)
{
face = (Ellipse)x;
}
else if(x instanceof RectangleM)
{
m = (RectangleM)x;
}
else
{
line = (Line)x;
}
}
public void draw(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.GREEN);
face.draw(g2);
line.draw(g2);
m.draw(g2);
}
private Ellipse face;
private Line line;
private RectangleM m;
}
ActionListener listener8 = new circleListener();
green.addActionListener(listener8);
}
private void createPanel()
{
panel = new JPanel();
panel.add(aNew);
// panel.add(save);
// panel.add(load);
panel.add(rectangle);
panel.add(line);
panel.add(ellipse);
panel.add(yellow);
panel.add(red);
panel.add(blue);
panel.add(green);
add(panel);
}
private static JPanel panel;
private static final int FRAME_WIDTH = 450;
private static final int FRAME_HEIGHT = 600;
private static JButton aNew;
private static JButton save;
private static JButton load;
private static JButton rectangle;
private static JButton line;
private static JButton ellipse;
private static JButton yellow;
private static JButton red;
private static JButton blue;
private static JButton green;
private static Object x;
private static Ellipse circle;
private static Line eyes;
private static RectangleM box;
}
Supporting classes:
import java.awt.geom.Line2D;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
* Course Number: Comp 1123.
* Assignment Number: 5
* Instructor: Rick Giles
* Question Number: 1
* The class creates Line objects.
* @author Thomas Symonds
* @version July 10, 08
*/
public class Line implements Graphic
{
public Line(int a, int b)
{
c = a;
d = b;
}
public void draw(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Line2D.Double line1 = new Line2D.Double(c, d, 125, 125);
Line2D.Double line2 = new Line2D.Double(c + 70, d + 70, 175, 175);
g2.draw(line1);
g2.draw(line2);
}
private int c;
private int d;
}
import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
* Course Number: Comp 1123.
* Assignment Number: 5
* Instructor: Rick Giles
* Question Number: 1
* The class creates rectangle objects
* @author Thomas Symonds
* @version
*/
public class RectangleM implements Graphic
{
public RectangleM(int a, int b)
{
c = a;
d = b;
}
public void draw(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Rectangle box = new Rectangle(c, d, 170, 170);
g2.draw(box);
}
private int c;
private int d;
}
import java.awt.geom.Ellipse2D;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
* Course Number: Comp 1123.
* Assignment Number: 5
* Instructor: Rick Giles
* Question Number: 1
* this class creates Ellipse objects
* @author Thomas Symonds
* @version
*/
public class Ellipse implements Graphic
{
public Ellipse(int a, int b)
{
c = a;
d = b;
}
public void draw(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double circle = new Ellipse2D.Double(c, d, 100, 100);
g2.draw(circle);
}
private int c;
private int d;
}
The interface:
import java.awt.Graphics;
/**
* graphic interface
*
* @author Thomas Symonds
* @version July 10, 08.
*/
public interface Graphic
{
void draw(Graphics g);
}