Article View: borland.public.jbuilder.java.api
Article #1016another drawing problem
From: PeterH
Date: Mon, 29 Apr 2002 00:25
Date: Mon, 29 Apr 2002 00:25
150 lines
5231 bytes
5231 bytes
I have another piece of code that is acting funny which I don't understand.
After I run this code in the IDE, after a few moments (five to ten seconds),
I get a copy of the Exit button at the top of the screen and I have no idea
why it's doing that. Any ideas?
The code simple is in the preliminary stages of getting a string to follow a
sine wave, but there are some basic issues I'm working through first (as you
can very well tell :>). And as I mentioned, I can't for the life of me
figure out why I'm getting a dupe of the Exit button at the top of the
frame, but it's not live.
------------------------------------------------------------
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.lang.InterruptedException;
public class DrawSineWave {
public DrawSineWave(){
}
public static void main( String args[] ){
DrawSineWaveFrame f = new DrawSineWaveFrame();
f.addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent e ){
System.exit( 0 );
}
});
f.show();
}
}
class DrawSineWaveFrame extends JFrame{
public DrawSineWaveFrame(){
super( "DrawSineWaveTitle Goes Here" );
JButton exit = new JButton( "Exit" );
exit.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
System.exit( 0 );
}
});
Container c = this.getContentPane();
c.add( exit, BorderLayout.SOUTH );
DrawSineWavePanel p = new DrawSineWavePanel();
setSize( 800, 600 );
getContentPane().add( p );
}
}
class DrawSineWavePanel extends JPanel{
final int UNITS = 5;
private double x = 0;
private double y = 0;
private Dimension panelSize;
DrawMe drawMe;
public DrawSineWavePanel(){
super();
drawMe = new DrawMe( this );
drawMe.start();
}
public void paintComponent( Graphics g ){
g.translate( UNITS, getHeight()/2);
g.setColor( Color.blue);
int w = getWidth();
int h = getHeight();
g.drawLine( -UNITS, 0, w, 0 );
g.drawLine( 0, (int)h/2, 0, -(int)(h/2));
int marker = 0;
Color markerColor = Color.red;
//x axis
for( int i = 0; i < w; i++ ){
if( i % UNITS == 0 ){
marker = 4 * UNITS;
markerColor = Color.red;
} else {
marker = 2 * UNITS;
markerColor = Color.blue;
}
g.setColor( markerColor);
g.drawLine( i, marker, i, -marker );
if( i % UNITS == 0 ) {
g.drawString( String.valueOf( i ), i - 10, marker + 15 );
}
i += ( 2 * UNITS );
}
//-y axis
for( int i = 0; i < h; i++) {
if( i % UNITS == 0 ){
marker = 4 * UNITS;
markerColor = Color.red;
} else {
marker = 2 * UNITS;
markerColor = Color.blue;
}
g.setColor( markerColor);
g.drawLine( marker, i, -marker, i );
if( i % UNITS == 0 ) {
g.drawString( String.valueOf( i ), marker + 15, i - 10 );
}
i += ( 2 * UNITS );
}
}
}
class DrawMe extends Thread{
final int UNITS = 5;
private Graphics g;
private int x;
private int y;
JPanel pp;
public DrawMe( DrawSineWavePanel p ){
super( "drawme" );
pp = p;
x = (int)p.getWidth() + 10;
y = (int)p.getHeight() / 2;
}
public void run(){
try{
for(;;){
g = pp.getGraphics();
if( g != null ){
if( x > pp.getWidth() ) {
x = 5 * UNITS;
y += ( 5 * UNITS );
}
g.drawString( "Trying this out", x, y);
x += UNITS;
sleep( 250 );
pp.repaint();
}
}// for
}
catch( InterruptedException ie ){
System.err.println("FORCED to stop the this thread");
ie.printStackTrace();
}
System.err.println("DONE the thread looping, about to exit");
}
}
Technical Details
Message-ID:
<3ccc9486$1_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!supernews.com!194.159.246.34.MISMATCH!kibo.news.demon.net!demon!news.maxwell.syr.edu!xmission!news-out.spamkiller.net!propagator2-maxim!propagator-maxim!news-in.spamkiller.net!news02.tsnz.net!newsfeed01.tsnz.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnewsShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,93d7db106da00afd,start X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2002-04-28 17:38:02 PST From: "PeterH" <!Sorry-TooMuchSpam!nope@nope.com> Newsgroups: borland.public.jbuilder.java.api Subject: another drawing problem Date: Sun, 28 Apr 2002 20:25:05 -0400 Lines: 154 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 24.112.10.56 Message-ID: <3ccc9486$1_1@dnews> X-Trace: dnews 1020040326 24.112.10.56 (28 Apr 2002 17:32:06 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!supernews.com!194.159.246.34.MISMATCH!kibo.news.demon.net!demon!news.maxwell.syr.edu!xmission!news-out.spamkiller.net!propagator2-maxim!propagator-maxim!news-in.spamkiller.net!news02.tsnz.net!newsfeed01.tsnz.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnews