Solved

private static final Color not working...

Posted on 2004-10-05
6
297 Views
Last Modified: 2010-03-31
I have created a class called Board, which is to represent a the board for a board game. I want to set the color of the board as a constant, so that any time i want to change the board color, i can do so in one location. I also want all instances of the board class to use this same color.

I assumed that the best way to do it would be similarly to how it is defined in Color.java, and did so in the following way:

private static final Color BOARD_COLOR = new Color(255, 255, 175);

However, when running the program, it draw using a pale offwhite color, regardless of what numbers I use in new Color(). Even when I set it equal to Color.cyan, it still draws the same pale offwhite.

What am I doing wrong? My other uses of private static final seemed to work without difficulty...is there something special about colors?
0
Comment
Question by:iggames
  • 2
  • 2
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12233061
how do you use the BOARD_COLOR?
0
 

Author Comment

by:iggames
ID: 12233253
I call

...
g.setColor(BOARD_COLOR);
...

in the function

private static final BufferedImage createBoardImage()

createBoardImage makes a BufferedImage of proper size, sets color to BOARD_COLOR, and draws the board using that color
0
 
LVL 92

Assisted Solution

by:objects
objects earned 40 total points
ID: 12233288
should be fine, can you post some example code to demonstrate the problem.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 7

Accepted Solution

by:
tomboshell earned 60 total points
ID: 12235940
in the function createBoardImage you always set the color to BOARD_COLOR, both are final static which should be ok.  As I see it you aren't allowing for the color to change.  I assume that you are always recreating the entire board by recalling this method which only sets the one color.  Try introducing a new variable, call it something like currentColor and create some getter/setters for it.  Have this currentColor initally set to the  BOARD_COLOR and then whenever you need a new color call the setter method for the currentColor and recreate/repaint it.  

so...

private static final Color BOARD_COLOR = new Color(255, 255, 175);

private static Color currentColor = BOARD_COLOR;

public static Color getCurrentColor(){
  return currentColor;
}
public static void setCurrentColor(Color c){
  currentColor = c;
}
...
..
.
private static final BufferedImage createBoardImage(){
   // whatever else
   setColor(getCurrentColor());

  // the return image
}
0
 

Author Comment

by:iggames
ID: 12283923
it didn't fix the problem, but i don't feel like spending the time trying to narrow down what the problem is. i only use the color in one line of code, so i just put it in manually there. thank you for your responses, though.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 12284253
Sorry that it wasn't solved that quick.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now