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 ??
Joined: Oct 13, 2005
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.
Joined: Oct 13, 2005
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.