2013-08-19 83 views
0

我正在使用swing中的2D平臺遊戲,我正在開發遊戲框架。我正在測試reapaint()並繪製函數,但它們不起作用,而且我很無能。這裏是我的代碼:爲什麼我的屏幕上沒有畫任何東西?

Window.java

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

public class Window extends JFrame { 

public Window() { 

    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    this.setSize(1000, 1000); 
    this.setVisible(true); 
    this.setTitle("Infiltrator"); 
    this.setLocationRelativeTo(null); 
    this.setContentPane(new Framework()); 
} 

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

} 





} 

Panel.java

import java.awt.Graphics; 
import java.awt.Graphics2D; 

import javax.swing.JPanel; 

public abstract class Panel extends JPanel { 

public Panel() { 

this.setDoubleBuffered(true); 
this.setFocusable(true); 

/* 
* 
if(false) 
{ 
    BufferedImage blankCursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); 
    Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(blankCursorImg, new Point(0, 0), null); 
    this.setCursor(blankCursor); 
} 
* 
*/ 
} 

public static void Draw(Graphics2D g2d) { 

} 

@Override 
public void paintComponent(Graphics g) 
{ 
Graphics2D g2d = (Graphics2D)g;   
super.paintComponent(g2d);   
Framework.Draw(g2d);  
} 

} 

Framework.java

import java.awt.Color; 
import java.awt.Graphics2D; 

import javax.swing.JFrame; 

public class Framework extends Panel { 

private static long beginTime, takenTime, sleepTime; 
private static long secInNano = 1000000000L; 
private static long secInMilli = 1000000L; 
private static long fps = 60L; 
private static long fpsTime = secInNano/fps; 

public static enum GameState {STARTING, MAINMENU, PLAYING, OVER}; 
public static GameState gameState; 

public Framework() 
{ 
    super(); 

    Thread gameThread = new Thread() { 
     @Override 
     public void run(){ 
      mainLoop(); 
     } 
    }; 

    gameState = GameState.STARTING; 

    gameThread.start(); 
} 

private void mainLoop() { 

    while (gameState != GameState.OVER) { 

     beginTime = System.nanoTime(); 

     switch (gameState) { 
      case STARTING: 
       LoadandInit(); 
       break; 
      case MAINMENU: 
       MainMenu.Update(); 
       break; 
      case PLAYING: 

       break; 
      default: 
       System.out.println("Ok, somebody messed up."); 
       break; 

     } 

     repaint(); 

     takenTime = System.nanoTime() - beginTime; 
     sleepTime = (fpsTime - takenTime)/secInMilli; 
     if (sleepTime < 10) 
      sleepTime = 10; 

     try { 
      Thread.sleep(sleepTime); 
     } catch (InterruptedException ex) { } 


    } 


} 



private static void LoadandInit() { 

    gameState = GameState.MAINMENU; 
} 


public static void Draw(Graphics2D g2d) { 

     g2d.setColor(Color.BLACK); 
     g2d.drawString("DEARP", 0, 0); 
} 

}

+0

太多的靜態方法/變量讓我明白髮生了什麼。使用靜態通常意味着設計問題。我建議你從基礎知識的[自定義繪畫](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html)上的Swing教程開始。 – camickr

+0

它是否陷入無限循環?嘗試從'paintComponent'實現中移除'super.paingComponent()'。 – Antoniossss

+2

@Antoniossss,你應該總是調用super.paintComponent()來確保面板的背景被繪製。它不會導致循環。 – camickr

回答

2

要添加框架的一個實例,你的窗口,但框架不覆蓋paintCompontents。它有一個由Panel調用的靜態Draw方法,但是你永遠不會創建面板的實例。

您可以將您的Draw方法轉換爲paintComponent(),並刪除您是Panel類。

此外,您不應該在Framework的構造函數中啓動線程。這可能會導致run方法查看成員變量的錯誤值。創建一個啓動線程的新的啓動方法。

+4

我希望你想寫'paintComponent()':-) –

-1

我會建議的第2章Java中的殺手遊戲編程。您可以在本書的網站閱讀章節的草案:

http://fivedots.coe.psu.ac.th/~ad/jg/ch1/index.html

+1

鏈接不是答案。充其量,這是一條評論。 – cHao

+0

我猜想沒有好行爲不會受到懲罰。本章共27頁,我無法在答案中總結。 – Michael

+0

如果這是發表評論,我會upvote它。但[這不是一個答案](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers)。 – cHao

相關問題