如何调用 Java GUI 的即时更新?(与 Thread.sleep() 冲突) [英] How do I invoke an immediate update to a Java GUI? (conflict with Thread.sleep())

查看:86
本文介绍了如何调用 Java GUI 的即时更新?(与 Thread.sleep() 冲突)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作游戏记忆,当您选择两张牌时,如果它们匹配,您可以保留它们,否则您将它们退回.如果你还记得你已经选择的牌,你可以对接下来的两张牌进行更好的猜测.

I am making the game Memory, when you pick two cards and if they match you get to keep them, otherwise you turn them back. If you then remember cards you have already chosen, you can venture a better guess on the next two cards.

我遇到的问题是 repaint() 方法不能立即重绘.

The problem I have involves the repaint() method not immediately repainting.

当我翻转第二张牌时,无论结果如何,我都希望在弃牌或翻转之前显示两张牌正面朝上翻转.我通过调用 sleep() 来做到这一点.

When I flip the second card, no matter the outcome, I want to show both cards flipped right-side up before either discarding them or flipping them back over. I do this by calling sleep().

当然,如果我 repaint() 将卡片正面朝上翻转,请稍等,然后根据它们的值再次 repaint(),很有帮助小 Java 只会重绘一次(我想念 C!).

Of course if I repaint() the cards to flip them right-side up, wait a second, and then repaint() again based on their values, helpful little Java will only repaint once (I miss C!).

基本上我想在我 sleep() 之前强制调用更新.我读过一些其他线程,基本上说最好的方法是创建两个线程来保持逻辑和图形分开,然后你可以 sleep() 你的逻辑并保持你的 GUI 更新,但我是在高中一年级 CS 课程的第一学期,我希望将其保持在课程水平(尽管我在暑期花了很多时间在 C 中进行网络开发和编码).

Basically I want to force invoke a update before I sleep(). I have read some other threads that basically say the best way is to create two threads to keep your logic and graphics separate, and then you can sleep() your logic and keep your GUI updating, but I am in the first semester of a first year CS course in high school, and I would want to keep it on the course's level (although I spent quite a bit of time over the summer web developing and coding in C).

因为我知道 StackOverflow 上的乐于助人的人喜欢阅读代码,所以这是我在下面提到的程序的一部分.类 HitArea 是 Card 对象,cards[] 数组包含一定数量的 HitArea.(我还没有重命名HitArea 类).activeCard1activeCard2 是我用来跟踪用户的两个选择的 HitArea 实例,空白构造函数是一个保留的不可见"HitArea,虽然稍后我会将其更改为 null.最后,cards.flip() 反转 toggle 布尔值,以确定卡片是否正面朝上.

Because I know the helpful folks on StackOverflow love to read code, here is the part of the program I am referring to below. The class HitArea is the Card object and the cards[] array contains an amount of HitArea's.(I haven't gotten to renaming the HitArea class). activeCard1 and activeCard2 are HitArea instances I use to keep track of the user's two selections, and the blank constructor is a reserved "invisible" HitArea, although I will change it to null later. Finally, cards.flip() inverts a toggle boolean which determines whether the card is right-side up.

public void respond(HitArea choice)
{
    if(choice.inGame)
    {
        if(activeCard1.value == 0 && activeCard1.value == 0)
            activeCard1 = choice;
        else if((!(activeCard1.value == 0) && activeCard2.value == 0) && (activeCard1.id != choice.id))
        {
            activeCard2 = choice;
            check();
        }

    }
}
public void check()
{
    update();
    pause(250);
    if(activeCard2.value == activeCard1.value)
    {
        score += 2;
        activeCard1.inGame = false;
        activeCard2.inGame = false;
    }
    activeCard1.flip();
    activeCard2.flip();
    activeCard1 = new HitArea();
    activeCard2 = new HitArea();
}
public void pause(int milliseconds)
{
    try{
      Thread.currentThread().sleep(milliseconds);
    }
    catch(InterruptedException e){
        System.out.println("Exception: " + e);
    }
}

public void mousePressed(MouseEvent e)
{
    int x = e.getX();
    int y = e.getY();

    for (int i = 0; i < cardNum; i++)
        if(cards[i].boundsCheck( x, y ) )
        {
            repaint();
            cards[i].flip();
            respond(cards[i]);
        }
}

我毫不怀疑我的代码中存在一些问题,所以请随时指出它们.我觉得我的基本结构还可以,我宁愿不为这个项目创建多个线程(记住,这是基本的!).

I have no doubt there are some issues in my code, so feel free to point them out. I think my basic structure is okay, and I would rather not create multiple threads for this project (remember, it's basic!).

推荐答案

不要在主 Swing 线程 EDT 上调用 Thread.sleep(...).永远.而是使用摇摆计时器.

Don't call Thread.sleep(...) on the main Swing thread, the EDT. Ever. Instead use a Swing Timer.

考虑使用 JLabels 来显示您的图像,然后您可以通过简单地换出 ImageIcons 来翻转"您的卡片.当第二张卡片翻转后,如果没有匹配,则启动一个延迟 xxxx 毫秒的非重复 Swing Timer,并在 Timer 的 ActionListener 的 actionCommand 方法中将两个 JLabel 恢复为默认的 ImageIcon.

Consider using JLabels to display your images, and then you could "flip" your cards by simply swapping out ImageIcons. When the second card has been flipped, if there's no match, start a non-repeating Swing Timer with a delay for xxxx ms, and in the Timer's ActionListener's actionCommand method have it revert both JLabel's back to the default ImageIcon.

javax.swing.Timer 教程可以在这里找到:如何使用摇摆计时器

The javax.swing.Timer tutorial can be found here: How to use Swing Timers


关于您关于使用 g.drawString 的评论:现在更容易了,因为您所要做的就是换出 JLabel 的文本.但稍后如果您决定升级程序以显示图像,您就可以完成所有设置.


Regarding your comment about using g.drawString: it's even easier now since all you have to do is swap out your JLabel's text. But later if you decide to upgrade the program to display images, you've got it all set to do this.

编辑 2:
关于你关于创建一个新的 ActionListener 类的问题:我会为此使用一个匿名内部类.例如:

Edit 2:
Regarding your question about making a new ActionListener class: I'd use an anonymous inner class for this. For example:

  int delayTime = 2 * 1000;
  javax.swing.Timer myTimer = new Timer(delayTime, new ActionListener() {

     @Override
     public void actionPerformed(ActionEvent e) {
        // TODO: put in the code you want called in xxx mSecs.
     }
  });
  myTimer.setRepeats(false);
  myTimer.start();

这篇关于如何调用 Java GUI 的即时更新?(与 Thread.sleep() 冲突)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