• Post Reply Bookmark Topic Watch Topic
  • New Topic

Graphics overwitten  RSS feed

 
Vincent Delane
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H folks!

I'm new to Java. My project was to draw a simple traffic light using
graphics and Grapic g worked fine. I called the method drawLight();
which drew the light on the frame. When I called JDialogBox, as part of the program, it overwrote the screen and I was unable to redraw it. What method do you call to preserve the screen. I already tried repaint() unless I'm using it wrong.

Help please.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vincent,

The way that things are intended to work is that you implement a special method which paints the screen the way you want it, and Java calls this method as needed. If you're extending a component in the java.awt package, the special method is "public void paint(Graphics)". If you're extending a javax.swing component, then use "public void paintComponent(Graphics)." In the latter case, be sure to call super.paintComponent(Graphics) as the first line of your implementation.

Most people start out trying to force Java to draw things by, for example, calling getGraphics() from a button event handler or from a constructor, and then making graphics calls from those locations. But as you've discovered, if you do that, your drawing gets erased soon enough.

This is more complex than I'd like, but it explains the whole process in detail.
 
Vinny Delane
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info. I will read up and try it out. This message board is a great idea. Thanks again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!