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 7906437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:58:21+00:00 2026-06-03T10:58:21+00:00

This is probably a stupid question but I can not seem to either remember

  • 0

This is probably a stupid question but I can not seem to either remember how to stop the program from doing this or I never learned it.

I’m creating a blackjack game which draws random cards with the paintComponent. When the hit button is clicked, I want it to draw the next card and generate the new total. However, whenever I click the hit button, it draws the new card perfect, but it then creates all new cards on top of my already drawn cards.

How do I stop the repaint method from picking new random cards and keep them the same throughout the program? I should mention, all the cards are stored in an array and are called to with a random number generator. Also, this happens when I resize the java window.

If asked, I will post the code but I feel like this has a simple solution.

  • 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-06-03T10:58:27+00:00Added an answer on June 3, 2026 at 10:58 am

    You will need to move your ‘select random card’ logic outside of the paint method.

    It might be a better idea to calculate all the cards in your array when you first build it, rather than selecting randomly at paint time. In other words, when you create the ‘deck’ array, build the entire deck so that every card knows what they are, before the user even begins to be dealt any cards from the deck. This way, there is no risk of them changing during gameplay. If you’re only talking about 52 cards, or a small multiple of 52 cards, then the array is still pretty small and it’ll be quick to randomise the entire array.

    Alternatively, you could put a check on your “select random card” method that says something like this…

    int cardValue = -1;
    
    paintComponent(){
        if (cardValue == -1){
            cardValue = drawRandomCard();
        }
        // now paint it.
    }
    

    ie – restrict the drawRandomCard() to only run if the card doesn’t already have a value.

    Ultimately though, the best solution would be to completely separate the painting code from the logic – its bad coding practice for GUI activities like painting to be mixed in with programming logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a stupid question, but I can't seem to do it. I
This may sounds like a stupid question but can't find anything on google, probably
This is probably a really stupid question but I can't figure this out. I
This is probably a stupid question, but as far as I can tell there's
At first, this question can sound really stupid, but it is not in fundamental.
Probably a stupid question but so far I can't figure this out... I have
So, this question is probably fairly stupid, but I'm not too familiar with Excel
This is probably a stupid question, but how can I execute a shell command
This is probably a stupid question, but not having much joy researching it myself.
It's probably stupid question, but I can not find an answer. How can I

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.