File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Drawing graphics with if statement ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drawing graphics with if statement ??" Watch "Drawing graphics with if statement ??" New topic
Author

Drawing graphics with if statement ??

Adnan Al-Jehani
Greenhorn

Joined: Mar 02, 2012
Posts: 8

Hello everybody,

I've been trying for the last couple of hours to solve this problem but I failed
I don't wanna post my program's code since it's too long, so I created a similar much simpler version of the problem. Here it is :



So, basically what I want to do is draw the shapes once the user tick the CheckBoxes, and un-draw or delete them once he un-tick the CheckBoxes !!
But as you can see, I keep getting this long ass exception once I tick any of the CheckBoxes :



How can I solve this issue now ?
and if there is a different method/algorithm/way you recommend for doing what I wanna do, please let me know.

Also one more question, now let's say I draw 2 or more shapes using the g object from Graphics class, is it possible to move one of them without affecting the others ? and how exactly will I specify this one item or shape since all of them were drawn using the same object ??





Thanks,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
I can see all sorts of things.
You are calling getContentPane(), which is unnecessary, but you are never using the return value.
You appear to be painting in the paint() method, whereas you ought to override paintComponent. Remember: protected access.
You appear to be adding the same handler to both buttons, never using the no 2 handler.
You do not require a Graphics object (which is, by the way, the null reference you are having trouble with) as a field. You allow the JVM to pass a graphics object reference to the paintComponent method, and you do not usually try to create your own Graphics objects. You appear to hve changed the code since you ran it; the exception occurs on line 43, not 42.
If you want to change the colour of a particular Component, use its seetBackground() or setForeground() methods.

I shall move this discussion to our GUIs forum.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Adnan Al-Jehani wrote: . . . I created a similar much simpler version of the problem.. . .
What a good idea
Also one more question, now let's say I draw 2 or more shapes using the g object from Graphics class, is it possible to move one of them without affecting the others ? . . .thanks,
What about an interface, maybe called Paintable, which instructions the shapes to paint themselves? You pass the Graphics object to that method inside the paintCompoent method.
What about another interface, maybe called Movable, allowing you to alter the x and y positions of the objects? You can alter the location of the Graphics object with its translate methods. You may need to do it like thisYou realise that y coordinates go downwards, and 456 will take your origin out of your display. You can alter the origin by moving it to the middle of the display with translate, or you can use g2.scale(1d, -1d); to make y go upwards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drawing graphics with if statement ??
 
Similar Threads
NoClassDefFoundError(unable to open projects)
Problem reading database.
null pointer
problem clicking jbuttons in jtable
Populate Array of type Double from text file