【发布时间】:2015-07-30 22:21:48
【问题描述】:
当我用 Java 制作 2D 游戏时,发生了一个(至少对我而言)非常奇怪的问题。我提供了一个可运行的缩短示例,它重现了我的问题。当红色方块的x坐标在100和120之间时应该绘制字符串“示例文本“在广场上方。但是,如果运行代码,您会看到窗口完全冻结几秒钟。滞后后,您可以毫无问题地越过该区域,并且将显示文本。仅当程序在正方形上方绘制字符串时,才会出现此问题。如果我更改代码,只有另一个方块出现在红色方块上方,则没有延迟。 (我在我的代码中评论过)
任何帮助将不胜感激。
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JApplet;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
public class MyExample extends JApplet {
int x = 10;
int y = 150;
public void init() {
setFocusable(true);
requestFocus();
Action right = new moveRight();
Action left = new moveLeft();
JRootPane rootPane = getRootPane();
rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
rootPane.getActionMap().put("right", right);
rootPane.getActionMap().put("left", left);
getContentPane().add(new Paint());
}
protected class moveRight extends AbstractAction {
public void actionPerformed(ActionEvent e) {
x+=3;
repaint();
}
}
protected class moveLeft extends AbstractAction {
public void actionPerformed(ActionEvent e) {
x-=3;
repaint();
}
}
class Paint extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.RED);
g.fillRect(x,y,10,10);
g.setColor(Color.BLACK);
g.drawLine(100,100,100,200);
g.drawLine(129,100,129,200);
if(x>100&&x<120) {
g.setFont(new Font("TimesRoman", Font.PLAIN, 15));
g.setColor(Color.BLACK);
g.drawString("Sample Text",x-30,y-25);
//g.fillRect(x,y-15,10,10); - This work fine if you remove the g.setFont and the drawString
}
}
}
}
【问题讨论】:
-
你有没有试过把它画到你的 if 之外?
-
嗯,如果它在您的 if 之外使用完全相同的代码工作,而其他代码在您的“if”内工作,我想不出是什么导致问题触发
-
很抱歉不能做很多事情,但我正在研究这个
-
嗯,如果我读到的内容正确,请尝试将您的 if 更改为“if(x=100)”,这意味着程序会无限绘制
-
啊,我的意思是它只是为了检查是否是触发错误的原因
标签: java swing graphics graphics2d