gpt4 book ai didi

带有计时器/延迟的JPanel上的java绘图

转载 作者:行者123 更新时间:2023-12-03 18:41:50 26 4
gpt4 key购买 nike

我正在尝试制作一个小的 Mosaic 程序,它在整个 JPanel 上随机绘制正方形。我想这样做,它每 0.2 秒 1 个方格绘制一次(不是一次全部绘制),但到目前为止我只能让它用 while 循环一次绘制。我尝试过使用 ActionListener 和 Timer,但我发现我无法将相同的 Graphics g 传递给 ActionListener。然后我尝试使用 Thread.Sleep(200) 但随后应用程序卡住了。现在我已经尝试过 System.currentTimeMillis();但它与 Threads 相同......在整个互联网上搜索但没有找到任何有效的方法。

主要内容:

import javax.swing.JApplet;


public class Main extends JApplet{
public void init(){
setSize(500, 300);
Mosaic mosaic = new Mosaic();

setContentPane(mosaic);

}
}

应用:

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class Mosaic extends JPanel{

private int width, height;
private int ruut; // square
private int w = width / 2, h = height / 2; // middle of the app

Random rand = new Random();
Color color;

public Mosaic(){
this(500, 300, 10);
}

public Mosaic(int width, int height, int ruut){
this.width = width;
this.height = height;
this.ruut = ruut;
setBackground(Color.BLACK);
}

public void paintComponent(Graphics g){
super.paintComponent(g);
//draws random squares
while (true) {
moveNext(g);
wait(200);
}
}

//delay n millisec
public void wait(int n){
long t0, t1;
t0 = System.currentTimeMillis();

do {
t1 = System.currentTimeMillis();
} while ((t1 - t0) < n);
}

//next square
public void moveNext(Graphics g){

int r = rand.nextInt(4);

switch (r) {
case 1:
h += ruut;
wallTest();
break;
case 2:
h -= ruut;
wallTest();
break;
case 3:
w -= ruut;
wallTest();
break;
case 4:
w -= ruut;
wallTest();
break;
}

color = new Color(0, rand.nextInt(255-50)+50, 0);
g.setColor(color);
g.fillRect(w, h, ruut, ruut);
}

public void wallTest(){
if (h > height){
h = 0;
}
if (h < 0){
h = height;
}
if (w > width){
w = 0;
}
if (w < 0){
w = width;
}
}

最佳答案

您需要维护一个离屏缓冲区,并在计时器事件的控制下将图 block 绘制到该缓冲区中。

然后 paintComponent 简单地将缓冲区复制到屏幕。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;

public class Mosaic extends JPanel{

private int width, height;
private int ruut; // square
private int w = width / 2, h = height / 2; // middle of the app
private BufferedImage buffer;

Random rand = new Random();
Color color;

public Mosaic(){
this(500, 300, 10);
}

public Mosaic(int width, int height, int ruut){
this.width = width;
this.height = height;
this.ruut = ruut;
this.buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
setBackground(Color.BLACK);
setPreferredSize(new Dimension(width, height));
setDoubleBuffered(false);
new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveNext(buffer.getGraphics());
}
}).start();
}

public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(buffer, 0, 0, this);
}

//next square
public void moveNext(Graphics g){

int r = rand.nextInt(4);

switch (r) {
case 1:
h += ruut;
wallTest();
break;
case 2:
h -= ruut;
wallTest();
break;
case 3:
w -= ruut;
wallTest();
break;
case 4:
w -= ruut;
wallTest();
break;
}

color = new Color(0, rand.nextInt(255-50)+50, 0);
g.setColor(color);
g.fillRect(w, h, ruut, ruut);
repaint();
}

public void wallTest(){
if (h > height){
h = 0;
}
if (h < 0){
h = height;
}
if (w > width){
w = 0;
}
if (w < 0){
w = width;
}
}
}

关于带有计时器/延迟的JPanel上的java绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8138017/

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