2014-03-29 38 views
0

我有一個使用繪製方法將車輛對象(例如車形)繪製到JFrame上的程序。然而,無論何時我點擊屏幕繪製車輛,我必須刷新窗口才能顯示,即使添加了repaint()方法在最小化/重新打開窗口後Java JFrame重新繪製


第一張圖片顯示我點擊的位置。什麼都沒發生。

enter image description here

最小化和打開窗口之後。

enter image description here

import java.awt.Graphics; 
    import java.awt.Graphics2D; 
    import java.awt.event.MouseEvent; 
    import java.awt.event.MouseListener; 
    import java.util.Iterator; 
    import java.util.LinkedList; 
    import javax.swing.JFrame; 


    /** Program specs: Draws shapes (Vehicles) that are contained within LinkedLists. 
    * When a shape is drawn over another shape it 'joins' its list. (not implemented yet, not my issue) 

    */ 

    /** 
     Creates JFrame 
    */ 
    public class FramePanel extends JFrame implements MouseListener 
    { 
     private static final int FRAME_WIDTH = 600; 
     private static final int FRAME_HEIGHT = 600; 

     Car car; // Car is a subclass of Vehicle. It only contains a draw method. 
     //Vehicle is an abstract class that only contains a draw method 

     LinkedList<LinkedList<Vehicle>> list = new LinkedList<LinkedList<Vehicle>>(); 
     LinkedList<Vehicle> temp = new LinkedList <Vehicle>(); 

     /** 
      Constructs the frame. 
     */ 
     public FramePanel() 
     { 
      addMouseListener(this); 
      setSize(FRAME_WIDTH, FRAME_HEIGHT); 

      repaint(); 
     } 


    @Override 
    public void mouseClicked(MouseEvent evt) { 

    car = new Car (evt.getX(), evt.getY()); 
    temp.add(car); //Add Vehicle to LinkedList 
    list.add(temp); //Add LinkedList to Collection of LinkedLists 

    } 

    public void mouseEntered(MouseEvent arg0) {} 
    public void mouseExited(MouseEvent arg0) {} 
    public void mousePressed(MouseEvent arg0) {} 
    public void mouseReleased(MouseEvent arg0) {} 


    public void paint(Graphics g) { 
     super.paintComponents(g) ; 
     Graphics2D g2 = (Graphics2D) g; 

     for (LinkedList<Vehicle> veh : list){ // list is collection of Linked Lists 

     Iterator<Vehicle> it = veh.iterator(); 
      while (it.hasNext()){ 
       it.next().draw(g2); 
      } 
     } 
    } 

    public static void main(String[] args) 
    { 
     JFrame frame = new FramePanel(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setTitle("Viewer"); 
     frame.setVisible(true);  
    } 

    } 

注:如果我添加重繪()語句到我的油漆(圖形G)方法該車將借鑑但將不希望閃爍/不重繪()在構造函數中。我不想要這個。

+0

您不會在mouseClicked中觸發重繪。這是故意的嗎? –

+0

你需要實現雙緩衝http://stackoverflow.com/questions/5924697/java-double-buffering。 – BitNinja

+1

@codeNinja這有**沒有任何**與雙緩衝... ... – Marco13

回答

3

mouseClicked方法的最後一行中添加對repaint()的調用將已解決此問題。

不過,也有其他幾個要點:

  • 不要延長JFrame(尤其是,不重寫的JFramepaint方法)。相反,使用JPanel進行繪畫,並覆蓋其paintComponent方法。
  • 不要讓一個頂級類實現MouseListener接口
  • 不要聲明你的清單爲LinkedList,但只是作爲ListWhat does it mean to "program to an interface"?
  • 的GUI應該在事件派發線程創建
  • 字段應該是私人(和最終,如果可能的話)

所以有些清理:

import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.util.LinkedList; 
import java.util.List; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class FramePanel extends JPanel 
{ 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       createAndShowGUI(); 
      } 
     }); 
    } 

    private static void createAndShowGUI() 
    { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     FramePanel framePanel = new FramePanel(); 
     f.getContentPane().add(framePanel); 

     f.setSize(600,600); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    private final List<List<Vehicle>> list = new LinkedList<List<Vehicle>>(); 

    /** 
     Constructs the panel. 
    */ 
    public FramePanel() 
    { 
     addMouseListener(new MouseAdapter() 
     { 
      @Override 
      public void mouseClicked(MouseEvent e) 
      { 
       addCarAt(e.getX(), e.getY()); 
      } 
     }); 
    } 

    private void addCarAt(int x, int y) 
    { 
     Car car = new Car(x, y); 
     List<Vehicle> temp = new LinkedList<Vehicle>(); 
     temp.add(car); //Add Vehicle to LinkedList 
     list.add(temp); //Add LinkedList to Collection of LinkedLists 
     repaint(); 
    } 

    @Override 
    protected void paintComponent(Graphics gr) 
    { 
     super.paintComponent(gr) ; 
     Graphics2D g = (Graphics2D)gr; 

     for (List<Vehicle> veh : list) // list is collection of Linked Lists 
     { 
      for (Vehicle v : veh) 
      { 
       v.draw(g); 
      } 
     } 
    } 
} 
+0

感謝您不僅告訴我我的錯誤,還改善我的代碼! – BDillan