gpt4 book ai didi

java - 重新绘制 JPanel

转载 作者:行者123 更新时间:2023-12-01 16:35:55 25 4
gpt4 key购买 nike

我想重新绘制我的 jPanel我有一个处理此 JFrame 中显示的屏幕的 App 类,我添加了一个处理所有 JPanel 的 Board JPanel 对象。在董事会类(class)中我有

ex = new Explosion(10, 10); 
new Thread(ex).start();

在我的 Explosion 类中,我有一个构造函数来裁剪我的 Sprite 文件,覆盖 Paint() 和:

public void run()
{
while(cursor < (rows*cols))
{
repaint();

try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println(cursor);

cursor++;
}
}

循环工作正常,但我的屏幕上没有重新绘制,只显示第一个图像。

我可以做什么来刷新?

谢谢

编辑:这是我的代码:

public class Explosion extends JPanel implements Runnable{

private BufferedImage img;

final int width = 320;
final int height = 320;
final int rows = 5;
final int cols = 5;

private int x,y;

private int cursor;

BufferedImage[] sprites = new BufferedImage[rows * cols];

public Explosion(int x, int y)
{
this.x = x;
this.y = y;

try {
try {
this.img = ImageIO.read(new File((this.getClass().getResource("files/explosion2.png")).toURI()));
} catch (URISyntaxException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}

cursor = 0;

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sprites[(i * cols) + j] = img.getSubimage(
i * (width/rows),
j * (height/cols),
width/rows,
height/cols
);
}
}
}

public void run()
{
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
cursor++;
repaint();
}
};

while(cursor < (rows*cols))
{
new Timer(50, taskPerformer).start();


if (cursor==(rows*cols)-1)
cursor=0;
}
}

public void paintComponent(Graphics g){
System.out.println("paintComponent");
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(sprites[cursor], x, y, this);
g.dispose();
}

/*public void paint(Graphics g) {
super.paint(g);

System.out.println("paint");

Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(sprites[cursor], x, y, this);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}*/


}
public class Main extends JFrame{

public Main()
{
Board board = new Board();
add(board);
setTitle("Explosion");
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) {
new Main();
}

}
public class Board extends JPanel{

Explosion ex;

public Board()
{
setDoubleBuffered(true);

ex = new Explosion(10,10);
new Thread(ex).start();
}

public void paint(Graphics g) {
ex.paintComponent(g);

super.paint(g);
Graphics2D g2d = (Graphics2D)g;
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
}

最佳答案

@StanislavL(我还不能发表评论):不,你不必在 EDT 中调用 repaint():

The following JComponent methods are safe to call from any thread: repaint(), revalidate(), and invalidate(). The repaint() and revalidate() methods queue requests for the event-dispatching thread to call paint() and validate(), respectively. The invalidate() method just marks a component and all of its direct ancestors as requiring validation.

来源:http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html#exceptions

cursor 是否声明为 volatile ?如果不是,EDT 可能看不到线程对光标所做的更改。因此,始终绘制第一个图像。

关于java - 重新绘制 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9094364/

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