This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JButton's Icon's Background is Black?  RSS feed

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to get a play symbol on a JButton.
I have drawn this using fillPoly(...), but when I create the JButton the space surrounding the triangle is black, I would like it to be the same as the JButton, so that it looks like just the triangle has been drawn on it.

Here is my code...(you need to create an instance of the class though)
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No worries, I fixed it. Thought it was an interesting problem, so I haven't deleted post.
All I had to do was specify BufferedImage.TYPE_INT_ARGB,
I remember using ARGB before to create 'transparent' pixels, and to my amazement it works here.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by colin shuker:
No worries, I fixed it. Thought it was an interesting problem, so I haven't deleted post.
All I had to do was specify BufferedImage.TYPE_INT_ARGB,
I remember using ARGB before to create 'transparent' pixels, and to my amazement it works here.


Thanks for not deleting it. I hope that you never delete a thread just because you figured it out before anyone else answered. I can't count the number of times I have posted a question and was able to figure it out quicker than I thought. I always try to come back and update the thread with the solution. That's how we make this place great.

So, with regards to your solution, what about transparent pixels and TYPE_INT_ARGB made your image appear rather than be black. I don't understand.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I don't really understand ARGB yet, but with RGB, if you specify 0 then thats just black, but in ARGB it comes up as transparent.

If you run my code above, you will have to create an instance of class first, you will see the part of the icon not drawn into shows up as black on the button.
If you then use ARGB instead, you'll see it works fine.

Basically, for my chess application, I have decided to store the images in the classes by saving the pixel data, then building the image from that.
Since the pieces aren't square, eg knight, when you move them on top of another picture, you will want to see the background behind the knight, as you drag it.
But the knights' pixels are stored within a 40 by 40 rectangle, hence a 1600 element array of colors.
If I used RGB colors, then whatever colors I used for the area surrounding knight, I wud still get the whole 40 by 40 square moving with it, whenever knight is dragged.
But by saving the points in the square (exterior to the knight) as 0 using ARGB makes them transparent, and the background shows through.
So thats where I got the idea to change the code in first message to ARGB instead of RGB to get it to work.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!