•
•
•
•
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
![]() |
•
•
Join Date: Aug 2004
Posts: 11
Reputation:
Rep Power: 5
Solved Threads: 0
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;
}
}
}
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;
}
}
}
This should go in your paint function or any function for which you have a Graphics object available.
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
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- win 98,colours (Windows 9x / Me)
- monitor colours (Monitors, Displays and Video Cards)
- Changing Phpbb forum template (Growing an Online Community)
- How to use different colours? (C)
- ugly colours in IE?!? (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: Java project
- Next Thread: last section of code has to a static class


Linear Mode