2013-12-17 30 views
0

我這樣做是爲了生成一個棋盤,並且每隔10秒翻轉10次瓦片(開關顏色)。但是,圖形要麼不加載,要麼不更新。圖形緩慢或沒有均勻加載

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class ShapeTest extends JPanel implements ActionListener { 
private static int FRAME_HEIGHT = 500; 
private static int FRAME_WIDTH = 500; 
private static JFrame frame = new JFrame(); 

Color color1 = Color.BLACK; 
Color color2 = Color.WHITE; 
javax.swing.Timer timer = null; 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      setAttributes(); 
     } 
    }); 
} 

public static void setAttributes() { 
    frame.add(new ShapeTest()); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setTitle("FrameTest"); 
    frame.pack(); 
    frame.setVisible(true); 
} 

/* Constructor */ 
public ShapeTest() { 
    timer = new Timer(5000, new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if (color1 == Color.BLACK) { 
       color1 = Color.WHITE; 
       color2 = Color.BLACK; 
       repaint(); 
      } else { 
       color1 = Color.BLACK; 
       color2 = Color.WHITE; 
       repaint(); 
      } 
     } 
    }); 
    timer.start(); 
} 

protected void painComponent(Graphics g) { 
    super.paintComponent(g); 
    for (int x = 0; x < FRAME_WIDTH; x = x + 10) { 
     for (int y = 0; y < FRAME_HEIGHT; y = y + 10) { 
      if (x % 20 == 0) { 
       g.setColor(color2); 
       g.fillRect(x, y, 10, 10); 
       y = y + 10; 
       g.setColor(color1); 
       g.fillRect(x, y, 10, 10); 
      } else { 
       g.setColor(color1); 
       g.fillRect(x, y, 10, 10); 
       y = y + 10; 
       g.setColor(color2); 
       g.fillRect(x, y, 10, 10); 
      } 
     } 
    } 
} 

public Dimension getPreferredSize() { 
    return new Dimension(FRAME_WIDTH, FRAME_HEIGHT); 
} 

@Override 
public void actionPerformed(ActionEvent arg0) { 

} 

} 

這是根據下面的答案修改後的代碼,但是,它似乎仍然沒有運行。

+0

對不起,我拼寫'paintComponent'錯誤。我有'painComponent'。做出修復,並且工作正常。我測試了它。 –

回答

1

兩件事。

首先,你應該從你JComponent#paintComponent被調用super.paintComponent,以確保你沒有得到任何的油漆文物,特別是因爲JComponent是透明

其次,你應該首先將你的組件和顯示你幀。 ..

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      addComponents(); 
      setAttributes(); 
     } 
    }); 
} 

如果你需要它的可見後新的組件添加到您的框架,您可能需要調用revalidaterepaint,以確保內容正確更新...

2

不要使用線程休眠Thread.sleep()在Swing程序中。使用Swing Timer.並全局定義Color對象,因此可以通過定時器和paintComponent訪問它。然後在計時器重繪嘗試此:

import java.awt.*; 
import javax.swing.*; 

public class ShapeTest extends JPanel { 
    private static int FRAME_HEIGHT = 500; 
    private static int FRAME_WIDTH = 500; 
    private static JFrame frame = new JFrame(); 

    Color color1 = Color.BLACK;      <-- Color objects 
    Color color2 = Color.WHITE; 
    javax.swing.Timer timer = null;     <-- Declare Timer object 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run(){ 
       setAttributes(); 
      } 
     }); 
    } 
    public static void setAttributes() { 
     frame.add(new ShapeTest());     <-- Add the panel 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setTitle("FrameTest"); 
     frame.pack();     <--- Pack. 
     frame.setVisible(true); 
    } 

    /* Constructor */ 
    public ShapeTest(){ 
     timer = new Timer(5000, new ActionListener(){ <-- "sleeps" 5 seconds (real word is "delay") 
      public void actionPerformed(ActionEvent e){ 
       if (color1 == Color.BLACK){ 
        color1 = Color.WHITE;    <-- Alternate colors 
        color2 = Color.BLACK; 
        reapaint();       <-- repaint 
       } else { 
        color1 = Color.BLACK;    <-- Alternate colors 
        color2 = Color.WHITE; 
        repaint(); 
       } 
      } 
     }); 
     timer.start();       <-- Start timer 
    } 

    protected void paintComponent(Graphics g){ 
     super.paintComponent(g); 

     for (int x = 0; x < FRAME_WIDTH; x = x + 10) { 
      for (int y = 0; y < FRAME_HEIGHT; y = y + 10) { 
       if (x % 20 == 0) { 
        g.setColor(color2);  <-- Just us the color Object 
        g.fillRect(x, y, 10, 10); 
        y = y + 10; 
        g.setColor(color1); 
        g.fillRect(x, y, 10, 10); 
       } else { 
        g.setColor(color1); 
        g.fillRect(x, y, 10, 10); 
        y = y + 10; 
        g.setColor(color2); 
        g.fillRect(x, y, 10, 10); 
       } 
      } 
     } 
    } 

    public Dimension getPreferredSize(){ 
     return new Dimension(FRAME_WIDTH, FRAME_HEIGHT);  <-- preferred size of panel 
    } 


} 
+0

哇,你把這段代碼寫出來了,這不是複製粘貼^^ +1 - im impresse peeeskillet!根據此答案, –

+0

更新了OP。 –

+0

只修復'painComponent()'。它拼錯了。應該是'paintComponent()'。此修復程序後正常工作。經過測試和批准。 :)不錯的圖形順便說一句 –