2016-06-18 39 views
-3

我有一個錯誤我的代碼說:的paintComponent錯誤

at javax.swing.JComponent.paintComponent(JComponent.java:783) 
at com.game.screen.screen.PaintComponent(screen.java:29) 
at com.game.screen.screen.GameScreen(screen.java:39) 
at com.game.gamecore.Core.StartGame(Core.java:15) 
at com.game.gamecore.Core.main(Core.java:6) 

我的代碼: com.game.screen

package com.game.screen; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.Ellipse2D; 

import javax.swing.*; 

public class screen extends JPanel implements ActionListener { 
    /** 
    * 
    */ 
    ActionListener AL = new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 

     } 
    }; 

    Timer timer = new Timer(5, AL); 
    double x = 0, y = 0, velY = 0,velX = 0; 


    public void PaintComponent(Graphics pixel){ 
     super.paintComponent(pixel); 
     Graphics2D g2 = (Graphics2D) pixel; 
     Ellipse2D circle = new Ellipse2D.Double(x,y,40,40); 
     g2.fill(circle); 
     timer.start(); 
    } 
    public void GameScreen(){ 

     JFrame GameScreen = new JFrame("THE GAME"); 
     GameScreen.setSize(600, 600); 
     PaintComponent(null); 
     GameScreen.setVisible(true); 




    } 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 

    } 

} 

com.game.gameCore.Core

package com.game.gamecore; 

public class Core { 
    public static void main(String[] args) { 
    // new Menu(); 
    new Core().StartGame(); 
    } 

    public void StartGame(){ 

     String GameState = "idle"; 

     if(GameState=="idle"){ 
     GameState="Running"; 
     new com.game.screen.screen().GameScreen(); 


     } else if(GameState=="Running") { 

      //enter process kill code here 

     } 
    } 
} 

我該如何調用我的PaintComponent(grahpics G)代碼?

ineedto型隨機的東西還有這件事情不會讓我張貼... 我只會繼續打字,直到它停止給我的錯誤...

+0

您不會收到錯誤,但會發生異常。 'at ..'指向異常涉及的位置,但是描述爲什麼異常被拋出的主要部分應該在'at..'部分之前解釋。請[編輯]你的問題,併發布適當的堆棧跟蹤,包括異常消息。 – Pshemo

+0

你不應該手動調用'paintComponent'。我會建議[閱讀教程](https://docs.oracle.com/javase/tutorial/uiswing/painting/)。 – resueman

+0

BTW:if(GameState ==「idle」)' - > [如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in -java)。我知道你的代碼現在可以工作,因爲你正在使用文字,但將來當你開始從某些資源(控制檯,文件)讀取字符串時,或者當你將動態創建新字符串時(比如'replace'或'substring '方法)你會看到'=='將開始失敗。 – Pshemo

回答

3

問題和一些解決方案:

  1. PaintComponent != paintComponent。字符串大小寫對於Java標識符很重要,所以請使用正確的一個,這裏是paintComponent
  2. 在paintComponent方法重寫之前,您不使用@Override。如果你這樣做了,編譯器會指出你的方法不是真正的覆蓋,迫使你更仔細地檢查你的方法簽名,看看爲什麼,你可能會發現你自己的錯誤。
  3. 關於if(GameState=="idle"){,請勿使用==!=來比較字符串。改爲使用equals(...)equalsIgnoreCase(...)方法。瞭解==檢查兩個對象引用是否相同,這不是您感興趣的內容。另一方面,方法檢查兩個字符串是否具有相同順序的相同字符,這是重要的。所以相反,if ("idle".equals(GameState)) {,或者更好的辦法是,使用枚舉來表示遊戲狀態,因爲這樣會更簡單。
  4. 只發布部分異常堆棧跟蹤問題,但省略了關鍵的開始部分,該部分告訴你究竟拋出了什麼異常。它是什麼?一個NullPointerException?
  5. re,timer.start(); - 你在你的paintComponent方法中調用它,你不應該這樣做。你不能完全控制何時或者甚至是否調用了paintComponent,所以在這個方法中永遠不應該有對象狀態改變的代碼。
  6. PaintComponent(null);你正在直接調用你的繪畫方法,你永遠不應該這樣做。如果您想建議JVM爲您繪畫,請撥打repaint()。這是你的NullPointerException(我們現在知道正在發生)的原因。您傳入Graphics對象的空引用,然後嘗試調用它的方法。不要這樣做。
+0

是的,它是一個Nullpointer異常,爲什麼問題是你可以清理我的代碼如何工作?因爲我沒有看到你想告訴我什麼gamestate的東西現在並不重要,因爲它永遠是空閒的 –

+0

@SvenTjeerdsma:你似乎並不清楚這個網站的工作方式,但請理解它不是「請爲我清理我的代碼」我們可以幫助回答問題,有時會發現錯誤,但是您有責任清理自己的代碼,否則有時候會導致您的問題被關閉,只是fyi。請考慮通過[tour]進行並做看看[幫助]部分以及[如何提出好問題]部分(http://stackoverflow.com/help/how-to-ask)部分。 –

+0

@SvenTjeerdsma:answer edited。你會想要要通過Swing圖形教程瞭解更多關於如何執行S的內容翼畫:* [教訓:執行自定義繪畫](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) –