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);
  
}

Ok I saw some weird lines in your code.

It looks like you declared inner classes within the scope of your methods.

I'm really not sure how Java handles that. Typically any type that is declared within a method is destroyed unless it is a valid pointer.

Then again I really didn't know you could declare an inner class within a method.

If you want to save yourself some code, try declaring an anonymous interface object of type ActionListener that is locally scoped to the class--

ActionListener al = new ActionListener()
{
     public void actionPerformed(ActionEvent e)
     {
            
               // code here

     }
};

Furthermore you can actually set the ActionCommand of each object to a String of your choosing. For example--

/*
   rectangle = new JButton("Rectangle");
   rectangle.setActionCommand("Rect1");
*/

ActionListener al = new ActionListener()
{
     public void actionPerformed(ActionEvent e)
     {
                          

               if(e.getActionCommand().equalsIgnoreCase("Rect1"))
               {
                       // functionality for object with action command "Rect1"
               }
               // else if (e.getActionCommand... snip

     }
};

Then you can simply say something like--

rectangle.addActionListener(al);

nothing is appearing on the screen because you are not overriding paint(Graphics) , if you want to draw stuff on a JComponent you must override paint(Graphics) , and then when you want to change something you need to call repaint() .

public class DrawComponent extends JComponent{
    public void paint(Graphics g){
         
         //DRAW STUFF HERE
         
    }
}
//illustrates repaint, animation, changes every time this class is ran
class Painter implements Runnable {
    DrawComponent dc;
    public Painter(DrawComponent d){
        dc=d;
    }
    public void run(){
        dc.repaint();
    }
}

i am also not sure about the inner classes within a method, but if it compiles then i don't think it is a problem


i am also not sure about the inner classes within a method, but if it compiles then i don't think it is a problem

I wouldn't be so sure, since there's no telling if the inner class will be nullified outside of the scope of the method.

Also, what happens if the method is called again? Does that then mean that the inner class can be overwritten with new information?

you have a point there, i'll see if i can find anything on it

i found this site that explains that classes are allowed to be defined in a method body, but are not usually used, but are legal.

public interface Listener{

	public void doSomething();
}
public class TestingMethodScope{

	Listener l = null;

	TestingMethodScope tms;

	final java.util.Random rgen = new java.util.Random();

	int i = 0;

	public TestingMethodScope(TestingMethodScope other, Listener l){
		tms = other;
		this.l = l;
	}

	public static void main(String... args){

		TestingMethodScope myTMS = new TestingMethodScope(null, null); // TMS class with null TMS and Listener pointers

		myTMS.initializeListener(); // first time MyLocalClass defined

		myTMS.tms.myCommand(); // polymorphic call to method doSomething in MyLocalClass

		myTMS.initializeListener(); // second time MyLoalClass defined

		myTMS.tms.myCommand(); 

		myTMS.initializeListener();

		myTMS.tms.myCommand();

	}

	public void myCommand(){
		l.doSomething();
	}

	public void initializeListener(){
		class MyLocalClass implements Listener{
			public void doSomething(){
				System.out.println("Whoo! " + rgen.nextInt(50));
				System.out.println(this.toString());
			}
		}
		
		TestingMethodScope ams = new TestingMethodScope(null, new MyLocalClass()); // creating a new TMS class with a MyLocalClass Listener
		this.tms = ams; // tms points to ams
	}
}

It works, but as mentioned before each time the method is called the class is different.

Since the OP is using it once per case I suppose it's fine.

Then the major issue would be the paint call, and possibly others.

I'd like to apologize about the incorrect statement above.

When the method is called again, the pointer with the old information is not overwritten and, I suppose is "safeguarded" from the redeclaring and redefinition of the local class.

This can be verified with this test--

public class TestingMethodScope{

	// bad example

	Listener l = null;

	TestingMethodScope tms;

	final java.util.Random rgen = new java.util.Random();

	int i = 0;

	public TestingMethodScope(TestingMethodScope other, Listener l){
		tms = other;
		this.l = l;
	}

	public static void main(String... args){

		TestingMethodScope myTMS = new TestingMethodScope(null, null); // TMS class with null TMS and Listener pointers

		myTMS.initializeListener(); // first time MyLocalClass defined

		myTMS.tms.myCommand(); 

		myTMS.initializeListener(); // second, i is odd so class is redefine but no new memory is allocated

		myTMS.tms.myCommand(); // checking the address / random value of the redefined class

		myTMS.initializeListener(); // third, i is even so class is redefined and new memory is allocated

		myTMS.tms.myCommand();

	}

	public void myCommand(){
		l.doSomething();
	}

	public void initializeListener(){
		class MyLocalClass implements Listener{ // once method called, MyLocalClass is redeclared/redefined

			int x = rgen.nextInt(9999);

			public void doSomething(){
				System.out.println("Whoo! " + x); // generates a random number each time
				System.out.println(this.toString());
			}
		}

		if(i%2 == 0){ // when i is an even number...
			TestingMethodScope ams = new TestingMethodScope(null, new MyLocalClass()); // creating a new TMS class with a MyLocalClass Listener
			this.tms = ams; // tms points to ams
		}

		i++; // increments each method call
	}
}

huh, i see that you researched this a lot more than i did, it is interesting to know, but unless i really needed to know i wouldn't have looked for it myself.

has any of this info solved your problem LevelSix, because i still think it is simply that you haven't overridden paint(Graphics) ?

I've started to tinker with the paint method. No results really. I've been able to get initial drawings, but not update them with the buttons yet.

add repaint(); to your action listener.

and post your rewritten code please, we cannot see what changes you made unless you show us

waaoo nice one.. thanks for it..[:)]

nice what?

I didn't post any updated code because I haven't yet made any significant changes. I want to remove the Graphic interface from the actionlistener embedded classes, so I can remove the draw method. I think if I'm able to put everything inside the actionPerformed method, then it will yield some results. I'm having a problem though, as those methods have no Graphical connections to allow me to set the color. Would an instance field of Graphics g2, allow me to use the method g2.setColor within those methods?

yes setColor() works with any Graphics object, including subclasses ie Graphics2D

and you can put all of your drawing in paint, use actionlistener to set a flag, in paint switch the flag to figure out what to draw

if i am misunderstanding your meaning then please explain more

I re-worked my code, and found a different approach to fixing this problem. The objects now need a Color as one of their parameters, so a color button is first clicked, then the shape.

For my save button, Iwrite the 3 objects to a text file, using toString() before writing.

Is tehre a way I can then read these parameters back in and use the information to re-create the objects? ( for the load button)

I re-worked my code, and found a different approach to fixing this problem. The objects now need a Color as one of their parameters, so a color button is first clicked, then the shape.

For my save button, Iwrite the 3 objects to a text file, using toString() before writing.

Is tehre a way I can then read these parameters back in and use the information to re-create the objects? ( for the load button)

Can you post the modified code please?

If your objects are Serializable (their state can be stored/saved) then I don't see why they couldn't be retrieved the same way you stored them.

You can serialize the entire object to file in it's current state by implementing the serializable interface. Here is a technical doc on that: http://java.sun.com/developer/technicalArticles/Programming/serialization/

To further add onto that, any objects without a default Constructor cannot be Serialized. At least from what I remember about Serialization.

If all else fails and you cannot store the information for the objects directly, you can bypass this (somewhat) in a fairly annoying way by saving the accessible data of each object to a file then sending that data back to a new object of the same type and applying the data to the objects.

That's the long way of doing it, and definitely unrecommended. But if there's no other way of doing it then that would be your second-best bet.

I modified each constructor class, and the main class. here they are:

import java.awt.geom.Line2D;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
/**
 * Course Number: Comp 1123.
 * Assignment Number: 5
 * Instructor: Rick Giles
 * Question Number: 1
 * The class creates Line objects.
 * @author Thomas Symonds 
 * @version July 21, 08
 */
public class Line implements Graphic
{
   public Line(int a, int b, int aSize, Color aColor)
   {
       c = a;
       d = b;
       size = aSize;
       color = aColor;
   }
   public void draw(Graphics2D g2)
   {
       
       Line2D.Double line1 = new Line2D.Double(c, d, c + size, d + size);
       Line2D.Double line2 = new Line2D.Double(c + 35 , d + 15,  c + 50, d);
       g2.setColor(color);
       g2.draw(line1);
       g2.draw(line2);
   
   }
   private int c;
   private int d;
   private int size;
   private Color color;
  
}
import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
/**
 * Course Number: Comp 1123.
 * Assignment Number: 5
 * Instructor: Rick Giles
 * Question Number: 1
 * The class creates rectangle objects
 * @author Thomas Symonds 
 * @version July 21, 08
 */
public class RectangleM implements Graphic
{
   public RectangleM(int a, int b, int aSize, Color aColor)
   {
       c = a;
       d = b;
       size = aSize;
       color = aColor;
    }
   public void draw(Graphics2D g2)
   {
       
       Rectangle box = new Rectangle(c, d, size + 50, size);
       g2.setColor(color);
       g2.draw(box);
   }
    private int c;
    private int d;
    private Color color;
    private int size;
}
import java.awt.geom.Ellipse2D;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
/**
 * Course Number: Comp 1123.
 * Assignment Number: 5
 * Instructor: Rick Giles
 * Question Number: 1
 * this class creates Ellipse objects
 * @author Thomas Symonds 
 * @version July 21, 08
 */
public class Ellipse implements Graphic
{
   public Ellipse(int a, int b, int aSize, Color aColor)
   {
       c = a;
       d = b;
       size = aSize;
       color = aColor;
    }
       
