Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6614461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:20:31+00:00 2026-05-25T20:20:31+00:00

I am making the game Memory, when you pick two cards and if they

  • 0

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.

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

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().

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!).

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).

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!).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:20:32+00:00Added an answer on May 25, 2026 at 8:20 pm

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

    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.

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

    Edit:
    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.

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a autoclicker atm to get back into C# before my school starts
I am making a memory type game on iPhone, I would like to know
I'm making an iphone game, currently using openAL for SFX, we want to keep
I'm making a small game engine and need to have a memory pool associated
I'm making the memory game concentration. In this I have a specified obligation to
I built a slideshow/decision-making game in Flash but would like to try to redo
I am making a game in JAVA where I want to come up with
With regards to making a game server, it seems Erlang always comes up as
I'm making this game where I'm trying to pair people. So I have this
I'm making a game. Now if my player goes to another level the music

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.