[英]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.