gpt4 book ai didi

java - 两行代码之间的延迟 - 图形

转载 作者:行者123 更新时间:2023-12-01 19:03:27 29 4
gpt4 key购买 nike

我正在尝试创建一个 Simon game 。我正在编写游戏程序,但遇到了问题。我希望程序从队列中读取游戏中之前存在的所有值,并以正确的顺序将它们的颜色变为闪烁(我选择将它们变为灰色,然后在第二秒后恢复正常),这是我的问题。如果您查看 play() 方法,您将看到我在那里写的注释。我该怎么做?

这是我的代码:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Arc2D;
import java.util.Date;

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

import unit4.collectionsLib.Queue;

public class window extends JPanel implements MouseListener , ActionListener{

Queue <Integer>data = new Queue<Integer> ();
Queue <Integer>temp = new Queue<Integer> ();
int random;
Timer prestart;
int prestartcount;
Color [] colors = {Color.red,Color.blue,Color.yellow,Color.green};

public window (){
prestart = new Timer (1000,this);
int prestartcount=0;
prestart.start();
}

public void play (){
random = (int)(Math.random()*4);
data.insert(random);

int x=0;
Color temp=Color.black;
x = data.remove();
this.temp.insert(x);
temp = colors[x];
colors[x]=Color.gray;
// delay of one second here
colors[x]=temp;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

g.setColor(colors[0]);
g.fillArc(80, 150, 250, 250, 0, 360);

g.setColor(colors[1]);
g.fillArc(80, 150, 250, 250, 0, 270);

g.setColor(colors[2]);
g.fillArc(80, 150, 250, 250, 0, 180);

g.setColor(colors[3]);
g.fillArc(80, 150, 250, 250, 0, 90);

g.drawString(prestartcount+"", 0, 30);
repaint();
}

@Override
public void mouseClicked(MouseEvent arg0) {
arg0.getLocationOnScreen();
}

@Override
public void mouseEntered(MouseEvent arg0) {
}


@Override
public void mouseExited(MouseEvent arg0) {
}


@Override
public void mousePressed(MouseEvent arg0) {
}


@Override
public void mouseReleased(MouseEvent arg0) {
}


@Override
public void actionPerformed(ActionEvent act) {
if (act.getSource()==prestart){
if (prestartcount<3)
prestartcount++;
else{
prestart.stop();
play();
}
}
}
}

最佳答案

使用基于Swing的单镜头Timer翻转颜色并调用repaint()。请参阅Using Timers in Swing Applications了解详情。

关于java - 两行代码之间的延迟 - 图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11281427/

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