[英]repaint() suddenly doesn't work
我已经进行了30种不同的Google搜索,但都没有给出答案,所以我来到了这里。 因此,我尝试在屏幕上左右移动播放器的矩形(黑色正方形)。 当我使用常规图形时,它工作正常,但是现在我在使用Graphics2D,则repaint()似乎什么也没做(即,当您按下左右箭头键时,矩形不会移动)。
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class boxface extends JComponent implements KeyListener {
private boxobj obj;
private int x=0, y=650;
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()== KeyEvent.VK_RIGHT)
moveRight();
else if(e.getKeyCode()== KeyEvent.VK_LEFT)
moveLeft(); }
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
Rectangle player = new Rectangle(x, y, 50, 50);
Rectangle floor = new Rectangle(0, 700, 750, 700);
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.GREEN);
g2.fill(floor);
g.setColor(Color.BLACK);
g2.fill(player); }
public void moveLeft() {
if(x > 0) {
x -= 50;
repaint(); }}
public void moveRight() {
if(x < 700) {
x += 50;
repaint(); }}
public boxface(){
this.obj=new boxobj();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false); }
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(400, 200, 756, 779);
f.setMinimumSize(new Dimension(756, 0));
f.setResizable(false);
f.getContentPane().add(new boxface());
f.setVisible(true);
}
});
final java.util.Timer tmr = new java.util.Timer();
tmr.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
System.out.println("A second has passed.");
/* the idea is that I could make a square with random
* dimensions (within a certain limit), so that every
* time the timer loops, a new, random square is made.
* I just can't seem to move the rectangles using
* repaint(); because they're Graphics2D rectangles,
* and I can't find a way around this.
*
* An example of this can be shown if you run this
* code; the "player" rectangle cannot be moved, even
* though keylistener is picking up inputs and the
* rectangle's co-ordinates are being changed. In
* other words, repaint(); isn't doing anything. */
}
},0,1000);
}//end main
}//end class
同样,“ boxobj”类现在只是一个空类。 在这里,我计划放置随机矩形的初始化位置。 我将其放在此处以方便复制粘贴。
public class boxobj {
}
问题是您要更新x
变量,但要绘制player
对象。
当构造player
(通过Rectangle player = new Rectangle(x, y, 50, 50);
),它将在执行该行时获取x
值的副本。 由于您是在同时声明和初始化,因此我们知道x
为零,因此将用(0, 650, 50, 50)
0,650,50,50)实例化player
。
稍后,用户按下向右箭头键,事件监听器将触发。 这会将x
增加到50,并调用repaint
但重要的是,根本不会更新player
对象。 当paintComponent
方法被绘画系统调用时, player
仍为(0, 650, 50, 50)
。
本质上, x
和y
记录了玩家的位置,但是您正在使用player
对象来绘制玩家,并且这些变量不会一并更新。
纠正此问题的最佳方法是将玩家的位置准确地存储在一个位置。 您可以保留x
和y
并修改paintComponent
方法以使用它们,也可以丢弃这两个变量并改为修改player
对象(使用player.setLocation
)。 无论哪种方式都可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.