我有一個錯誤我的代碼說:的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型隨機的東西還有這件事情不會讓我張貼... 我只會繼續打字,直到它停止給我的錯誤...
您不會收到錯誤,但會發生異常。 'at ..'指向異常涉及的位置,但是描述爲什麼異常被拋出的主要部分應該在'at..'部分之前解釋。請[編輯]你的問題,併發布適當的堆棧跟蹤,包括異常消息。 – Pshemo
你不應該手動調用'paintComponent'。我會建議[閱讀教程](https://docs.oracle.com/javase/tutorial/uiswing/painting/)。 – resueman
BTW:if(GameState ==「idle」)' - > [如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in -java)。我知道你的代碼現在可以工作,因爲你正在使用文字,但將來當你開始從某些資源(控制檯,文件)讀取字符串時,或者當你將動態創建新字符串時(比如'replace'或'substring '方法)你會看到'=='將開始失敗。 – Pshemo