The Artima Developer Community
Sponsored Link

Java Answers Forum
Converting applet into standalone application

1 reply on 1 page. Most recent reply: May 24, 2003 9:35 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Fredrik

Posts: 1
Nickname: fresan
Registered: May, 2003

Converting applet into standalone application Posted: May 21, 2003 1:42 PM
Reply to this message Reply
Advertisement
Hi!
How do I convert this applet into a standalone application?
I've tried putting it into a Frame but I get Nullpointerexception from the init() method.

import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.awt.event.*;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;
 
public class TheApplet extends Applet
    implements Runnable, MouseListener
{
 
    public void init()
    {
        addMouseListener(this);
        displayStatus = false;
        iBufferWidth = getBounds().width;
        iBufferHeight = getBounds().height;
        iBuffer = createImage(iBufferWidth, iBufferHeight);
        gBuffer = iBuffer.getGraphics();
        charsStart_ticks =  4;
        charsSpeed_ticks = 1;
        charsFade_ticks = 32;
        pulseStart_ticks = 0;
        pulseSpeed_ticks = 1;
        pulseFade_ticks = 8;
        bkdColor = new Color(100, 255, 255);
        charColor = new Color(255,200,255);
        pulseColor = new Color(255,255,160);
        calculateFadeValues();
        gBuffer.setFont(Font.decode("SansSerif-bolditalic-20"));
        metrics = gBuffer.getFontMetrics();
        lines = parseTextParameter();
        parsePaddingParameter();
        lineHeight = metrics.getAscent();
        liney = (iBufferHeight / 2 + lineHeight / 2) - (metrics.getLeading() + metrics.getDescent()) / 2;
        pulsey = iBufferHeight / 2 + lineHeight / 2;
        pulseMagnitude = metrics.getAscent();
        ticker = null;
        ticks = 0;
        runningTime = 0;
        currentLine = 0;
        initLine();
    }
    public void start()
    {
        ticker = new Thread(this);
        tickerActive = true;
        ticker.start();
    }
    public void update(Graphics g)
    {
        paint(g);
    }
    public void paint(Graphics g)
    {
        g.drawImage(iBuffer, 0, 0, this);
    }
    public void stop()
    {
        tickerActive = false;
    }
    public void run()
    {
        while(tickerActive) 
        {
            if(ticks >= runningTime)
            {
                ticks = 0;
                currentLine++;
                currentLine %= lines.length;
                initLine();
            }
            renderFrame();
            repaint();
            ticks++;
            try
            {
                Thread.sleep(50);
            }
            catch(InterruptedException _ex) { }
        }
    }
    public void calculateFadeValues()
    {
        charFadeColors = new Color[charsFade_ticks];
        pulseFadeColors = new Color[pulseFade_ticks];
        for(int k1 = 0; k1 < charsFade_ticks; k1++)
        {
            int i = (bkdColor.getRed() * k1 + charColor.getRed() * (charsFade_ticks - k1)) / charsFade_ticks;
            int k = (bkdColor.getGreen() * k1 + charColor.getGreen() * (charsFade_ticks - k1)) / charsFade_ticks;
            int i1 = (bkdColor.getBlue() * k1 + charColor.getBlue() * (charsFade_ticks - k1)) / charsFade_ticks;
            charFadeColors[k1] = new Color(i, k, i1);
        }
        for(int l1 = 0; l1 < pulseFade_ticks; l1++)
        {
            int j = (bkdColor.getRed() * l1 + pulseColor.getRed() * (pulseFade_ticks - l1)) / pulseFade_ticks;
            int l = (bkdColor.getGreen() * l1 + pulseColor.getGreen() * (pulseFade_ticks - l1)) / pulseFade_ticks;
            int j1 = (bkdColor.getBlue() * l1 + pulseColor.getBlue() * (pulseFade_ticks - l1)) / pulseFade_ticks;
            pulseFadeColors[l1] = new Color(j, l, j1);
        }
    }
    public void renderFrame()
    {
        gBuffer.setColor(bkdColor);
        gBuffer.fillRect(0, 0, iBufferWidth, iBufferHeight);
        int i = (ticks - pulseStart_ticks - (pulseFade_ticks - pulseSpeed_ticks)) / pulseSpeed_ticks;
        int j = (ticks - pulseStart_ticks) / pulseSpeed_ticks;
        i = i >= 0 ? i : 0;
        j = j < chars.length ? j : chars.length - 1;
        for(int i1 = i; i1 <= j; i1++)
        {
            gBuffer.setColor(pulseFadeColors[ticks - pulseStart_ticks - i1 * pulseSpeed_ticks]);
            int l = chars[i1].equals(" ") ? pulsey - pulseMagnitude / 2 : pulsey - pulseMagnitude * (i1 % 2);
            gBuffer.drawLine(charOffsets[i1], pulsey - pulseMagnitude / 2, charOffsets[i1] + charWidths[i1] / 2, l);
            gBuffer.drawLine(charOffsets[i1] + charWidths[i1] / 2, l, charOffsets[i1] + charWidths[i1], pulsey - pulseMagnitude / 2);
        }
 
        i = (ticks - charsStart_ticks - (charsFade_ticks - charsSpeed_ticks)) / charsSpeed_ticks;
        j = (ticks - charsStart_ticks) / charsSpeed_ticks;
        i = i >= 0 ? i : 0;
        j = j < chars.length ? j : chars.length - 1;
        for(int j1 = i; j1 <= j; j1++)
        {
            int k = ticks - charsStart_ticks - j1 * charsSpeed_ticks;
            k = k < 0 ? 0 : k;
            gBuffer.setColor(charFadeColors[k]);
            gBuffer.drawString(chars[j1], charOffsets[j1], liney);
        }
    }
    public void initLine()
    {
        charOffsets = new int[lines[currentLine].length()];
        chars = new String[lines[currentLine].length()];
        charWidths = new int[lines[currentLine].length()];
        int i = iBufferWidth / 2 - metrics.stringWidth(lines[currentLine]) / 2;
        for(int j = 0; j < lines[currentLine].length(); j++)
        {
            charOffsets[j] = i + metrics.stringWidth(lines[currentLine].substring(0, j));
            chars[j] = lines[currentLine].substring(j, j + 1);
            charWidths[j] = metrics.stringWidth(chars[j]);
        }
        int k = (lines[currentLine].length() - 1) * charsSpeed_ticks + charsFade_ticks + charsStart_ticks;
        int l = (lines[currentLine].length() - 1) * pulseSpeed_ticks + pulseFade_ticks + pulseStart_ticks;
        runningTime = k <= l ? l : k;
    }
    public Color parseColorParameter(String s, Color color)
    {
        String s1 = getParameter(s);
        if(s1 == null)
            return color;
        try
        {
            return Color.decode(s1);
        }
        catch(NumberFormatException _ex)
        {
            return decodeColorName(s1, color);
        }
    }
    public int parseIntParameter(String s, int i)
    {
        String s1 = getParameter(s);
        if(s1 == null)
            return i;
        try
        {
            return Integer.parseInt(s1);
        }
        catch(NumberFormatException _ex)
        {
            return i;
        }
    }
    public Color decodeColorName(String s, Color color)
    {
        for(int i = 0; i < HTMLColorNames.length; i++)
            if(s.equalsIgnoreCase(HTMLColorNames[i]))
                return new Color(HTMLColorCodes[i]);
 
        return color;
    }
    public String[] parseTextParameter()
    {
        String s = "text";
        Vector vector = new Vector();
        int i = 0;
        int j;
        do
        {
            j = s.indexOf("\\n", i);
            j = j != -1 ? j : s.length();
            String s1 = s.substring(i, j).trim();
            if(!s1.equals(""))
                vector.addElement(s1);
            i = j + 2;
        } while(j < s.length());
        String as[] = new String[vector.size()];
        for(int k = 0; k < as.length; k++)
            as[k] = (String)vector.elementAt(k);
 
        return as;
    }
    public void parsePaddingParameter()
    {
        String s = getParameter("padding");
        int i = 0;
        boolean flag = false;
        if(s == null)
            i = 10;
        else
        if(s.equalsIgnoreCase("extents"))
            flag = true;
        else
            try
            {
                i = Integer.parseInt(s);
            }
            catch(NumberFormatException _ex)
            {
                i = 10;
            }
        int l = metrics.stringWidth(" ");
        for(int i1 = 0; i1 < lines.length; i1++)
        {
            int j = metrics.stringWidth(lines[i1]);
            int k = l * i * 2;
            if(j < iBufferWidth)
            {
                if(flag || k + j > iBufferWidth)
                    i = (iBufferWidth - j) / l / 2;
                lines[i1] = insertPadding(i) + lines[i1] + insertPadding(i);
            }
        }
    }
    public String insertPadding(int i)
    {
        String s = "";
        for(int j = 0; j < i; j++)
            s = s + " ";
 
        return s;
    }
    public TheApplet()
    {
    }
 
    String lines[];
    int iBufferWidth;
    int iBufferHeight;
    FontMetrics metrics;
    Image iBuffer;
    Graphics gBuffer;
    Thread ticker;
    boolean tickerActive;
    int currentLine;
    int ticks;
    int runningTime;
    int liney;
    int lineHeight;
    int charWidths[];
    int charOffsets[];
    String chars[];
    int pulseMagnitude;
    int pulsey;
    Color charFadeColors[];
    Color pulseFadeColors[];
    Color bkdColor;
    Color charColor;
    Color pulseColor;
    int charsStart_ticks;
    int charsSpeed_ticks;
    int charsFade_ticks;
    int pulseStart_ticks;
    int pulseSpeed_ticks;
    int pulseFade_ticks;
    boolean displayStatus;
    static final String HTMLColorNames[] = {
        "aqua", "black", "blue", "fuschia", "gray", "green", "lime", "maroon", "navy", "olive", 
        "purple", "red", "silver", "teal", "white", "yellow"
    };
    static final int HTMLColorCodes[] = {
        65535, 0, 255, 0xff00ff, 0xbebebe, 32768, 65280, 0x800000, 128, 0x808000, 
        0x800080, 0xff0000, 0xc0c0c0, 32896, 0xffffff, 0xffff00
    };
 
}


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Converting applet into standalone application Posted: May 24, 2003 9:35 AM
Reply to this message Reply
You could leave your applet class and write another class that runs it.

I wrote a java class that does this. Its available at:
http://www.quantumhyperspace.com/SourceBank/AppletRunner.java

You can have it emailed to you by going to this link:
http://www.quantumhyperspace.com/SourceBank/getEmail.jsp?javaFile=AppletRunner.java

Flat View: This topic has 1 reply on 1 page
Topic: Lookup table & multiple jars Previous Topic   Next Topic Topic: Java basics

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use