0

forgive me if there is already a thread on here dealing with this but I can't seem to find one that deals with my specific task.

I'm new to Java having only ever coded in BASIC and Z80 Assembly at various points in my life. I'm slowly getting the hang of it but I'm struggling with a task I'm doing as part of a course I'm studying.

I need to create some basic Graphics, a circle, a square and so on, then I need to store them as objects in a linked list. I have managed to easily draw the objects and store them in a linked list but I can't figure out how to create a method that will then take whats stored in the list and paint them inside a JFrame.

Here is my code: public class Shape extends JFrame {

public void paint(Graphics g){
LinkedList<Object> ll = new LinkedList<Object>();
setSize(800,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);

Circle c = new Circle();
c.setHeight(100);
c.setWidth(100);
c.setXpos(100);
c.setYpos(250);

ll.add(c);

Square s = new Square();
s.setHeight(100);
s.setWidth(100);
s.setXpos(300);
s.setYpos(250);

ll.add(s);

Bar b = new Bar();
b.setHeight(100);
b.setWidth(200);
b.setXpos(500);
b.setYpos(250);

ll.add(b);

}

and an example of the code in the Circle Class (which is pretty much the same for the square etc..)

public class Circle {
private int xpos;
private int ypos;
private int width;
private int height;




public void paint(Graphics g) {

   g.setColor(Color.RED);
   g.fillOval(xpos, ypos, width, height);
    }

public void setXpos(int xpos) {
    this.xpos = xpos;
}

public void setYpos(int ypos) {
    this.ypos = ypos;
}

public void setWidth(int width) {
    this.width = width;
}

public void setHeight(int height) {
    this.height = height;
}

Basically I have a total mental block on how to get what's contained in the LinkedList to the JFrame.

Any help is greatly appreciated!

  • `paint` is not the place to change the state of the frame or component or construct various other elements of what you to do, instead, these should be setup before `paint` is called and `paint` should simply render this state. You should also avoid overriding `paint` and instead, create a custom class the extends from something like `JPanel` and override it's `paintComponent` method. Take a look at [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details – MadProgrammer Dec 04 '13 at 00:17
  • [This example](http://stackoverflow.com/questions/12683533/drawing-a-rectangle-that-wont-disappear-in-next-paint/12683601#12683601) is similar in concept, in that it has a `List` of `Shape`s to paint and it paints them from the `List` (the list is updated separately) – MadProgrammer Dec 04 '13 at 00:28

1 Answers1

0

Your paint method is not doing what it should do: draw your objects on the Graphics instance. Instead, you are just adding objects to a list. Most probably, these objects should be included in the list at some other time, perhaps even on the constructor. In addition, if you intend the Circle, Bar, etc objects in a generic way, you should make their classes inherit from some general interface. For example:

   interface Painter {
      public void paint(Graphics g);
   }

And then:

public class Shape extends Painter {...}

In this case, you could have a class member of type List with the content that you are now adding to the local variable ll.

Then, your paint method could be something along the lines of:

public void paint(Graphics g){
   super.paint(g);

   for (Painter p : painters) {
       p.paint(g);
   }
};

Hope this helps.

coderazzi
  • 430
  • 6
  • 8
  • Thanks coderazzi! Your reply has definitely given me some more insight! Ultimately I will have to deal with inheritance when dealing with the task I've been set so your post has definitely helped me there! For the time being I was mainly concerned with getting the "shapes" back from the list then I had a bit of a brainwave and added this code... Circle retrieveC; retrieveC = (Circle) ll.get(0); retrieveC.paint(g); If anyone has anymore tips that will be great! It all helps me learn! – user3063717 Dec 04 '13 at 00:45