Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

percentage based background scheme for a JPanel

 
thushar kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

my requirement is as follows,

I would like to associate a JPanel with a value. This is in a visual way. For that the idea i had in mind is to have half of the JPanel set with a colour if the value is 50%, 1/4th set if value is 25% and so on. These are small JPanels grouped together to form a grid. So the idea is that the user can guess the value of a grid by looking at the percentage area of the panel thats been coloured. Is there such an instance of JPanel/or similar in Swings?

thanks in advance,
Thushar
 
Darrin Cartwright
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thushar,

You need to create a subclass of JPanel and override the paint() method. Here is a quick example class:



Hope that helps!
 
thushar kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Darrin...this looks really helpful
 
thushar kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi darrin,

your code worked exquisitely...But i discovered a new problem. Let me explain. Consider a Jpanel to represent the duration of 1 hr. The percentage based design was to shade the Jpanel based on how much of the 1 hr is to be represented. The hitch now is that, it may be required to display several ranges within an hour itself. For example 10:10-10:15, 10:20-10:30, 10:45-10:50. Is there any way to implement this. If nothing works out, i am planning to have a combined shade for all these ranges and provide a mouse tool tip to display the details.

do help

thanks,
thushar
 
Darrin Cartwright
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thushar,

You just need a way to represent the ranges in the hour time period. Use a different color for each, and change the paint() method to change colors as needed. This will be easier if you can specify that no ranges overlap.
I would probably create a new class to hold the range information. You can define a maximum number of ranges, an array of colors to use when drawing, add setters and getters for each range that verify they do not overlap, etc... Then, include an instance of the new range class in each panel, and access it in the paint method.

Good luck!

Darrin
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic