【发布时间】:2016-06-18 23:18:34
【问题描述】:
我的代码出错:
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(graphics G) 代码?
需要随机输入一些东西,否则这个东西不会让我发布它...... 我会一直打字直到它停止给我错误...
【问题讨论】:
-
你不会得到错误,而是异常。
at ..也指出了与异常有关的位置,但描述为什么抛出异常的主要部分应该在at..部分之前解释。请edit您的问题并发布正确的堆栈跟踪,包括异常消息。 -
您不应该手动调用
paintComponent。我建议reading a tutorial 讨论这个问题。 -
顺便说一句:
if(GameState=="idle")-> How do I compare strings in Java?。我知道您的代码现在可以工作,因为您使用的是文字,但将来当您开始从某些资源(控制台、文件)读取字符串时,或者当您将动态创建新字符串时(例如使用replace或 @987654332 @方法)你会看到==会开始让你失望。 -
困扰我的不是游戏状态或堆栈跟踪,而是我如何称呼该图形。我创建了一个 void 并将我的代码插入到那里,通常你在主类中调用,但现在你需要将它插入到你的 JFrame 中。我的问题是我需要调用什么方法(JFrame 的?