• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding graphic objects to JPanel

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear JavaRanch,

i recently stepped into the world of java programming leaving my short by enjoyable Python roots behind.

it was not long untill i started to bump into difficulties.

like now for instance, i'm trying to make a ball appear on mouseclick that circles around the cursor.
now i got 2 things that i cant stand.

1, the offset of the ball is odd, its not located in the middle and i tried adding/substracting half of its size to the X,Y.

2, how do i make the ball (or a multiplicity of which) appear without inviking errors like NullPointerExeption.

here comes my code, dont know bricks about text formatting on forums yet

sorry for the mess and the lack of commenting.



and for the Skwer object that i want to make appear.



now where and how do i get n error free ball on my screen by the click of a mouse?
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First things first, the reason why your ball isn't at the center of your cursor is because your mouse listeners are registered to a JFrame object, a JFrame is the whole window frame so the 0,0 coords are actually at the top left corner of the frame (above the java icon). Add the mouse listeners to the Skwer object instead



or make a JPanel inside the JFrame and register the listeners to it instead
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I shall move this discussion to the GUIs forum, where it would fit better.

And welcome to the Ranch )
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you considered a MouseAdapter or MouseMotionAdapter which might avoid your having to write seven methods? Make sure to use the @Override annotation with your overridden methods.
 
frank van schie
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh i see.

i shall try these suggestions when i get access to my machine again!
thanks in advance!
 
frank van schie
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, well that worked.

But i still have no clue on how to add (multiple) Skwer objects on my JPanel.
like uh

for instance i'll make an InvertedSkwer object that moves in the direct opposide direction when i move the mouse
and preferable have it appear when i click the mouse or something.
reply
    Bookmark Topic Watch Topic
  • New Topic