Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-01-2008, 12:50 AM
Member
 
Join Date: Mar 2008
Posts: 8
CyberFrog is on a distinguished road
Color objects
hey all,
am a bit stuck here. basically would like to return the color object so am I right in thinking this is just the rgb values? If not then what would the return type be in the method? i.e. in this case I have put int for the rgb values

Also, what would be the syntax of returning the final three integers, seem to only be able to put one i.e. r in this case and not r,g,b?


Code:
package Chapter4; import java.awt.*; import java.util.Random; public class RandomColor { public int randomcolor(int r, int g, int b){ return r; } public static void main (String []args){ RandomColor pickcolor = new RandomColor(); Random generator = new Random(); int num1 = generator.nextInt(255); int num2 = generator.nextInt(255); int num3 = generator.nextInt(255); int randcolor = pickcolor.randomcolor(num1, num2, num3); System.out.println(randcolor); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-01-2008, 01:07 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Below is one solution as far as i understood your problem.

And if you want to return three integers, you can return them as an array in the simplest way (or you can create another object to hold all three integers).

Code:
package Chapter4; import java.awt.*; import java.util.Random; public class RandomColor { int r, g, b; private RandomColor(int r, int g, int b) { this.r = r; this.g = g; this.b = b; } public static RandomColor createRandomColor(){ Random generator = new Random(); int r = generator.nextInt(255); int g = generator.nextInt(255); int b = generator.nextInt(255); RandomColor c = new RandomColor(r,g,b); return c; } public String toString() { return "R: "+r+" G: "+g+" B: "+b; } public static void main (String []args){ RandomColor pickcolor = RandomColor.createRandomColor(); System.out.println(pickColor); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-01-2008, 01:42 AM
Member
 
Join Date: Mar 2008
Posts: 8
CyberFrog is on a distinguished road
Thanks for the reply there, but think my level of coding was already at its limit, now am a bit confused ; )

What does the this.r etc do?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-01-2008, 02:30 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
With 'this' keyword, you are accessing to the class variables. If i do not write 'this' keyword there, i can not change the instance variable of the class. Check parameter names of that method! They are same with class variables' names. In this way, i told compiler which variable to access to eliminate confusion. If you dont want 'this' keyword there just rename method parameters to sth else and remove 'this' keyword.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-01-2008, 02:41 AM
Member
 
Join Date: Mar 2008
Posts: 8
CyberFrog is on a distinguished road
ah okay, thanks for the advice : )
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with switch color Daniel AWT / Swing 2 09-18-2008 09:54 AM
Color a button in Gnome waka New To Java 0 02-13-2008 02:52 AM
A bit of color! tim Java 2D 8 02-12-2008 01:57 AM
How to change TXT color Onclick dave700800 New To Java 1 12-08-2007 03:39 AM
Color of the focued row in a JTable SteM AWT / Swing 2 11-20-2007 08:55 PM


All times are GMT +3. The time now is 08:03 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org