Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to remove on graphics object from the same method?  RSS feed

 
Adam Miszczak
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I have two rectangles on my screen. Can some one tell me how to remove only one of them using g.dispose or any other way? I'm sure it can be done with g.dispose but I'm not sure how...




Thanks!
 
Adam Miszczak
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, wasn't meant to mark this as resolved.
 
Vince Valentin
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try using an if statement that will only print the rectangle if a certain boolean value is true, then set that value to false when you want it deleted.



Then when you need your program to not draw the rectangle, just set the supposedToBeHere variable to false.

Hope it helps
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest this duscission would fit better in out GUIs forum, so I shall move it.

I would suggest you want a Rectangle object, which might implement a Paintable interface, and that interface has a method like void paintShape(Graphics g);
If you go searching for Rectangle2D, you will find a class which models a rectangle. It is a bit complicated, that you have to decide whether you want the Float version or the Double version, but the Graphics object can paint it directly.
It is a bit more complicated than that; you have to use a Graphics2D object, but the supposed Graphics object is a Graphics2D object too. So you can cast it.

So, you are going to override the paintComponent method, which has protected access. You need to paint the superclass' version first, which means that anything you don't want to paint will vanish. The paintComponent() method is automatically called by the JVM whenever the panel has to be shown, etc., or you can call the panel's repaint() method.
You are going to have some Paintable objects, and paint them. You will end up with something like this:-Now, you can do all sorts of things with a Paintable class. You can have a visible boolean field, and you can use that to decide whether you paint it or not.
 
Adam Miszczak
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys. Both worked well!
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

Which are you going to use? The more object‑oriented one, I hope.
 
Adam Miszczak
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I'll use the the object oriented way more, however I will also use the boolean one for simple tests.
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought to get into the habit of being object‑oriented for the simple things. Then you will get familiar with thinking in objects for the more difficult problems
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!