painting dissappearing during resizing (with mouse)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: painting dissappearing during resizing (with mouse)

  1. #1
    Join Date
    May 2004
    Posts
    2

    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.

    Thanks, Ira

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    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?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    May 2004
    Posts
    2

    Thanks

    Yes, it makes sense, and what I was beginning to conclude as well. I really appreciate your help.
    Ira

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center