gpt4 book ai didi

java - SwingWorker、Thread.sleep() 还是 javax.swing.timer?我需要 "insert a pause"

转载 作者:行者123 更新时间:2023-12-01 23:51:02 26 4
gpt4 key购买 nike

我正在开发一个内存游戏,我想对其进行设置,所以我单击第一个 "card" ,然后是第二张,如果它们不相同,第二张卡片会显示几秒钟,然后返回到 "non-flipped"位置。

我尝试使用SwingWorker , Thread.sleepSwingTimer但我无法让它工作。与Thread.sleep第二张卡不会 "flip"如果它是重复的,它会等待 sleep 时间并消失。如果不匹配,它将“面朝下”等待,并且在 sleep 计时器结束后,第一张卡会翻转回来。无论我将 Thread.sleep 放在哪里,都会发生这种情况。

Swing Timer它似乎只是在我与卡片交互时“更改计时器”,所以我最终在它激活之前翻转了 8 张卡片。

我没有运气 SwingWorker我什至不确定它是否适合我正在寻找的东西。

这是我的代码:

    class ButtonListener implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
for(int index = 0; index < arraySize; index++)
{

if(button[index] == e.getSource())
{
button[index].setText(String.valueOf(cards.get(index)));
button[index].setEnabled(false);

number[counter]=cards.get(index);

if (counter == 0)
{
counter++;
}
else if (counter == 1)
{
if (number[0] == number[1])
{
for(int i = 0; i < arraySize; i++)
{
if(!button[i].isEnabled())
{
button[i].setVisible(false);
}
}
}
else
{
for(int i = 0; i < arraySize; i++)
{
if(!button[i].isEnabled())
{
button[i].setEnabled(true);
button[i].setText("Card");
}
}
}

counter = 0;
}
}
}
}
}

基本上,我需要的是当计数器 == 1 并且卡片不匹配时执行此代码:

    button[index].setText(String.valueOf(cards.get(index)));
button[index].setEnabled(false);

然后暂停一下,然后 card is revealed到那时,最后它恢复将卡片恢复到面朝下的位置。

这是我尝试过的 Thread.sleep() :

class ButtonListener implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
for(int index = 0; index < arraySize; index++)
{

if(button[index] == e.getSource())
{
button[index].setText(String.valueOf(cards.get(index)));
button[index].setEnabled(false);

number[counter]=cards.get(index);

if (counter == 0)
{
counter++;
}
else if (counter == 1)
{
if (number[0] == number[1])
{
for(int i = 0; i < arraySize; i++)
{
if(!button[i].isEnabled())
{
button[i].setVisible(false);
}
}
}
else
{
try
{
Thread.sleep(800);
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}

for(int i = 0; i < arraySize; i++)
{
if(!button[i].isEnabled())
{
button[i].setEnabled(true);
button[i].setText("Card");
}
}
}

counter = 0;
}
}
}
}
}

预先感谢您的任何建议

最佳答案

使用javax.swing.Timer来安排将来触发的事件。这将允许您安全地更改 UI,因为计时器是在事件调度线程的上下文中触发的。

能够同时翻转多张卡片的问题更多地与您没有设置阻止用户翻转卡片的状态有关,而不是使用计时器。

下面的示例基本上只允许每组一次翻转一张牌。

一旦两组中的卡片都被翻转,计时器就会启动。当它被触发时,卡片将被重置。

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LinearGradientPaint;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class FlipCards {

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

public FlipCards() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class Card extends JPanel {

private BufferedImage image;
private boolean flipped = false;

private Dimension prefSize;

public Card(BufferedImage image, Dimension prefSize) {
setBorder(new LineBorder(Color.DARK_GRAY));
this.image = image;
this.prefSize = prefSize;
}

@Override
public Dimension getPreferredSize() {
return prefSize;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
LinearGradientPaint lgp = new LinearGradientPaint(
new Point(0, 0),
new Point(0, getHeight()),
new float[]{0f, 1f},
new Color[]{Color.WHITE, Color.GRAY});
g2d.setPaint(lgp);
g2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
if (flipped && image != null) {
int x = (getWidth() - image.getWidth()) / 2;
int y = (getHeight() - image.getHeight()) / 2;
g2d.drawImage(image, x, y, this);
}
g2d.dispose();
}

public void setFlipped(boolean flipped) {

this.flipped = flipped;
repaint();

}
}

public class CardsPane extends JPanel {

private Card flippedCard = null;

public CardsPane(List<BufferedImage> images, Dimension prefSize) {
setLayout(new GridBagLayout());
MouseAdapter mouseHandler = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (flippedCard == null) {
Card card = (Card) e.getComponent();
card.setFlipped(true);
flippedCard = card;
firePropertyChange("flippedCard", null, card);
}
}
};
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.25f;
for (BufferedImage img : images) {
Card card = new Card(img, prefSize);
card.addMouseListener(mouseHandler);
add(card, gbc);
}
}

public Card getFlippedCard() {
return flippedCard;
}

public void reset() {
if (flippedCard != null) {
flippedCard.setFlipped(false);
flippedCard = null;
}
}
}

public class TestPane extends JPanel {

private CardsPane topCards;
private CardsPane bottomCards;

private Timer resetTimer;

public TestPane() {

resetTimer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
topCards.reset();
bottomCards.reset();
}
});
resetTimer.setRepeats(false);
resetTimer.setCoalesce(true);

PropertyChangeListener propertyChangeHandler = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
Card top = topCards.getFlippedCard();
Card bottom = bottomCards.getFlippedCard();
if (top != null && bottom != null) {
resetTimer.start();
}
}
};
BufferedImage[] images = new BufferedImage[4];
try {
images[0] = ImageIO.read(new File("./Card01.png"));
images[1] = ImageIO.read(new File("./Card02.jpg"));
images[2] = ImageIO.read(new File("./Card03.jpg"));
images[3] = ImageIO.read(new File("./Card04.png"));

Dimension prefSize = getMaxBounds(images);

List<BufferedImage> topImages = new ArrayList<>(Arrays.asList(images));
Random rnd = new Random(System.currentTimeMillis());
int rotate = (int) Math.round((rnd.nextFloat() * 200) - 50);
Collections.rotate(topImages, rotate);
topCards = new CardsPane(topImages, prefSize);
topCards.addPropertyChangeListener("flippedCard", propertyChangeHandler);

List<BufferedImage> botImages = new ArrayList<>(Arrays.asList(images));

int botRotate = (int) Math.round((rnd.nextFloat() * 200) - 50);
Collections.rotate(botImages, botRotate);
bottomCards = new CardsPane(botImages, prefSize);
bottomCards.addPropertyChangeListener("flippedCard", propertyChangeHandler);

setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(topCards, gbc);
add(bottomCards, gbc);
} catch (Exception e) {
e.printStackTrace();
}
}

protected Dimension getMaxBounds(BufferedImage[] images) {
int width = 0;
int height = 0;

for (BufferedImage img : images) {
width = Math.max(width, img.getWidth());
height = Math.max(height, img.getHeight());
}

return new Dimension(width, height);
}
}
}

关于java - SwingWorker、Thread.sleep() 还是 javax.swing.timer?我需要 "insert a pause",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16292498/

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