*
The moose likes Swing / AWT / SWT and the fly likes adding graphic objects to JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "adding graphic objects to JPanel" Watch "adding graphic objects to JPanel" New topic
Author

adding graphic objects to JPanel

frank van schie
Greenhorn

Joined: May 16, 2011
Posts: 7
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?
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
I shall move this discussion to the GUIs forum, where it would fit better.

And welcome to the Ranch )
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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

Joined: May 16, 2011
Posts: 7
oh i see.

i shall try these suggestions when i get access to my machine again!
thanks in advance!
frank van schie
Greenhorn

Joined: May 16, 2011
Posts: 7
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: adding graphic objects to JPanel
 
Similar Threads
Paint program
How to add MouseLsitener to JMenu
How to copy from 4 JTextFields to 1 JTextArea
Movable Objects
How do i add a circles without the black corners