Reputation: 2549
I'm overriding a JPanel's paintComponent()
method to draw some things on the screen.
This is what it looks like:
I'm using the following code to set the clipping area. This is so that the edges can be curved.
RoundRectangle2D r = new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 100, 150);
g.setClip(r);
Obviously, there is a problem with the right side of the window. Upon further inspection, I found that JPanel's getWidth()
method is not reporting the correct figure. Usually, it's slightly off. For example:
Width = 500, getWidth() = 492
Width = 1000, getWidth() = 992
Width = 100, getWidth() = 192
Width = 350, getWidth() = 342
Width = 600, getWidth() = 592
My question is, why is it so consistently wrong? And why is getHeight()
correct?
If there's no programmatic way to get the real width, should I simply add 8
Upvotes: 0
Views: 6828
Reputation: 347204
Firstly, be careful with clipping. The repaint manager has already set the clipping rectangle when it passes it to your component and you could end up painting outside of the component (if you've created your own buffer, then knock yourself out).
Component sizes are 0 based, that means the the width you want is getWidth() - 1
This is the same concept as collections and arrays.
Also, while I'm here, you're assuming that the window size (say 500) will wrap around you component. It's the other way around. A window of size 500 will produce components smaller then that, because of the window border.
Upvotes: 3