Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Applets Code Examples

Walking Text

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
Walking Text import java.applet.*; import java.awt.*; public class WalkingText extends Applet implements Runnable { protected String mesg = null; protected int xloc, yloc, width, height, textWidth, textHeight; protected Thread t; protected boolean done = false; /** How long to nap for each move */ protected int napTime = 150; /** Applet Initializer */ public void init() { xloc = 0; width = getSize().width; height = getSize().height; if ((mesg = getParameter("text")) == null) mesg = "Hello World of Java"; String pSize = getParameter("fontsize"); if (pSize == null) pSize = "12"; String fontName = getParameter("fontName"); if (fontName == null) fontName = "Helvetica"; // System.out.println("Font is " + pSize + " point " + fontName); Font f = new Font(fontName, Font.PLAIN, Integer.parseInt(pSize)); setFont(f); FontMetrics fm = getToolkit().getFontMetrics(f); textWidth = fm.stringWidth(mesg); textHeight = fm.getHeight(); // System.out.println("TextWidth " + textWidth + ", ht " + textHeight); // use textHeight in y coordinate calculation yloc = height - ((height-textHeight) / 2); } /** This is important: we create a thread, so we must kill it */ public void stop() { done = true; t = null; } /** create the thread and start it. */ public void start() { if (t == null) t = new Thread(this); done = false; t.start(); } // Usage: public String[][] getParameterInfo() { String[][] params = { { "text", "text", "Text to walk across the screen" }, { "fontName", "text", "Name of font to display in" }, { "fontsize", "int", "How big to make the text" }, }; return params; } /** Run is called by the Thread class when there is work to do */ public void run() { while (!done) { if ((xloc+=5) > getSize().width) xloc = 0; repaint(); try { Thread.sleep(napTime); } catch (Exception e) { System.out.println("Who dares to interrupt my Sleep()? " + e); }; } } /** Paint is called by Applet to redraw the screen */ public void paint(Graphics g) { g.drawString(mesg, xloc, yloc); // if ((xloc + textWidth) > getSize().width) { // int negLoc = textWidth-(width-xloc); // System.out.println("xloc, textWidth, negLoc: " + xloc + "," + // textWidth + ", " + negLoc); // g.drawString(mesg, negLoc, yloc); // } } } /* <APPLET CODE="WalkingText" WIDTH=500 HEIGHT=70> <PARAM NAME=FontName Value="Helvetica"> <PARAM NAME=FontSize Value="24"> </APPLET> */