Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 8th, 2012, 10:13 AM
kvn7 kvn7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 20 kvn7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);
}
}

Reply With Quote
  #2  
Old March 8th, 2012, 10:44 AM
Winters Winters is offline
Super Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jul 2003
Posts: 3,874 Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 17 h 30 m 25 sec
Reputation Power: 2569
Mmm. Remember: JavaScript is not Java but they do taste similar.

Moving to the Java forum!

P.S. Welcome to DevShed!
__________________
[PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

W3Fools - A W3Schools Intervention.

Last edited by Winters : March 8th, 2012 at 10:46 AM.

Reply With Quote
  #3  
Old March 8th, 2012, 11:08 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
Can you explain what your problem is?
If there are errors, copy and paste the full text of the error message here.

Reply With Quote
  #4  
Old March 8th, 2012, 12:01 PM
kvn7 kvn7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 20 kvn7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old March 8th, 2012, 12:20 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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.

Reply With Quote
  #6  
Old March 8th, 2012, 12:30 PM
kvn7 kvn7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 20 kvn7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old March 8th, 2012, 12:35 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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]

Reply With Quote
  #8  
Old March 8th, 2012, 12:44 PM
kvn7 kvn7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 20 kvn7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);
}
}

Reply With Quote
  #9  
Old March 8th, 2012, 12:48 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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.

Reply With Quote
  #10  
Old March 8th, 2012, 12:55 PM
kvn7 kvn7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 20 kvn7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #11  
Old March 8th, 2012, 01:01 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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.

Reply With Quote
  #12  
Old March 8th, 2012, 01:29 PM
kvn7 kvn7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 20 kvn7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #13  
Old March 8th, 2012, 01:34 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
The code tags are at the wrong ends. Swap them.

Please post the full text of the error message.

Reply With Quote
  #14  
Old March 8th, 2012, 01:50 PM
kvn7 kvn7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 20 kvn7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #15  
Old March 8th, 2012, 02:01 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Need help making 3 images become random. code inside

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap