2012-09-02 50 views
4

嗨我試圖在遊戲循環中每次調用paint方法。此時屏幕彈出一個標籤和一個按鈕,一旦按鈕已經按下標籤和按鈕去我想要的,但我無法得到油漆方法開始我試圖j.repaint()& j.validate()既不訪問繪畫method.Any幫助將不勝感激。在按鈕開始時從遊戲循環調用paint方法

package sgame; 

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class SGame extends JPanel 
{ 

    public static void main(String[] args) 
    { 

     final JFrame window = new JFrame("hello"); 
     final JPanel window1 = new JPanel(); 
     Timer loop = new Timer(1000, new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       GameLoop(window1); 

      } 
     }); 
     Window(window, loop); 

    } 

    public static void Window(final JFrame window, final Timer loop) 
    { 
     final JButton b1 = new JButton("GO!"); 
     b1.setLocation(210, 300); 
     b1.setSize(70, 50); 
     window.setVisible(true); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setSize(500, 500); 
     window.setLocation(420, 170); 
     window.setBackground(Color.BLACK); 
     final JLabel Title = new JLabel("Snake", JLabel.CENTER); 
     Title.setFont(new Font("Times New Roman", Font.ITALIC, 60)); 
     Title.setVerticalAlignment(JLabel.CENTER); 
     Title.setForeground(Color.WHITE); 
     window.add(b1); 
     window.add(Title); 
     b1.addActionListener(new ActionListener() // runs when buttons pressed 
      { 

       @Override 
       public void actionPerformed(ActionEvent e) // clears header,button and starts timer 
       { 
        b1.invalidate(); 
        b1.setVisible(false); 
        Title.setVisible(false); 
        Title.invalidate(); 
        loop.start(); 
       } 

      }); 

    } 

    public static void GameLoop(JPanel j) 
    { 
     // Call paint method 
    } 

    public void paintComponent(Graphics g) 
    { 
     g.setColor(Color.yellow); 
     g.drawRect(30, 30, 30, 30); 
    } 
} 
+3

你應該幾乎從不直接調用paint方法,肯​​定不會出現這種情況。你的代碼對靜態變量和方法有點沉重,這表明重新設計是有序的。另外,你在哪裏創建一個SGame實例?如果沒有SGame的實例被創建並顯示在頂層窗口中,paintComponent將沒有意義。你有沒有經歷教程?首先考慮一些教程,包括Swing教程。 –

+0

請學習java命名約定並堅持使用它們。 – kleopatra

回答

1

paintComponent()方法SGame永遠不會被調用,因爲你沒有任何實例化對象SGame。並且window1尚未添加到框架中。

像這樣的東西應該會更好:

public class SGame extends JPanel { 

    private Timer loop; 

    public SGame(){ 
     loop = new Timer(1000, new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       gameLoop(); 
      } 
     }); 
    } 

    public static void main(String[] args) { 
     final JFrame window = new JFrame("hello"); 
     final JButton b1 = new JButton("GO!"); 
     b1.setLocation(210, 300); 
     b1.setSize(70, 50); 
     window.setVisible(true); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setSize(500, 500); 
     window.setLocation(420, 170); 
     window.setBackground(Color.BLACK); 
     final JLabel Title = new JLabel("Snake", JLabel.CENTER); 
     Title.setFont(new Font("Times New Roman", Font.ITALIC, 60)); 
     Title.setVerticalAlignment(JLabel.CENTER); 
     Title.setForeground(Color.WHITE); 
     window.add(b1); 
     window.add(Title); 
     b1.addActionListener(new ActionListener() { // runs when buttons pressed 
      @Override 
      public void actionPerformed(ActionEvent e) // clears header,button 
      { 
       b1.invalidate(); 
       b1.setVisible(false); 
       Title.setVisible(false); 
       Title.invalidate(); 
       SGame sg = new SGame(); 
       window.add(sg); 
       sg.start(); 
      } 
     });; 
    } 

    public void start(){ 
     loop.start(); 
    } 

    public void gameLoop() { 
     repaint(); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     g.setColor(Color.BLACK); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
     g.setColor(Color.yellow); 
     g.drawRect(30, 30, 30, 30); 
    } 
}