2015-12-26 23 views
1

PaintComponent用於繪製數字。只是沒有發生,乾淨的Jframe出現。 我覺得有什麼不妥列表或我所謂的方法 名單是在課堂上與塗料成分Object.draw本身在PaintComponent中不起作用

public class Paint extends JPanel implements ActionListener { 
    List<Figures> figuresList = new ArrayList<Figures>(); 
    Timer t = new Timer(5, this); 

public void paintComponent(Graphics g) { 

    super.paintComponent(g); 
    for (Figures figure : figuresList) { 
     figure.drawItself(g, figure.getLocationX(), figure.getLocationY()); 
    } 
    t.start(); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 

    { 
     for (Figures figure : figuresList) { 
      if (figure.getLocationX() < 0 || figure.getLocationX() > 540) { 
       figure.setVelocityX(-figure.getVelocityX()); 
      } 
      if (figure.getLocationY() < 0 || figure.getLocationX() > 220) { 
       figure.setVelocityY(-figure.getVelocityY()); 
      } 
      figure.setLocationX(figure.getLocationX() 
        + figure.getVelocityX()); 
      figure.setLocationY(figure.getLocationY() 
        + figure.getVelocityY()); 
     } 
    } 
    repaint(); 

} 

而且drawitself方式:

public class Circle implements Figures {  
    public int locationX = 12; 
    public int locationY = 12; 
    public int velocityX =1; 
    public int velocityY =1; 


    public void drawItself(Graphics g, int locationX, int locationY){ 
     this.locationX = locationX; 
     this.locationY = locationY; 
     g.drawOval(locationX, locationY, 40, 40); 
     g.fillOval(locationX, locationY, 40, 40); 
    } 

主營:

public static void main(String[] args) { 

    Circle c = new Circle(); 
    Quadrat q = new Quadrat(); 
    Paint p = new Paint(); 
    p.figuresList.add(c); 
    p.figuresList.add(q); 
    GUI.Configuration(); 


    } 

GUI

public class GUI { 


    public static void Configuration(){ 
     JFrame frame = new JFrame("Figures Animation"); 
     frame.setSize(600,300); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel panel = new Paint(); 
     frame.getContentPane().add(BorderLayout.CENTER, panel); 
    } 
+0

你是什麼意思的「不工作」?請稍微解釋一下您的預期和發生的情況。 –

+0

我期望表格數字出現在JFrame上,但沒有任何事情發生。沒有錯誤只是乾淨的JFrame。 – Ivan

+0

當我編寫這個方法作爲一個構造函數,並寫在PaintComponent方法「新的Circle(g,0,0)」內時,一切都很好,但我需要ActionPerformed方法的數字列表,我需要調用get方法。 – Ivan

回答

2

創建並在此處添加油漆實例:

public class GUI { 
    public static void Configuration(){ 
     JFrame frame = new JFrame("Figures Animation"); 
     frame.setSize(600,300); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel panel = new Paint(); // *** new Paint is here, but nothing is added 
     frame.getContentPane().add(BorderLayout.CENTER, panel); 
    } 

但沒有使用已經被添加到它。所有重要的東西都添加到一個完全不同的Paint JPanel中,一個永遠不會顯示:

public static void main(String[] args) { 
    Circle c = new Circle(); 
    Quadrat q = new Quadrat(); 
    Paint p = new Paint(); // **** ANOTHER new Paint is here, and it gets goodies 
    p.figuresList.add(c); 
    p.figuresList.add(q); 

    // but is never added to a JFrame and is never displayed. 

    GUI.Configuration(); 
} 

不要這樣做。創建一個繪製JPanel,僅向其中添加重要組件,然後僅將其添加到JFrame。最重要的是,不要只是輸入代碼,在將代碼提交給代碼之前先考慮並計劃你的程序,並且不會看到類似的錯誤。

另外,同樣,不要從paintComponent中啓動Timer,也不要在那裏創建Circle。您可以在PaintComponent中繪製Circle實例,但可以在Paint構造函數中創建它並啓動Timer。