This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Need help understanding random loops. code inside

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm trying to make 3 images become random with one of them having a string of text on it. I was curious as to if my code will make these images random and also, i'm having trouble having the image appear. I've imported them in the right way but i guess i must have formatted them wrong because i get an error messge saying that the image action variable has no value

here is the project to clarify anything:

In your applet, randomly show an image when the applet is started or restarted. Also, print a message on the applet, depending on which image is shown. Hints: the start() method would be a good place to put some sort of random flag to determine which image should be shown. It is also a good place to call a separate method to set the image and message. The paint() method should only do the actual drawing of the image and the message. Separate methods can be created to set the image and the message into variables for the paint() method to use.

sorry for not using the proper way to show code i dont know how
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are overriding start(), add a public access modifier. In addition, use graph.drawImage() in paint() method -- to display a random image.
 
Marshal
Posts: 67412
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mack Grill wrote: . . . sorry for not using the proper way to show code i dont know how . . .

You can click the code button, which gets you [code=java][/code] and you put your cursor inside the pair of ][. Then you can write your code there. Or you can highlight the code and push to code button. Which is what I did. Unfortunately that improves things little because you have done the only thing worse than indenting with tabs: mixing spaces and tabs.
You are using Math.random and multiplying by 2: you need to read the API documentation to see what you get, and then work out whether multiplying by 2 is any good. You are also using a very peculiar means of choosing from three possibilities. I would have thought an array would be a better way to choose; I think I have already told you that.
Also your choice is a local variable in the start() method, which you never use in the paint() method. In fact, your choice vanishes into cyber-limbo never to be seen again, long before you get anywhere near paint().
 
Mack Grill
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


thanks
 
Ranch Hand
Posts: 77
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tips:

  • Load all the images in the init() and store them as class attributes in an array.
  • Also declare as an attribute of the class:
    1) A Random object .
    2) In int index.
  • In start(), call Random.nextInt() and set it as the value of the index, then call repaint().
  • In paint(), draw the image of the array that is at index.
  •  
    Campbell Ritchie
    Marshal
    Posts: 67412
    257
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You mean random.nextInt(int)? It should be easy enough to work out which number to put there!
     
    Andrew Thompson
    Ranch Hand
    Posts: 77
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:..It should be easy enough to work out which number to put there!



    Yep, that was pretty much what I was thinking. Paint with a broad stroke, presume the OP knows how to find & use the documentation.
     
    We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!