This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Implementing repaint() to panel after mouseclick Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Implementing repaint() to panel after mouseclick" Watch "Implementing repaint() to panel after mouseclick" New topic

Implementing repaint() to panel after mouseclick

Victor Mota

Joined: Jun 01, 2010
Posts: 1
I am a beginner with this Java GUI (comp sci has taught only basic basic things) and I am trying to get mouseclick to work on a panel but when the mouseclick method is called the drawings I make in them are not repainted to the panel. How would I go about doing this? And I do know that the mousepressed is working since I have tried doing a System.close(0) with it and it has worked, it is just the graphics that are screwy. Please help!

pete stein

Joined: Feb 23, 2007
Posts: 1561
Your problem is that to do proper graphics programming in Swing you (we) first have to shed many assumptions. For instance you should not extract a JPanel's Graphic object as you're doing it and assuming that it will persist because it won't. Read a tutorial or two on Swing graphics and you'll see that you should do all of your drawing in the JPanel's paintComponent method only and use the Graphics object that has been passed as a parameter (but don't try to save it as again it won't persist).


Some other things you might want to check out: I'd set the preferred size (not the size -- your current JPanel is size 10 x 10) of the drawing panel, and leave the JFrame size alone -- let it decide what is the best size based on its components. I'd pack and set the JFrame visible after adding components not before. I wouldn't set the background color of the JFrame but rather its components or its content pane.

Much luck.
I agree. Here's the link:
subject: Implementing repaint() to panel after mouseclick
It's not a secret anymore!