painting dissappearing during resizing (with mouse)
I am relatively new to java and am building a Swing Graphics app. The gui is a JFrame with nested JPanels using a variety of Box layouts. The main drawng algorithm plots geometry in real time (based on user interaction) using a GeneralPath. The app runs continuously(utilizing a Swing Timer) unless the user hits a stop button. The drawing algorithm is in a class called Poly that extends a JComponent. I have tried implementing in 2 different ways.
1. I add an instance of Poly each iteration, which works fine, except that it is really slow, as each JComponent gets added. After a few hundred iterations, the repaint rate is not acceptable.
2. I only instantiate a single Poly object and call some drawing methods. The repaint rate is really fast, but whenever I resize the main application window or change the background of the JPanel, the drawing dissappears. I have removed all paint/paintcomponent methods in all my classes and am using getGraphics() locally in the drawing algoirthm.
This is really, really frustrating and help would be greatly appreciated.
the graphics will be cleared and redrawn when a resize occurs, or when the window is covered and then uncovered. override your paint method so that it paints from an off-screen or invisible buffer..
basically, your bits of code that draw things ONTO the graphics context, should draw ONTO the offscreen context (use a BufferedImage, call createGraphics, and draw on the Graphics2D that is returned. your drawing will affect the bufferedImage object),
when repaint time comes, get the graphics for what you want to draw on, e.g. a jpanel, using getGraphics, then call the drawImage() method on that graphics, and pass in your buffered image
this way, you have a model of data, the buffered image.. you have a controller - the graphics2d that manipulates the BufferedImage, and you have a view by telling some on-view component such as a JPanel to getGraphics().drawImage(the BufferedImage)
does that make sense?
Yes, it makes sense, and what I was beginning to conclude as well. I really appreciate your help.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL