/* *Class TextWriter * *Created by Martin Carlsson * *Internetprogrammering 1 - Course **/ import java.awt.*; import javax.swing.*; /**TextWriter, which is a thread class by implementing the the Runnable interface, is used for *creating dynamic text in the GUI(MagnificentBall). *The dynamic effect is accomplished by sending characters *to the GUI, one character at a time with a certain frequency. *@see MagnificentBall *@see java.lang.Thread */ public class TextWriter implements Runnable { private Thread t; private String titleText = new String("MagnificentBall--not connected..yet"); private String hostText = new String("Enter hostname"); private MagnificentBall gui; private int frequency = 80; private boolean looping = true; public TextWriter(MagnificentBall gui) { this.gui = gui; t = new Thread(this); t.start(); } /** *Sends a substring by a certain frequency to the GUI */ public void run() { boolean doAddress = true; boolean doTitle = true; int i = 0; while(looping) { try { Thread.sleep(frequency); } catch(InterruptedException e) { } try { if(doAddress) gui.setRemoteAdrFieldText( hostText.valueOf(hostText.charAt(i))); } catch(StringIndexOutOfBoundsException e) { doAddress = false; // stops writing to the address field } try { if(doTitle) gui.setTitle(gui.getTitle() + titleText.valueOf(titleText.charAt(i))); } catch(StringIndexOutOfBoundsException e) { doTitle = false; // stops writing to the title bar } i++; if(!doTitle && !doAddress) // when both host address and title text is written the looping stops looping = false; } }// End run } // End class TextWriter