• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Custom-Drawn Component

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
The moustache of a titan! The ad of a flea:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic