2013-01-23 5 views
0

여기 내 코드입니다. 4 개의 클래스, 추상 클래스 (GeoShape), GeoShape를 확장하는 Rectangle 클래스, 내 GeoShapes가 포함 된 GraphicsPanel, 그리기를 시도하는 클래스 및 Launcher 클래스가 있습니다.Java : 내 paintComponents() 메소드가 호출되지 않습니다

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(); 
    } 
} 
,745,

프레임에서 아무 일도 일어나지 않습니다 ... 도움을 주셔서 감사합니다.

+4

paintComponents 이상 변경'공공 무효 paintComponents (그래픽 g)''(메소드 이름 없음's')로 교체합니다. 복수 철자가있는 양식은 일반적으로 무시해야하는 것이 아닙니다. –

+2

paintComponents 대신 paintComponent (Graphics g)를 사용하십시오. –

답변

2

봅니다 공공 무효의 paintComponent (그래픽 g)`에 paintComponent 오히려

public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    for(int i = 0 ; i < list.size() ; i++) list.get(i).drawItself(g); 
} 
+0

잘 일 했어, 고마워! – Rob

관련 문제