Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Make an Image disappear and appear at random  RSS feed

 
S Gresty
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to be honest and say I don't have a clue on Java.

If someone could give me a code on how 2 make my 3 images appear and disappear at random within a 30 second period, then that would be great.

My code so far:




Basically, I have to create a game which you get scores from for a college project.

The scoring is workng fine, on the actual main games screen.

All I need to do now is get these images to appear at random and disappear.

After this is done.

I need to work out a way on how to get the score into a high score list, I have some how managed to get the players name to appear in it, but can't seem to get the score achieved on this list.

Any help would be superb.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We generally don't just hand out an answer, but folks can help point you in the right direction, give you tips and pointers, etc.

So, for starters,

do you know how to make an image appear at all? and then make it disappear?

Your question is still pretty broad. When you say "appear and disappear at random", do you mean appear in a random place on the screen? do you mean appear at a random time, then disappear after a set time? do they stay on the screen for a random length of time? Are there requirements as to how many times any or all the images must appear, and for how long? Can the same image (or even different images) appear on the screen simultaneously, or is it only one at a time?

think about these, and come back with a better defined question. Also, it helps to focus your question down to one issue at a time. Which part do you need help with first? it does no good for someone to spend 10 minutes composing an answer on how to display an image on screen when you only need help with the random part.

 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Is this a College or University assignment? If so, I am a bit surprised you are using NetBeans; you need to write the code by hand to know what it does. I am afraid we cannot give out code for assignments, otherwise you might get a 0 overall.

Not sure what would work; I would think using something like SwingWorker to start a secondary thread with a random sleep() duration, then alter the visibility. You can find details here, but it is by no means easy. This sort of thing is usually discussed on the Swing forum, so I shall move you.

You could always put Middlesbrough (our local team) on the display; that will make the whole thing vanish randomly
 
S Gresty
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Middlesbrough lol. I was hoping for a Boro victory on monday, was watching it in the pub like.

I want to see Newcastle down, mainly because of the "we're a big club" fan mentality. The Inter-Toto cup surely doesn't count!

Anyway, back on topic.

I understand you can't just give out the answer.

I will explain what I want to be able to do then as simply as I can from the beginning.

I have been given a project of creating a game, which has a menu screen (where user puts name in), an actual game screen (with images to click and getting a score from the image that has been clicked), and a timer within this screen, the other screen is a high scores screen.

I have the menu screen working, you can enter in a name and it'll remember the name in the high scores screen at the end once you have finished playing the game. However, I can't get the high scores screen to record the score that was achieved within the game. I've been told about vectors, arrays and alsorts, but to me, they mean absolutely nothing

Back to the image thing, the images that are clicked need to appear and disappear at random (basically a bit like that "wack a snake" game you can play in game arcades haha).

If you could point us in the right direction, then that'd be superb.

 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't follow footer, but I do know about Boro's ability to snatch defeat from the jaws of victory.

If you set up a Timer, you can easily get the images to appear and disappear; it is a bit awkward to change the frequency of appearances and disappearance; you would have to use the setDelay() method.
Use the Random class to get the different delays.
Try setting up one image, and a Timer and Random, and call some sort of ActionListener to make the image appear, on a simple GUI divorced from your game. If you get that to work, you might be able to transfer the technique to a larger app. I have only used Timer for regular occurrences. Note there are two tutorial links in the Timer API documentation.

If that doesn't work, you might need to create new Threads with SwingWorker or similar.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!