The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Need help making 3 images become random. code inside
Discuss Need help making 3 images become random. code inside in the Java Help forum on Dev Shed. Need help making 3 images become random. code inside Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 8th, 2012, 10:13 AM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 20
Time spent in forums: 8 h 43 m 44 sec
Reputation Power: 0
|
|
|
Need help making 3 images become random. code inside
I'm trying to run 3 images in an applet with one of them having text on the image.
here is the code that I have:
import java.applet.Applet;
import java. awt.*;
public class MyPictures extends Applet {
public void start(){
int rint = (int)(Math.random() * 3);
if (rint == 0){
Image action = getImage(getDocumentBase(),"images/image 3.jpg");
}
else{
getImage(getDocumentBase(),"images/image 1.jpg");
getImage(getDocumentBase(),"images/image 2.jpg");
}
}
public void paint(Graphics graph) {
graph.drawImage(action, 50, 50, this);
graph.drawString("The Forest of Bent Tress", 475, 30);
resize(1200,900);
}
}
|

March 8th, 2012, 10:44 AM
|
|
|
|
Mmm. Remember: JavaScript is not Java but they do taste similar.
Moving to the Java forum!
P.S. Welcome to DevShed!
Last edited by Winters : March 8th, 2012 at 10:46 AM.
|

March 8th, 2012, 11:08 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Can you explain what your problem is?
If there are errors, copy and paste the full text of the error message here.
|

March 8th, 2012, 12:01 PM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 20
Time spent in forums: 8 h 43 m 44 sec
Reputation Power: 0
|
|
|
I don't receive any errors but I can't make the 3 images become random
Each time I run the applet, all I see the third picture that the paint() method displays
|

March 8th, 2012, 12:20 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | I don't receive any errors |
The code you posted does NOT compile without errors. If it does not compile, you can not execute it.
|

March 8th, 2012, 12:30 PM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 20
Time spent in forums: 8 h 43 m 44 sec
Reputation Power: 0
|
|
|
i just tried executing it and and the applet started like normal.
I need to re-write the other 2 image statements but I'm not sure how.
|

March 8th, 2012, 12:35 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Please post your version of the program that executes. Your first post does NOT compile.
When posting code, please Wrap your code in code tags:
[code]
> YOUR CODE <
[/code]
|

March 8th, 2012, 12:44 PM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 20
Time spent in forums: 8 h 43 m 44 sec
Reputation Power: 0
|
|
|
sorry about that.
It seems I posted a code with a missing statement and that was why it would not compile
[CODE]
import java.applet.Applet;
import java. awt.*;
public class MyPictures extends Applet {
public void start(){
int rint = (int)(Math.random() * 3);
if (rint == 0){
Image action = getImage(getDocumentBase(),"images/image 3.jpg");
}
else{
getImage(getDocumentBase(),"images/image 1.jpg");
getImage(getDocumentBase(),"images/image 2.jpg");
}
}
public void paint(Graphics graph) {
Image action = getImage(getDocumentBase(),"images/image 3.jpg");
graph.drawImage(action, 50, 50, this);
graph.drawString("The Forest of Bent Tress", 475, 30);
resize(1200,900);
}
}
|

March 8th, 2012, 12:48 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
You left off the end tag: [/code]
What is all the code in the start method for if you are not going to use it?
That is where you need to:
1) generate a random number
2) use that number to set the value of the image variable.
|

March 8th, 2012, 12:55 PM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 20
Time spent in forums: 8 h 43 m 44 sec
Reputation Power: 0
|
|
sorry again
My project said that the start() method would be a good place to use as a 'random flag' for the images but I don't know how to write the image statements to make them random so I just toyed around with it.
I've yet to get any other images to show except the one i put in my paint() method.
|

March 8th, 2012, 01:01 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Take the image load code out of the paint method. The code in the start() should be where you load the image.
If you get compiler errors, post them if you want help fixing them.
|

March 8th, 2012, 01:29 PM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 20
Time spent in forums: 8 h 43 m 44 sec
Reputation Power: 0
|
|
|
Do you mean like this?
[/CODE]
import java.applet.Applet;
import java. awt.*;
public class MyPictures extends Applet {
public void start(){
int rint = (int)(Math.random() * 3);
if (rint == 0){
Image action = getImage(getDocumentBase(),"images/image 3.jpg");
}
else{
getImage(getDocumentBase(),"images/image 1.jpg");
getImage(getDocumentBase(),"images/image 2.jpg");
}
}
public void paint(Graphics graph) {
graph.drawString("The Forest of Bent Tress", 475, 30);
resize(1200,900);
}
}
[CODE]
When I run the code, the applet pops up but only with my string of text.
I tried to insert the .drawImage() method after the first image action statement but I received an error saying that graph can't be resolved.
|

March 8th, 2012, 01:34 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
The code tags are at the wrong ends. Swap them.
Please post the full text of the error message.
|

March 8th, 2012, 01:50 PM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 20
Time spent in forums: 8 h 43 m 44 sec
Reputation Power: 0
|
|
Code:
import java.applet.Applet;
import java. awt.*;
public class MyPictures extends Applet {
public void start(){
int rint = (int)(Math.random() * 3);
if (rint == 0){
Image action = getImage(getDocumentBase(),"images/image 3.jpg");
graph.drawImage(action, 50, 50, this);
}
else{
getImage(getDocumentBase(),"images/image 1.jpg");
getImage(getDocumentBase(),"images/image 2.jpg");
}
}
public void paint(Graphics graph) {
graph.drawString("The Forest of Bent Tress", 475, 30);
resize(1200,900);
}
}
I can't copy the error message but all it says is that "graph cannot be resolved"
the error has to do with me placing the .drawImage() method right underneath the first image statement
|

March 8th, 2012, 02:01 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
The graph variable is defined in the paint() method. That is the only place that it is known. You must call the Graphics class's drawImage method in the paint() method.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|