Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Custom Button

 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I know that i had similar problem recently and i should to do it yourself but i tried many times and I was looking for solution without success.

I know that is possibility to add various graphics for different state: pressed etc. but i want to create button not only in the shape of a Rectangle or Square. I just want to more graphics control for my swing components.



Method paint is invoked but the Rectangle is not drawn. The Graphics is not displayed but if i change something for parameters component for example

this work

What is wrong ?
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Check out the drawRect API to see what the parameters indicate.
Hint: The button cannot and will not paint outside its own "region"
 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry you have right but if i add size manually that won't work. example:


 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coordinates are in user space. This means that the button's 4 corners are locate at [0, 0] [getWidth(), 0] [getWidth(), getHeight()] [0, getHeight()]

Take it from there.
 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coordinates
If i good understood this article and you my button has this coordinates:
x: 0
y: 0
c.getWidth()
c.getHeight()

but still it is the same. this is my corrected code:

 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably just can't see the drawn rectangle. Maybe you meant to fillRect(...) instead?

And when you get that sorted out, you'll still have a button sans text and icon, without focus or pressed decoration. Are you sure what you want can't be achieved by setting the values contained in Button.gradient in the UIManager?
 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed coord. and instead of draw i used fill and now works. I see that i could used UIManager to get the same effect.
I wonder if there is any possibility to add custom shape to button. For example circle. If i draw circle using MetalButtonUI he will be inside the square or rectangle. Maybe i paste image to be more specific.






btw. Darryl thanks you again
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dawid Skrzypczynski wrote:If i draw circle using MetalButtonUI he will be inside the square or rectangle.


Did you read what Maneesh said?
Hint: The button cannot and will not paint outside its own "region"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic