0

Here is my code. I have 4 classes, an abstract class (GeoShape), a Rectangle class that extends GeoShape, a GraphicsPanel containing my GeoShapes and trying to draw them, and a Launcher class.

GeoShape.java

public abstract class GeoShape
{
 protected Point p;
 protected int width, height;
 public GeoShape(Point p, int width, int height)
 {
 this.p = p;
 this.width = width;
 this.height = height;
 }
 public void drawItself(Graphics g)
 {
 g.setColor(Color.BLACK);
 }
 // Getters and setters...
} 

Rectangle.java

public class Rectangle extends GeoShape
{
 public Rectangle(Point p, int width, int height)
 {
 super(p, width, height); 
 }
 @Override
 public void drawItself(Graphics g)
 {
 super.drawItself(g);
 g.drawRect((int)p.getX(), (int)p.getY(), width, height);
 }
}

GraphicsPanel.java

public class GraphicsPanel extends JPanel
{
 private static final long serialVersionUID = 1L;
 private ArrayList<GeoShape> list;
 public GraphicsPanel() 
 {
 list = new ArrayList<GeoShape>();
 }
 @Override
 public void paintComponents(Graphics g) 
 {
 super.paintComponents(g);
 for(int i = 0 ; i < list.size() ; i++) list.get(i).drawItself(g);
 }
 public void addShapeInList(GeoShape s)
 {
 list.add(s);
 }
}

Launcher.java

public class Launcher extends JFrame
{
 private static final long serialVersionUID = 1L;
 private GraphicsPanel panel;
 public Launcher()
 {
 panel = new GraphicsPanel();
 panel.addShapeInList(new Rectangle(new Point(3,9),120,20));
 panel.repaint();
 this.setTitle("Test");
 this.setContentPane(panel);
 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 this.setSize(700, 500);
 this.setVisible(true);
 }
 public static void main(String[] args) 
 {
 new Launcher();
 }
}

And nothing happens in the frame... thanks for your help.

Andrew Thompson
169k42 gold badges224 silver badges441 bronze badges
asked Jan 23, 2013 at 10:58
2
  • 4
    Change public void paintComponents(Graphics g) to public void paintComponent(Graphics g) (no s on the method name). The form with plural spelling is not something we should generally override. Commented Jan 23, 2013 at 10:59
  • 2
    Try paintComponent(Graphics g) instead of paintComponents. Commented Jan 23, 2013 at 11:00

1 Answer 1

2

Try to replace it with paintComponent rather than paintComponents

public void paintComponent(Graphics g) 
{
 super.paintComponent(g);
 for(int i = 0 ; i < list.size() ; i++) list.get(i).drawItself(g);
}
answered Jan 23, 2013 at 11:02
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.