• Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom-Drawn Component

 
John Blanco
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have this chessboard component that is a 512x512 subclass of a JComponent that handles all its drawing itself (squares and pieces).

I lay it out on a null layout manager as follows:

JFrame f = ((JFrame)_screen.getWindow());

// configure controls
f.getContentPane().removeAll();
f.getContentPane().setLayout(null);
f.getContentPane().add(_boardRenderer);

// position
_boardRenderer.setSize(512, 512);
_boardRenderer.setLocation(100, 100);

The problem I have is, though the component is drawn perfectly, the mouse events are handled incorrectly...with the reported mouse clicks being reported as happening about 20 pixels lower and 10 pixels to the right of where they actually are.

I don't get it. I draw a red rectangle around the bounds of the board, and it's exactly where it needs to be. I don't understand this 20/10 offset. Has anyone else seen this when writing a custom, self-drawing component?

(This is J2SE 5)
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if you can modify this to demonstrate the offset behavior
 
John Blanco
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, thank you...

I transplanted in your code and observed the same faulty behavior. After hours, It occurred to me that the weird offset just happened to come awfully close to the size of the borders on the window. Then, I swapped in full-screen support to the screen and it worked!

Turns out, the Window reference was faulty with the windowed version of the code because it included the border as part of the whole window...while m later getGraphics() code did not. Yuck!

So, all patched up...thanks!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!