Trying to make 3 images become random in an Applet. Need help
I need to have 3 images become random with one having a string of text on it. I get the Applet to run and load the image and text but when I restart, the applet just opens up the same image and string of text.
Code :
import java.applet.Applet;
import java. awt.*;
public class MyPictures extends Applet {
Image action;
public void start(){
action = getImage(getDocumentBase(),"");
int rint = (int)(Math.random() * 3);
if (rint == 0)
{
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, 10, 50, this);
graph.drawString("The Forest of Bent Tress", 475, 30);
resize(1200,900);
}
}
Re: Trying to make 3 images become random in an Applet. Need help
Cross posted at: Need help making 3 images become random. code inside - Dev Shed
Does this code show an image when it is executed?
Re: Trying to make 3 images become random in an Applet. Need help
not at the moment because I don't have any image placed in it but whenever I give it a specific image, the applet just shows that image and the string of text
Re: Trying to make 3 images become random in an Applet. Need help
Quote:
the applet just shows that image
Can you post the code that displays the image?
This code won't show any image:
Code :
import java.applet.Applet;
import java. awt.*;
public class MyPictures extends Applet {
Image action;
public void start(){
action = getImage(getDocumentBase(),"");
int rint = (int)(Math.random() * 3);
if (rint == 0)
{
getImage(getDocumentBase(),"http://www.javaprogrammingforums.com/images/image 3.jpg");
}
else{
getImage(getDocumentBase(),"http://www.javaprogrammingforums.com/images/image 1.jpg");
getImage(getDocumentBase(),"http://www.javaprogrammingforums.com/images/image 2.jpg");
}
}
public void paint(Graphics graph) {
graph.drawImage(action, 10, 50, this);
graph.drawString("The Forest of Bent Tress", 475, 30);
resize(1200,900);
}
}
Re: Trying to make 3 images become random in an Applet. Need help
I'm using the first image this time for an example
Code :
import java.applet.Applet;
import java. awt.*;
public class MyPictures extends Applet {
Image action;
public void start(){
action = getImage(getDocumentBase(),"images/image 1.jpg");
int rint = (int)(Math.random() * 3);
if (rint == 0)
{
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, 10, 50, this);
graph.drawString("The Forest of Bent Tress", 475, 30);
resize(1200,900);
}
}
Re: Trying to make 3 images become random in an Applet. Need help
You call the getImage() method in 4 places. The first place you call it you save what it returns in the action variable. For the other 3 places you ignore what getImage() returns. Try assigning what it returns to the actoin variable in all the places that you call it.
Re: Trying to make 3 images become random in an Applet. Need help
but how do I get the string of text to be only displayed on the third image? the text is also appearing each time i run the applet
Re: Trying to make 3 images become random in an Applet. Need help
Quote:
how do I get the string of text to be only displayed on the third image?l
When you set action to the third image, you need a way to tell the paint method that the text is to be displayed. When action is set to the other images, you should tell paint not to display the text.
One way is to use a boolean variable that you set true when the text is to be displayed and use it in paint() to control when to display the text.
Re: Trying to make 3 images become random in an Applet. Need help
i was given this example but even this code does not make the images random.
Code :
import java.applet.Applet;
import java. awt.*;
public class MyPictures extends Applet {
Image action = null;
public void init(){
int rand = (int) Math.random() * 3;
if(rand == 0){
action = getImage(getDocumentBase(), "image/image3.jpg");
} else if(rand == 1){
action = getImage(getDocumentBase(), "images/image1.jpg");
} else {
action = getImage(getDocumentBase(), "images/image2.jpg");
}
}
public void paint(Graphics g){
if(action != null){
g.drawImage(action, 10, 50, this);
g.drawString("The Forest of Bent Tress", 475, 30);
resize(1200,900);
}
}
}
Re: Trying to make 3 images become random in an Applet. Need help
Quote:
this code does not make the images random.
Please explain.
Add a println statement immediately following the call to random() that prints out the value of rand.
execute the program several times and see what number is printed out. Is it always the same number?
Re: Trying to make 3 images become random in an Applet. Need help
sorry but my course has not covered using println statements yet.
Re: Trying to make 3 images become random in an Applet. Need help
Here is a sample:
System.out.println("var=" + var);
Replace var with any variable you want to print out the value of.
Re: Trying to make 3 images become random in an Applet. Need help
I think it would be a bad idea to use code that I have not been taught on this project.
Any other way around this problem?
Re: Trying to make 3 images become random in an Applet. Need help
Quote:
Any other way around this problem?
Use an interactive debugger so you can see what the code is doing. You must see what the code is doing if you are going to be able to fix it. printlns are the most common way to do it.
If you don't see what the code is doing you will NOT be able to fix it.
What did your instructor explain to you about how to debug your program?
What techniques were given?
It is impossible to write a program and get it to work without being able to debug it.
Re: Trying to make 3 images become random in an Applet. Need help
This was the code I sent to him and his reply was one of the first solutions you told me and nothing else was explained.
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)
{
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, 10, 50, this);
graph.drawString("The Forest of Bent Tress", 475, 30);
resize(1200,900);
}
}
My question to him:
"I can not figure out how to write the image statements after the start() method.
I think I have the right idea, but I seem to be writing the image statements wrong".
his reply:
"When you write the data type in front of a variable, you're declaring it. You only need to declare action once, then you can assign it any value you want.
But in your code, every time you want to *use* action, you've written 'Image' in front of it. On line 13, Java is telling you that you're declaring action for the second time in one block".
Re: Trying to make 3 images become random in an Applet. Need help
If you want to understand what your code is doing when it executes, you must debug it.
One way is using an interactive debugger that is part of some IDEs.
Another way is to use the println statement as I showed you earlier.
If you don't use some technique to see what the code is doing, you will not be able to make it do what you want.
Your choice.
Re: Trying to make 3 images become random in an Applet. Need help
I was able to figure out how to make the images random but now I can't get the text to only go with the bent tree image.
here is the new code:
Code :
import java.applet.Applet;
import java. awt.*;
public class MyPictures extends Applet {
Image action;
public void start(){
int rint = (int)(Math.random() * 3);
if(rint == 0){
action = getImage(getDocumentBase(), "images/image 3.jpg");
} else if(rint == 1){
action = getImage(getDocumentBase(), "images/image 1.jpg");
} else {
action = getImage(getDocumentBase(), "images/image 2.jpg");
}
}
public void paint(Graphics graph) {
if(action != null){
graph.drawImage(action, 10, 50, this);
graph.drawString("The Forest of Bent Tress", 475, 30);
resize(1200,900);
}
}
}
Re: Trying to make 3 images become random in an Applet. Need help
Look back at post #8. You need a flag (a boolean variable for example) that you set when the image is the bent tree and that you can use in the paint method to control the drawing of the text.
Very similar to how you are using the value of the action variable to control the drawing of an image.
Re: Trying to make 3 images become random in an Applet. Need help
The exclamation point is apparently making the image a boolean. can I use this statement as a boolean? I tried inserting the third image but it did not change anything.
Code :
import java.applet.Applet;
import java. awt.*;
public class MyPictures extends Applet {
Image action;
public void start(){
int rint = (int)(Math.random() * 3);
if(rint == 0){
action = getImage(getDocumentBase(), "/images/image 3.jpg");
} else if(rint == 1){
action = getImage(getDocumentBase(), "/images/image 1.jpg");
} else {
action = getImage(getDocumentBase(), "/images/image 2.jpg");
}
}
public void paint(Graphics graph) {
if(action != getImage(getDocumentBase(), "http://www.javaprogrammingforums.com/images/image 3.jpg"));{
graph.drawImage(action, 10, 50, this);
graph.drawString("The Forest of Bent Tress", 475, 30);
resize(1200,900);
}
}
}
Re: Trying to make 3 images become random in an Applet. Need help
That is NOT the way to do it!!!
You code will never show image3 .
Re: Trying to make 3 images become random in an Applet. Need help