Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1108

Re: Drawing problem

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.alt.net!news-in-01.newsfeed.easynews.com!easynews.com!easynews!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail
From "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this>
Subject Re: Drawing problem
Message-ID <eseihg$83f$1@registered.motzarella.org> (permalink)
X-Comment-To comp.lang.java.gui
Newsgroups comp.lang.java.gui
In-Reply-To <20059$45ea913d$5448a71c$24173@news.hispeed.ch>
References <20059$45ea913d$5448a71c$24173@news.hispeed.ch>
Content-Type text/plain; charset=IBM437
Content-Transfer-Encoding 8bit
X-Gateway time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92]
Lines 74
Date Wed, 27 Apr 2011 15:31:24 GMT
NNTP-Posting-Host 96.60.20.240
X-Complaints-To news@tds.net
X-Trace newsreading01.news.tds.net 1303918284 96.60.20.240 (Wed, 27 Apr 2011 10:31:24 CDT)
NNTP-Posting-Date Wed, 27 Apr 2011 10:31:24 CDT
Organization TDS.net
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.gui:1108

Show key headers only | View raw


  To: comp.lang.java.gui
Robert Sturzenegger wrote:
>>> I would like to draw a graph in a container. Since the calculation of the 
>>> graph may be very time consuming, I would like to watch how the graph 
>>> builds up.

Michael Rauscher wrote:

>> If it's just for testing purposes, you could turn off Swing's double 
>> buffering. Otherwise Andrew Thompson's method is much preferred.

[Andrew Thompson suggested to remove the time consuming task from the EDT].

Robert Sturzenegger wrote:

> I tried this and it really accomplishes what I want. Since you stated, that 
> this solution should be for testing purposes only and that Andrew Thompson's 
> method would be the preferred one, I also wanted to try his way. However I 
> failed .... Details can be found in a response to his suggestion.

The key is to separate the 'current state' of the drawing from the 
drawing process.

E.g. you might want to use a BufferedImage as offscreen image. 
paintComponent simply draws that image on screen.

private BufferedImage img = new BufferedImage(400, 400,
         BufferedImage.TYPE_INT_ARGB);

public void paintComponent( Graphics g ) {
     g.drawImage( img, 0, 0, null );
}

In theory, you could now manipulate the image and call repaint to show 
the result on screen. In practice, this is possible, too. The only thing 
you'd have to care of, is to manipulate the image on the EDT.

So:

private void calculateAndPaint() {
     for ( int x = 0; x < 200; ++x ) {
         final int i = x;
         SwingUtilities.invokeLater( new Runnable() {
             public void run() {
                 Graphics g = img.createGraphics();
                 g.drawLine( i, i, i-1, i-1 );
                 g.dispose();
                 repaint();
             }
         });
         try { Thread.sleep(10); } catch ( InterruptedException e ) {}
     }
}

The last thing one has to do is to let calculateAndPaint be run on a new 
Thread:

public void paint() {
     Thread t = new Thread( new Runnable() {
         public void run() {
             calculateAndPaint();
         }
     });
     t.setPriority( Thread.NORM_PRIORITY );
     t.start();
}

Bye
Michael

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Drawing problem "Robert Sturzenegger" <robert.sturzenegger@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
  Re: Drawing problem "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
    Re: Drawing problem "Robert Sturzenegger" <robert.sturzenegger@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
  Re: Drawing problem "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
    Re: Drawing problem "Robert Sturzenegger" <robert.sturzenegger@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
      Re: Drawing problem "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
        Re: Drawing problem "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
          Re: Drawing problem "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
            Re: Drawing problem "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
              Re: Drawing problem "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
                Re: Drawing problem "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
                Re: Drawing problem "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
        Re: Drawing problem "Robert Sturzenegger" <robert.sturzenegger@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000

csiph-web