   public void draw(Graphics2D g2)
   {
       
       Ellipse2D.Double circle = new Ellipse2D.Double(c, d, size, size);
       g2.setColor(color);
       g2.draw(circle);
    }
    private int c;
    private int d;
    private int size;
    private Color color;
}
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;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileNotFoundException;

/**
 * Course Number: Comp 1123.
 * Assignment Number: 
 * Instructor: Rick Giles
 * Question Number: 
 * 
 * @author Thomas Symonds 
 * @version July 21, 08
 */
public class GraphicsEditor extends JFrame
{
   public GraphicsEditor()
   {

       circle = new Ellipse(0, 0, 0, Color.WHITE);
       eyes = new Line(0, 0, 0, Color.WHITE);
       box = new RectangleM(0, 0, 0, Color.WHITE);
       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, 0, Color.WHITE);
                eyes = new Line(0, 0, 0, Color.WHITE);
                box = new RectangleM(0, 0, 0, Color.WHITE);
                repaint();
           }
       }
       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(120, 170, 10, color);
               repaint();
               
           }
       }
       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(120, 115, 15, color);
               repaint();
               
           }
       }
       ActionListener listener3 = new lineListener();
       line.addActionListener(listener3);
    }
    public void createCircle()
    {
        ellipse = new JButton("Ellipse");
        class circleListener implements ActionListener
        {
           public void actionPerformed(ActionEvent event)
           {
              circle = new Ellipse(100, 100, 100, color);
              repaint();
               
               
           }
                      
           
       }
       ActionListener listener4 = new circleListener();
       ellipse.addActionListener(listener4);
    }
    public void createRed()
    {
        red = new JButton("Red");
        class redListener implements ActionListener
        {
           public void actionPerformed(ActionEvent event) 
           {
               color = Color.RED;
           }
               
       }
       ActionListener listener5 = new redListener();
       red.addActionListener(listener5);
    }
    public void createYellow()
    {
        yellow = new JButton("Yellow");
        class yellowListener implements ActionListener
        {
           public void actionPerformed(ActionEvent event) 
           {
               color = Color.YELLOW;
           }
               
       }
       ActionListener listener6 = new yellowListener();
       yellow.addActionListener(listener6);
    }
    public void createBlue()
    {
        blue = new JButton("Blue");
        class blueListener implements ActionListener 
        {
           public void actionPerformed(ActionEvent event) 
           {
              color = Color.BLUE; 
            }    
       }
       ActionListener listener7 = new blueListener();
       blue.addActionListener(listener7);
    }
     public void createGreen()
    {
        green = new JButton("Green");
        class circleListener implements ActionListener
        {
           public void actionPerformed(ActionEvent event)
           {
              color = Color.GREEN;
            }
        }
         
           
               
       
       ActionListener listener8 = new circleListener();
       green.addActionListener(listener8);
    }
    public void createSave()
   {
       save = new JButton("Save");
       
       
       class saveListener implements ActionListener
       {
           public void actionPerformed(ActionEvent event)
           {
               try
               {
               int n = 1;
               String saveFile = "File" + n + ".txt";
               PrintWriter writer = new PrintWriter(saveFile);
               writer.println(eyes.toString());
               writer.println(line.toString());
               writer.println(box.toString());
               writer.close();
               n++;
            }
               
               catch (IOException exception)
               {
                   System.out.print("Bad file name");
                }
           }
       }
       ActionListener listener9 = new saveListener();
       save.addActionListener(listener9);
       
   }
   public void createLoad()
   {
       load = new JButton("Load");
       
       
       class loadListener implements ActionListener
       {
           public void actionPerformed(ActionEvent event)
           {
               try
               {
               Scanner in = new Scanner(System.in);
               System.out.print("Enter input file: ");
               String enter = in.nextLine();
               FileReader reader = new FileReader(enter);
               Scanner console = new Scanner(reader);
               //unfinished
            }
               catch(FileNotFoundException exception)
               {
                   System.out.print("File not found");
                }
           }
       }
       ActionListener listener10 = new loadListener();
       load.addActionListener(listener10);
       
   }
    public void paint(Graphics g)
   {
       super.paint(g);
       Graphics2D g2 = (Graphics2D)g;
       circle.draw(g2);
       eyes.draw(g2);
       box.draw(g2);

   }
       
   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 Ellipse circle;
   private static Line eyes;
   private static RectangleM box;

   private Color color;
}

My Professor instructed me to do the saving and loading as follows:

In order to load and save the shapes, choose appropriate text data for each shape and read and write the data for the shapes using a text file.

You can use a simple comma-separated value format for that then. Record the type of the object first, then the values that you need to re-create.

This is what is printed to the text document. After I fixed the names of the objects so i was printing the right ones.

Ellipse@3afb99
Line@5c3987
RectangleM@c4d04d

You will need to override the toString() method or provide some other method like toFileString() that returns the line of data that you need to record.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.