User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,528 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,768 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2412 | Replies: 3
Reply
Join Date: Aug 2004
Posts: 11
Reputation: i_me_roo is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
i_me_roo i_me_roo is offline Offline
Newbie Poster

changing colours

  #1  
Aug 11th, 2004
Hi,

This may be a really basic query, but im not really a java expert! In my program, i am drawing rectangles and the colour of the rectangle depends on a number stored in an array, so if the number is 1 then i want to set the colour to red, if 2 then blue etc. I have to keep changing the colour a lot of times (atleast 10) so instead of writing a switch statement each time i have created the class below. The thins im having trouble calling the method in my main program. I have tried d.decide(A[i]), but its not working. Am i calling it in the wrong way? Any help would be great!

Thanks!

import java.awt.*;
public class colors{

public Color decide(int a){
if(a==1){
return Color.red;
}
if(a==2){
return Color.blue;
}
if(a==3){
return Color.green;
}
if(a==4){
return Color.pink;
}
if(a==5){
return Color.yellow;
}
}
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: changing colours

  #2  
Aug 12th, 2004
Please list the caller's source code. The function you listed does not seem to have any serious flaw.
Reply With Quote  
Join Date: Aug 2004
Posts: 11
Reputation: i_me_roo is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
i_me_roo i_me_roo is offline Offline
Newbie Poster

Re: changing colours

  #3  
Aug 12th, 2004
Thats the problem i am having, could u tell me how i would call it in the main code?
Thanks
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: changing colours

  #4  
Aug 12th, 2004
This should go in your paint function or any function for which you have a Graphics object available.

public void paint(Graphics g) {

    // your class colors... by the way,  this is not java
    // convention to lower case class names.  Should be Colors
    colors colorChooser = new colors();
  
    Color c = colorChooser.decide(A[i])
    g.setColor(c);
    // Color is now set :-)

    // now you can g.drawRect(....) with the correct color.
}

See my post here if you want to know how to use loops to step through your array.
http://www.daniweb.com/techtalkforum...ead.php?t=9203


Hope this helps!

Ed
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 4:20 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC