import java.awt.Graphics; import java.awt.Canvas; import java.awt.Color; import java.io.Serializable; // För mainfunktionen import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class flowtext_c extends Canvas implements Serializable, Runnable { public flowtext_c() { super.setSize( 60, 40); super.setBackground(Color.white); super.setForeground(Color.black); this.setText( "En väldigt lång sträng" ); this.text_width = 100; this.setVertical_step( 10 ); this.setTime_step( 100 ); this.setRunning( false ); } // flowtext_c public void setText( String text ) { this.text = text; } // setFlow_text public String getText() { return this.text; } // getFlow_text public void setTime_step( int time_step ) { this.time_step = time_step; } // setTime_step public int getTime_step() { return this.time_step; } // getFlow_text public void setVertical_step( int vertical_step ) { this.xstep = vertical_step; } // setTime_step public int getVertical_step() { return this.xstep; } // getFlow_text public void setRunning( boolean run_flag ) { this.keep_running = run_flag; if ( this.keep_running && ( this.run_thread == null ) ) { this.run_thread = new Thread( this ); this.run_thread.start(); } else { this.run_thread = null; } // if } // setRunning public boolean getRunning() { return this.keep_running; } // getRunning public void run() { while ( this.keep_running ) { int width = super.getWidth(); this.xpos = this.xpos - this.xstep; if ( ( this.xpos < -this.text_width ) || ( this.xpos > width ) ) { this.xpos = width; } super.repaint(); try { Thread.sleep( this.time_step ); } catch ( InterruptedException exc ) { } } // while } // run public void paint( Graphics gr ) { gr.drawString( this.text, this.xpos, 20 ); } // paint private int xpos, xstep, time_step; private int text_width; private String text; private Thread run_thread; private boolean keep_running; public static void main( String args[] ) { Frame frame = new Frame( "Bean test" ); flowtext_c canvas = new flowtext_c(); canvas.setBounds( 10, 10, 200, 200 ); frame.add( canvas ); frame.pack(); frame.setResizable( false ); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent wev ) { System.exit( 0 ); } // windowClosing } // WindowAdapter ); frame.show(); canvas.setRunning( true ); } // main } // flowtext_c