gpt4 book ai didi

java - 告诉 Java 不要重绘

转载 作者:行者123 更新时间:2023-12-01 22:30:36 26 4
gpt4 key购买 nike

我正在创建一个小程序,允许用户在使用箭头键时移动网格上的框。我已经做到了,所以盒子会移动,但我有一个问题。我想将任何已经访问过的网格框着色为黑色。因此,如果我的盒子的坐标是(20,20,20,20),它会将盒子留在原来的位置,最终整个屏幕都会变黑。我尝试删除 repaint(); 但程序无法正常工作。有没有办法告诉java不要重新绘制特定的框架?非常感谢任何帮助!

package robot;
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.JApplet;
import java.awt.event.KeyListener;


public class Robot extends JApplet implements KeyListener {
private Rectangle rect;

public void init(){
rect = new Rectangle (0, 0, 20,20);
setFocusable(true);
requestFocusInWindow();

addKeyListener(this);
}

public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0,0,500,500);
{
this.setSize(360, 360);
}



g.setColor(Color.black);
g.drawLine(0,0,360,0);
g.drawLine(0,20,360,20);
g.drawLine(0,40,360,40);
g.drawLine(0,60,360,60);
g.drawLine(0,80,360,80);
g.drawLine(0,100,360,100);
g.drawLine(0,120,360,120);
g.drawLine(0,140,360,140);
g.drawLine(0,160,360,160);
g.drawLine(0,180,360,180);
g.drawLine(0,200,360,200);
g.drawLine(0,220,360,220);
g.drawLine(0,240,360,240);
g.drawLine(0,260,360,260);
g.drawLine(0,280,360,280);
g.drawLine(0,300,360,300);
g.drawLine(0,320,360,320);
g.drawLine(0,340,360,340);
g.drawLine(0,360,360,360);
//verticle lines
g.drawLine(0,0,0,360);
g.drawLine(20,0,20,360);
g.drawLine(40,0,40,360);
g.drawLine(60,0,60,360);
g.drawLine(80,0,80,360);
g.drawLine(100,0,100,360);
g.drawLine(120,0,120,360);
g.drawLine(140,0,140,360);
g.drawLine(160,0,160,360);
g.drawLine(180,0,180,360);
g.drawLine(200,0,200,360);
g.drawLine(220,0,220,360);
g.drawLine(240,0,240,360);
g.drawLine(260,0,260,360);
g.drawLine(280,0,280,360);
g.drawLine(300,0,300,360);
g.drawLine(320,0,320,360);
g.drawLine(340,0,340,360);
g.drawLine(360,0,360,360);













Graphics2D g1 = (Graphics2D) g;

g1.setColor(Color.black);
g1.fill(rect);
}


@Override
public void keyPressed(KeyEvent e)
{ switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
rect.x -= 20;
break;
case KeyEvent.VK_RIGHT:
rect.x += 20;
break;
case KeyEvent.VK_UP:
rect.y -= 20;
break;
case KeyEvent.VK_DOWN:
rect.y += 20;
break;


}

repaint();

}
public void pain(Graphics g){
g.fillRect(rect.x, rect.y, 20, 20);
}



@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}


}

最佳答案

不,Java 必须能够在需要时重新绘制。 repaint 不仅在更新屏幕时被调用。如果用户移动框架或最小化它或将另一个窗口放在前面等。所有这些都可能需要重新绘制。

这意味着您的程序必须有一种方法来跟踪框的位置,以便您可以重新绘制之前的线条。

但是,这可能会很慢。另一种更复杂的选择是将盒子绘制到 BufferedImage 上,然后在 paint() 中显示 BufferedImage。对框位置的任何更新都可以覆盖 BufferedImage 中所需的像素

关于java - 告诉 Java 不要重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27842800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com