2012-10-07 82 views
1

我正在嘗試使用布爾標誌來繪製基於按鈕單擊的圖形。之前,我在ShapeGUI類中將布爾值設置爲公共靜態布爾類型,並使用Shape.draw在ShapeListener中將其調用,並且工作正常,但花費大約10秒鐘的時間才能繪製圖形。Java Swing重繪速度緩慢或無法正常工作

現在我在ShapeListener類本身內部設置了布爾標誌。現在它不是重新粉刷就是很慢。點擊按鈕後,如何使圖形立即顯示?

public class ShapeGUI extends JFrame 
{ 
    public static final int WIDTH = 500; 
    public static final int HEIGHT = 500; 

    public ShapeGUI() 
    { 
     setSize(WIDTH, HEIGHT); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //setLayout(new FlowLayout()); 
     setLocationRelativeTo(null); 
     JMenuBar menu = new JMenuBar(); 
     JMenu file = new JMenu("File"); 
     menu.add(file); 
     JMenuItem save = new JMenuItem("Save Information"); 
     JMenuItem exit = new JMenuItem("Exit"); 
     file.add(save); 
     file.add(exit);    
     setJMenuBar(menu); 

     ShapeListener test = new ShapeListener();   
     JButton button = new JButton("Hello"); 
     test.setBackground(Color.CYAN); 
     button.addActionListener(new ShapeListener()); 
     test.add(button); 
     add(test); 

//followed this person's advice   
//First off, your drawing should be done in a paintComponent method override that is held in a class that extends JPanel. Next, this class could have a boolean variable that the paintComponent uses to decide if it will draw a rectangle or not. The button press simply sets the boolean variable and calls repaint on the drawing JPanel. 
    } 
} 

    public class ShapeListener extends JPanel implements ActionListener 

    { 

    boolean draw = false; 
    Shape s = new Circle(); 
    Shape a = new Rectangle(); 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     draw = true; 
     repaint(); 
    } 
    @Override 
    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 

     if(draw) 
     { 
     s.draw(g); 
     } 
     else 
     { 
      a.draw(g); 
     } 

    } 

} 
+0

? 'Shape'沒有'draw'方法? – MadProgrammer

回答

2

你的邏輯是有點過......

ShapeListener test = new ShapeListener(); // First instance 
JButton button = new JButton("Hello"); 
test.setBackground(Color.CYAN); 
button.addActionListener(new ShapeListener()); // Second instance... 
test.add(button); 
add(test); 

您創建ShapeListener類的兩個實例,添加一個容器,並使用其他的動作偵聽器。兩者都不會相互瞭解,這意味着你可以點擊回家的牛,ShapeListener的第一個實例將永遠不會改變。

而且,除非你打算重寫ShapeListener窗格的preferredSize方法,我會使用的東西就像一個BorderLayout在框架上,以確保面板佈局合理,否則會看起來像面板不被畫...

嘗試類似...

public class ShapeGUI extends JFrame { 

    public static final int WIDTH = 500; 
    public static final int HEIGHT = 500; 

    public ShapeGUI() { 
     setSize(WIDTH, HEIGHT); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 
     JMenuBar menu = new JMenuBar(); 
     JMenu file = new JMenu("File"); 
     menu.add(file); 
     JMenuItem save = new JMenuItem("Save Information"); 
     JMenuItem exit = new JMenuItem("Exit"); 
     file.add(save); 
     file.add(exit); 
     setJMenuBar(menu); 

     setLayout(new BorderLayout()); 

     ShapeListener test = new ShapeListener(); 
     JButton button = new JButton("Hello"); 
     test.setBackground(Color.CYAN); 
     button.addActionListener(test); 
     test.add(button); 
     add(test); 

     setVisible(true); 

    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       new ShapeGUI(); 

      } 
     }); 
    } 

    public class ShapeListener extends JPanel implements ActionListener { 

     Shape s = new Ellipse2D.Float(0, 0, 20, 20); 
     Shape a = new Rectangle2D.Float(0, 0, 20, 20); 

     private Shape paintMe = a; 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      paintMe = s; 
      repaint(); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 

      int x = (getWidth() - paintMe.getBounds().width)/2; 
      int y = (getHeight() - paintMe.getBounds().height)/2; 

      Graphics2D g2d = (Graphics2D) g.create(); 
      g2d.translate(x, y); 
      g2d.draw(paintMe); 
      g2d.dispose(); 

     } 
    } 
} 

,而不是...

+0

哦哈哈。感謝您發現我的錯誤,將其更改爲button.addActionListener(test); 現在它立即更新 – Dog