This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Custom-Drawn Component

 
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)
 
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!!!
 
My pie came with a little toothpic holding up this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!