All pastes #344549 Raw Edit

Someone

public java v1 · immutable
·published 2007-02-07 20:55 UTC
rendered paste body
import java.applet.Applet;import java.applet.AppletContext;import java.awt.*;import java.awt.image.ImageObserver;import java.io.PrintStream;import java.net.MalformedURLException;import java.net.URL;import java.util.Hashtable;public class Coalesce extends Applet    implements Runnable{    public void init()    {        app_bg_color = GetParmToColor("AppBGColor", null);        txcount = GetParmToInt("TextCount", 0);        tx = GetParmToString("Text", "");        xoffset = GetParmToInt("XOffset", 0);        yoffset = GetParmToInt("YOffset", -1);        textcolor = GetParmToColor("TextColor", null);        horizcenter = GetParmToBoolean("HorizCenter", false);        vertcenter = GetParmToBoolean("VertCenter", false);        font = GetParmToFont("Font", "Style", "PointSize");        if(txcount > 0)            txa = new String[txcount];        for(int i = 0; i < txcount; i++)            txa[i] = GetParmToString("Text" + (i + 1), "");        chardelay = GetParmToInt("DelayBetweenChars", 20);        rundelay = GetParmToInt("DelayBetweenRuns", 3000);        tile = GetParmToBoolean("AppTile", false);        bgimage_file = GetParmToString("AppBGImage", null);        if(bgimage_file != null)        {            bgimage = process(getDocumentBase(), bgimage_file, false);            prepareImage(bgimage);            bgimage_width = getWidth(bgimage, this);            bgimage_height = getHeight(bgimage, this);        }        setFont(font);        fontmetrics = getFontMetrics(font);        width = fontmetrics.stringWidth(tx);        height = fontmetrics.getHeight();        ascent = fontmetrics.getMaxAscent();        descent = fontmetrics.getDescent();        if(txcount > 0)            widtha = new int[txcount];        for(int j = 0; j < txcount; j++)            widtha[j] = fontmetrics.stringWidth(txa[j]);        if(app_bg_color != null)            setBackground(app_bg_color);        else            app_bg_color = getBackground();        if(textcolor != null)            setForeground(textcolor);        else            textcolor = getForeground();        separated = new char[tx.length()];        xlocation = new int[tx.length()];        xwidth = new int[tx.length()];        if(txcount > 0)        {            separateda = new char[txcount][];            xlocationa = new int[txcount][];            xwidtha = new int[txcount][];        }        for(int k = 0; k < txcount; k++)        {            separateda[k] = new char[txa[k].length()];            xlocationa[k] = new int[txa[k].length()];            xwidtha[k] = new int[txa[k].length()];            txa[k].getChars(0, txa[k].length(), separateda[k], 0);        }        tx.getChars(0, tx.length(), separated, 0);        if(txcount > 0)            total_widtha = new int[txcount];        for(int l = 0; l < txcount; l++)            total_widtha[l] = 0;        for(int i1 = 0; i1 < txcount; i1++)        {            for(int k1 = 0; k1 < txa[i1].length(); k1++)            {                xwidtha[i1][k1] = fontmetrics.charWidth(separateda[i1][k1]);                total_widtha[i1] += xwidtha[i1][k1];                if(k1 + 1 < txa[i1].length())                    xlocationa[i1][k1 + 1] = total_widtha[i1];            }        }        total_width = 0;        for(int j1 = 0; j1 < tx.length(); j1++)        {            xwidth[j1] = fontmetrics.charWidth(separated[j1]);            total_width += xwidth[j1];            if(j1 + 1 < tx.length())                xlocation[j1 + 1] = total_width;        }        offScreen = createImage(size().width, size().height);        offGC = offScreen.getGraphics();        offGC.setFont(font);        steps = GetParmToInt("Steps", 50);        xval = new int[tx.length()];        yval = new int[tx.length()];        calc_new_random();    }    public void paint(Graphics g)    {        if(applet_width != size().width || applet_height != size().height)        {            applet_width = size().width;            applet_height = size().height;            offScreen = createImage(applet_width, applet_height);            offGC = offScreen.getGraphics();            offGC.setFont(font);        }        if(horizcenter)            xoffset = size().width / 2 - total_width / 2;        if(vertcenter)            yoffset = size().height / 2 + ascent / 3;        else        if(yoffset == -1)            yoffset = height - descent;        offGC.setColor(getBackground());        offGC.fillRect(0, 0, applet_width, applet_height);        tilebackground(offGC);        if(Index == 0)        {            offGC.setColor(getBackground());            offGC.fillRect(0, 0, size().width, size().height);        }        offGC.setColor(getBackground());        offGC.fillRect(0, 0, size().width, size().height);        tilebackground(offGC);        offGC.setColor(getForeground());        for(int i = 0; i < tx.length(); i++)            offGC.drawChars(separated, i, 1, calcval(xval[i], xoffset + xlocation[i], Index, steps), calcval(yval[i], yoffset, Index, steps));        g.drawImage(offScreen, 0, 0, this);    }    public void run()    {        while(StringThread != null)         {            if(coalescing)            {                Index++;                if(Index > steps)                {                    Index = steps;                    calc_new_random();                    coalescing = false;                    StringThread = null;                }            } else            {                Index--;                if(Index < 0)                {                    Index = 0;                    coalescing = true;                    StringThread = null;                }            }            repaint();            try            {                Thread.sleep(chardelay);            }            catch(InterruptedException _ex) { }        }    }    public int calcval(int i, int j, int k, int l)    {        int i1;        if(i == j)            i1 = i;        else        if(i > j)            i1 = i - ((i - j) * k) / l;        else            i1 = i + ((j - i) * k) / l;        return i1;    }    public synchronized boolean mouseEnter(Event event, int i, int j)    {        if(StringThread == null)        {            StringThread = new Thread(this);            StringThread.start();        } else        {            coalescing = true;        }        return true;    }    public synchronized boolean mouseExit(Event event, int i, int j)    {        if(StringThread == null)        {            StringThread = new Thread(this);            StringThread.start();        } else        {            coalescing = false;        }        return true;    }    public boolean keyDown(Event event, int i)    {        if(i == 102)        {            chardelay *= 0.90000000000000002D;            if(chardelay < 10)                chardelay = 10;            getAppletContext().showStatus("Setting image delay to: " + chardelay);        } else        if(i == 115)        {            if(chardelay < 10)                chardelay++;            else                chardelay *= 1.1000000000000001D;            getAppletContext().showStatus("Setting image delay to: " + chardelay);        }        return true;    }    public void start()    {        Index = 0;    }    public void calc_new_random()    {        for(int i = 0; i < tx.length(); i++)        {            xval[i] = (int)((Math.random() * 1000D) % (double)size().width);            yval[i] = (int)((Math.random() * 1000D) % (double)size().height);        }    }    public void update(Graphics g)    {        paint(g);    }    public void stop()    {        if(StringThread != null)            StringThread.stop();        StringThread = null;    }    protected void tilebackground(Graphics g)    {        if(bgimage == null)            return;        if(!isImagePrepared(bgimage))            loadImageAndWait(bgimage);        if(app_width != size().width || app_height != size().height)        {            app_width = size().width;            app_height = size().height;            tiled_bgimage = createImage(size().width, size().height);            bg_g = tiled_bgimage.getGraphics();            bg_g.setColor(app_bg_color);            bg_g.fillRect(0, 0, size().width, size().height);            if(tile)            {                for(int i = -bgimage_height; i < app_height; i += bgimage_height)                {                    for(int j = -bgimage_width; j < app_width; j += bgimage_width)                        bg_g.drawImage(bgimage, j + 2, i + 2, this);                }            } else            {                bg_g.drawImage(bgimage, 0, 0, this);            }        }        if(tiled_bgimage != null)            g.drawImage(tiled_bgimage, 0, 0, this);    }    public int GetParmToInt(String s, int i)    {        String s1 = getParameter(s);        if(s1 == null)            return i;        else            return Integer.parseInt(s1);    }    public String GetParmToString(String s, String s1)    {        String s2 = getParameter(s);        if(s2 == null)            return s1;        else            return s2;    }    public boolean GetParmToBoolean(String s, boolean flag)    {        String s1 = getParameter(s);        if(s1 == null)            return flag;        else            return s1.equalsIgnoreCase("true");    }    public Color GetParmToColor(String s, Color color)    {        String s1 = getParameter(s);        if(s1 == null)            return color;        try        {            if(s1.charAt(0) == '#')            {                char ac[] = new char[s1.length()];                s1.getChars(0, s1.length(), ac, 0);                Color color1 = new Color(Integer.parseInt(new String(ac, 1, s1.length() - 1), 16));                return new Color(Integer.parseInt(new String(ac, 1, s1.length() - 1), 16));            } else            {                Color color2 = new Color(Integer.parseInt(s1, 16));                return color2;            }        }        catch(NumberFormatException numberformatexception)        {            String s2 = getColor(s1);            if(s2 != null)            {                return new Color(Integer.parseInt(s2, 16));            } else            {                System.out.println("Bad color specification: " + numberformatexception.getMessage());                return null;            }        }    }    public URL GetParmToURL(String s)    {        URL url = null;        String s1 = getParameter(s);        if(s1 == null)            return null;        try        {            url = new URL(s1);        }        catch(MalformedURLException _ex)        {            url = null;        }        if(url == null)            try            {                url = new URL(getDocumentBase(), s1);            }            catch(MalformedURLException _ex)            {                url = null;            }        if(url == null)            try            {                url = new URL(getCodeBase(), s1);            }            catch(MalformedURLException _ex)            {                url = null;            }        if(url == null)            System.out.println("Unable to load URL: " + s1);        return url;    }    public Font GetParmToFont(String s, String s1, String s2)    {        int i = -1;        Font font2 = getFont();        String s3 = getParameter(s);        if(s3 == null)            s3 = font2.getName();        String s4 = getParameter(s1);        if(s4 == null)            i = font2.getStyle();        if(!s3.equalsIgnoreCase("TimesRoman") && !s3.equalsIgnoreCase("Helvetica") && !s3.equalsIgnoreCase("Courier") && !s3.equalsIgnoreCase("Dialog") && !s3.equalsIgnoreCase("DialogInput") && !s3.equalsIgnoreCase("ZapfDingbats"))            s3 = font2.getName();        if(i == -1)            if(s4.equalsIgnoreCase("bold"))                i = 1;            else            if(s4.equalsIgnoreCase("italic"))                i = 2;            else            if(s4.equalsIgnoreCase("bolditalic"))                i = 3;            else                i = 0;        String s5 = getParameter(s2);        int j;        if(s5 == null)            j = font2.getSize();        else            try            {                j = Integer.parseInt(s5);            }            catch(NumberFormatException _ex)            {                j = font2.getSize();                System.out.println("NumberformatException: " + s5);            }        Font font1 = new Font(s3, i, j);        return font1;    }    public String getColor(String s)    {        if(colors == null)            createHashTable();        return (String)colors.get(s);    }    public void createHashTable()    {        if(colors != null)        {            return;        } else        {            colors = new Hashtable(650);            colors.put("aliceblue", "f0f8ff");            colors.put("antiquewhite", "faebd7");            colors.put("aquamarine", "7fffd4");            colors.put("azure", "f0ffff");            colors.put("beige", "f5f5dc");            colors.put("bisque", "ffe4c4");            colors.put("black", "000000");            colors.put("blanchedalmond", "ffebcd");            colors.put("blue", "0000ff");            colors.put("blueviolet", "8a2be2");            colors.put("brown", "a52a2a");            colors.put("burlywood", "deb887");            colors.put("cadetblue", "5f9ea0");            colors.put("chartreuse", "7fff00");            colors.put("chocolate", "d2691e");            colors.put("coral", "ff7f50");            colors.put("cornflowerblue", "6495ed");            colors.put("cornsilk", "fff8dc");            colors.put("cyan", "00ffff");            colors.put("darkgoldenrod", "b8860b");            colors.put("darkgreen", "006400");            colors.put("darkkhaki", "bdb76b");            colors.put("darkolivegreen", "556b2f");            colors.put("darkorange", "ff8c00");            colors.put("darkorchid", "9932cc");            colors.put("darksalmon", "e9967a");            colors.put("darkseagreen", "8fbc8f");            colors.put("darkslateblue", "483d8b");            colors.put("darkslategray", "2f4f4f");            colors.put("darkslategrey", "2f4f4f");            colors.put("darkturquoise", "00ced1");            colors.put("darkviolet", "9400d3");            colors.put("deeppink", "ff1493");            colors.put("deepskyblue", "00bfff");            colors.put("dimgray", "696969");            colors.put("dimgrey", "696969");            colors.put("dodgerblue", "1e90ff");            colors.put("firebrick", "b22222");            colors.put("floralwhite", "fffaf0");            colors.put("forestgreen", "228b22");            colors.put("green", "00ff00");            colors.put("gainsboro", "dcdcdc");            colors.put("ghostwhite", "f8f8ff");            colors.put("gold", "ffd700");            colors.put("goldenrod", "daa520");            colors.put("gray", "bebebe");            colors.put("honeydew", "f0fff0");            colors.put("hotpink", "ff69b4");            colors.put("indianred", "cd5c5c");            colors.put("ivory", "fffff0");            colors.put("khaki", "f0e68c");            colors.put("lavender", "e6e6fa");            colors.put("lavenderblush", "fff0f5");            colors.put("lawngreen", "7cfc00");            colors.put("lemonchiffon", "fffacd");            colors.put("lightblue", "add8e6");            colors.put("lightcoral", "f08080");            colors.put("lightcyan", "e0ffff");            colors.put("lightgoldenrod", "eedd82");            colors.put("lightgoldenrodyellow", "fafad2");            colors.put("lightgray", "d3d3d3");            colors.put("lightgrey", "d3d3d3");            colors.put("lightpink", "ffb6c1");            colors.put("lightsalmon", "ffa07a");            colors.put("lightseagreen", "20b2aa");            colors.put("lightskyblue", "87cefa");            colors.put("lightslateblue", "8470ff");            colors.put("lightslategray", "778899");            colors.put("lightslategrey", "778899");            colors.put("lightsteelblue", "b0c4de");            colors.put("lightyellow", "ffffe0");            colors.put("limegreen", "32cd32");            colors.put("linen", "faf0e6");            colors.put("magenta", "ff00ff");            colors.put("maroon", "b03060");            colors.put("mediumaquamarine", "66cdaa");            colors.put("mediumblue", "0000cd");            colors.put("mediumorchid", "ba55d3");            colors.put("mediumpurple", "9370db");            colors.put("mediumseagreen", "3cb371");            colors.put("mediumslateblue", "7b68ee");            colors.put("mediumspringgreen", "00fa9a");            colors.put("mediumturquoise", "48d1cc");            colors.put("mediumvioletred", "c71585");            colors.put("midnightblue", "191970");            colors.put("mintcream", "f5fffa");            colors.put("mistyrose", "ffe4e1");            colors.put("moccasin", "ffe4b5");            colors.put("navajowhite", "ffdead");            colors.put("navy", "000080");            colors.put("navyblue", "000080");            colors.put("oldlace", "fdf5e6");            colors.put("olivedrab", "6b8e23");            colors.put("orange", "ffa500");            colors.put("orangered", "ff4500");            colors.put("orchid", "da70d6");            colors.put("palegoldenrod", "eee8aa");            colors.put("palegreen", "98fb98");            colors.put("paleturquoise", "afeeee");            colors.put("palevioletred", "db7093");            colors.put("papayawhip", "ffefd5");            colors.put("peachpuff", "ffdab9");            colors.put("peru", "cd853f");            colors.put("pink", "ffc0cb");            colors.put("plum", "dda0dd");            colors.put("powderblue", "b0e0e6");            colors.put("purple", "a020f0");            colors.put("red", "ff0000");            colors.put("rosybrown", "bc8f8f");            colors.put("royalblue", "4169e1");            colors.put("saddlebrown", "8b4513");            colors.put("salmon", "fa8072");            colors.put("sandybrown", "f4a460");            colors.put("seagreen", "2e8b57");            colors.put("seashell", "fff5ee");            colors.put("sienna", "a0522d");            colors.put("skyblue", "87ceeb");            colors.put("slateblue", "6a5acd");            colors.put("slategray", "708090");            colors.put("slategrey", "708090");            colors.put("snow", "fffafa");            colors.put("springgreen", "00ff7f");            colors.put("steelblue", "4682b4");            colors.put("tan", "d2b48c");            colors.put("thistle", "d8bfd8");            colors.put("tomato", "ff6347");            colors.put("turquoise", "40e0d0");            colors.put("violet", "ee82ee");            colors.put("violetred", "d02090");            colors.put("wheat", "f5deb3");            colors.put("white", "ffffff");            colors.put("whitesmoke", "f5f5f5");            colors.put("yellow", "ffff00");            colors.put("yellowgreen", "9acd32");            return;        }    }    public Image process(URL url, String s, boolean flag)    {        if(s == null)            return null;        Image image = getImage(url, s);        if(flag)            loadImageAndWait(image);        return image;    }    public boolean isImagePrepared(Image image)    {        boolean flag = prepareImage(image, this);        return flag;    }    public void prepareImage(Image image)    {        boolean flag = prepareImage(image, this);    }    public synchronized void loadImageAndWait(Image image)    {        boolean flag = prepareImage(image, this);        int i;        if(!flag)            while(((i = checkImage(image, this)) & 0x20) == 0)                 try                {                    wait(100L);                }                catch(InterruptedException _ex) { }    }    public synchronized int getWidth(Image image, ImageObserver imageobserver)    {        int i;        while((i = image.getWidth(imageobserver)) == -1)             try            {                wait(100L);            }            catch(InterruptedException _ex) { }        return i;    }    public synchronized int getHeight(Image image, ImageObserver imageobserver)    {        int i;        while((i = image.getHeight(imageobserver)) == -1)             try            {                wait(100L);            }            catch(InterruptedException _ex) { }        return i;    }    public Coalesce()    {        threadSuspended = false;        endofstring = false;        coalescing = true;    }    protected char separated[];    protected char separateda[][];    protected Thread StringThread;    protected int xlocation[];    protected int xlocationa[][];    protected int xwidth[];    protected int xwidtha[][];    protected boolean threadSuspended;    protected int chardelay;    protected int rundelay;    protected boolean endofstring;    protected int total_width;    protected int total_widtha[];    protected boolean thread_running;    protected Color app_bg_color;    protected Image offScreen;    protected Graphics offGC;    protected int applet_width;    protected int applet_height;    protected int app_height;    protected int app_width;    protected String bgimage_file;    protected Image bgimage;    protected boolean tile;    protected int bgimage_height;    protected int bgimage_width;    protected Image tiled_bgimage;    protected Graphics bg_g;    protected String tx;    protected String txa[];    protected int xoffset;    protected int yoffset;    protected Color textcolor;    protected boolean horizcenter;    protected boolean vertcenter;    protected Font font;    protected FontMetrics fontmetrics;    protected boolean underline;    protected int width;    protected int widtha[];    protected int height;    protected int ascent;    protected int descent;    protected int txcount;    protected int Index;    protected int xval[];    protected int yval[];    protected boolean coalescing;    protected int steps;    Hashtable colors;}