简体   繁体   中英

How to Retrieve a Graphics Object From a Linked List in Java and Display it in a JFrame

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM