Article View: borland.public.jbuilder.java.api
Article #1022Re: another drawing problem
From: John McGrath TeamB
Date: Mon, 29 Apr 2002 04:41
Date: Mon, 29 Apr 2002 04:41
94 lines
3839 bytes
3839 bytes
On Sun, 28 Apr 2002 20:25:05 -0400, PeterH wrote:
BTW, I also noticed the DrawMe class. It is generally not a very good
idea to try to draw outside of the update/paint/paintComponent methods.
If something obscures the drawn area and then it is exposed, the part
that you have drawn directly will not be repainted. Also, doing this
does not play nice with Swing's buffering, so it tends to result in a
lot of flashing.
I would do three things differently:
1) It will work a lot better if you use a transparent component placed
above the DrawSizeWavePanel and do the drawing on that.
2) There is really no need to do the drawing yourself. JLabel will
paint strings very nicely.
3) A javax.swing.Timer works very well when you need to do something at
regular intervals. It is much simpler than creating a separate thread,
and more efficient if you have more than one thing going on, since
Timer will use a single thread for them all. And finally, Timer will
run your action on the AWT event thread, and that is where you should
execute any code that mucks with the UI.
Here is how I would do it: First create a panel that will move the
text (JLabel) around.
class DrawMePanel
extends JPanel
{
private static final int UNITS = 1;
private static final int INTERVAL = 25;
private int x = 0;
private int y = 0;
private JLabel label = new JLabel( "Trying this out" );
private ActionListener updateAction = new ActionListener() {
public void actionPerformed( ActionEvent event ) {
if ( x > getWidth() ) {
Dimension size = label.getSize();
x = - size.width;
y += size.height;
}
x += UNITS;
label.setLocation( x, y );
}
};
Timer timer = new Timer( INTERVAL, updateAction );
public DrawMePanel() {
setOpaque( false );
setLayout( null );
label.setForeground( Color.black );
label.setLocation( x, y );
label.setSize( label.getPreferredSize() );
add( label );
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
}
}
Now place this component above your other component. You can do this
using an OverlayLayout.
DrawSineWavePanel sineWavePanel = new DrawSineWavePanel();
DrawMePanel drawMePanel = new DrawMePanel();
JPanel overlayPanel = new JPanel();
overlayPanel.setLayout( new OverlayLayout( overlayPanel ) );
overlayPanel.add( drawMePanel );
overlayPanel.add( sineWavePanel );
getContentPane().add( overlayPanel, BorderLayout.CENTER );
I think you will find that this runs a *lot* smoother this way.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID:
<MPG.173698e774263cea98b5aa@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.online.be!skynet.be!skynet.be!newsfeed01.tsnz.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnewsReferences:
<3ccc9486$1_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,93d7db106da00afd X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2002-04-28 21:52:34 PST From: John McGrath [TeamB] <ng-b@JPMcGrath.net> Newsgroups: borland.public.jbuilder.java.api Subject: Re: another drawing problem Date: Mon, 29 Apr 2002 00:41:51 -0400 Message-ID: <MPG.173698e774263cea98b5aa@newsgroups.borland.com> References: <3ccc9486$1_1@dnews> X-Newsreader: MicroPlanet Gravity v2.60 NNTP-Posting-Host: 209.92.40.60 X-Trace: dnews 1020055309 209.92.40.60 (28 Apr 2002 21:41:49 -0700) Lines: 94 X-Authenticated-User: jpmcgrath Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.online.be!skynet.be!skynet.be!newsfeed01.tsnz.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnews