Welcome to Dream.In.Code
Become a Java Expert!

Join 150,140 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,289 people online right now. Registration is fast and FREE... Join Now!




convert applet into standalone application

 
Reply to this topicStart new topic

convert applet into standalone application

gryfja
5 Aug, 2008 - 09:57 PM
Post #1

New D.I.C Head
*

Joined: 5 Aug, 2008
Posts: 1

Hello. I need to convert applet into application. What's wrong. I haven't got Exceptions but it doesn't work. I've a frame but it displays "nothing". It seems to me that somewhere is logical mistake but i can't find it. As an applet it works (visual effect of fire) but as an application not. Please help me...thx


CODE


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.io.*;

public class RamkaApletu extends JFrame
    implements AppletStub, AppletContext
{  
    public RamkaApletu(Applet aAplet)
    {  
       aplet = aAplet;
       Container powZawartosci = getContentPane();
       powZawartosci.add(aplet);
       aplet.setStub(this);
    }

    public void show()
    {
       aplet.init();
       super.show();
       aplet.start();
    }
  
    public boolean isActive(){return true;}
    public URL getDocumentBase(){return null;}
    public URL getCodeBase(){return null;}
    public String getParameter(String nazwa){return "";}
    public AppletContext getAppletContext(){return this;}
    public void appletResize(int szerokosc,int wysokosc){}
    
    public AudioClip getAudioClip(URL url){return null;}
    public Image getImage(URL url){return null;}
    public Applet getApplet(String nazwa){return null;}
    public Enumeration getApplets(){return null;}
    public Iterator getStreamKeys(){return null;}
    public InputStream getStream(String key){return null;}
    public void setStream(String key, InputStream stream){}
    public void showDocument(URL url){}
    public void showDocument(URL url,String cel){}
    public void showStatus(String status){}

    private Applet aplet;
}
]
//*************************************************************

CODE

import java.awt.*;
import java.lang.*;
import java.util.*;
import javax.swing.*;

public class fire extends java.applet.Applet implements Runnable {
  boolean first=true;
  int ROWS = 50;
  int COLS = 64;
  int HIDDEN = 4;
  int ROWS_SEED = 4;
  int ROWS_RESEED = 48;
  int MAX_SEED = 8;
  int PALETTE_SIZE = 64;
  int COOLING_LIMIT = 32;
  int COOLING_ROWS = 42;
  int COOLING_FACTOR = 2;
  java.awt.Color palette[] = new java.awt.Color[PALETTE_SIZE];
  byte Buffer[],Buffer2[];
  String message,textfont;
  int textsize,textX,textY;
  Color textcolor;
  Image offScrImage=null;
  Graphics offScrGC;
  Dimension offScrSize;

  Thread kicker=null;

  public void init() {

    int r,i;
    String aux;
    // Set some constants
    COLS = size().width;
    ROWS = size().height + HIDDEN;
    // Get parameters
    aux="50%";
    if((aux!=null)&&(aux.endsWith("%"))) { aux=aux.substring(0,aux.length()-1); }
    COOLING_LIMIT = (aux==null)?(int)(PALETTE_SIZE*0.5):(int)(PALETTE_SIZE*(Integer.valueOf(aux).intValue())/100);
    aux="50%";
    if((aux!=null)&&(aux.endsWith("%"))) { aux=aux.substring(0,aux.length()-1); }
    COOLING_ROWS = (aux==null)?(int)(ROWS*0.8):(int)(ROWS*(Integer.valueOf(aux).intValue())/100);
    aux="2";
    COOLING_FACTOR= (aux==null)?2:(int)(Integer.valueOf(aux).intValue());
    ROWS_RESEED = (int)(ROWS*0.96);
    // Get text parameters
    //Date dzisiaj=new Date();
    
    aux="";//dzisiaj.toString();    
    message=(aux==null)?"":aux;
    aux ="Helvetica";
    textfont=(aux==null)?"TimesRoman":aux;
    aux="11";
    textsize=(aux==null)?18:(Integer.valueOf(aux).intValue());
    aux="000000";
    textcolor=hexColor(aux,Color.white);
    // Setup buffers;
    Buffer = new byte[COLS*ROWS];
    Buffer2 = new byte[COLS*ROWS];
    // Setup palette
    for(i=0; i<16; ++i)
      palette[i]= new Color(16*i,0,0);
    for(i=0; i<16; ++i)
      palette[16+i] = new Color(255, 16*i, 0);
    for(i=0; i<32; ++i)
      palette[32+i] = new Color(255,255,8*i);
    // Setup text
    Font myFont=new Font(textfont, Font.BOLD, textsize);
    FontMetrics myMetrix=getFontMetrics(myFont);
    int textH=myMetrix.getHeight();
    int textW=myMetrix.stringWidth(message);
    textX=(int)((COLS-textW)/2);
    textY=ROWS-HIDDEN-(int)((ROWS-HIDDEN-textH)/2)-myMetrix.getDescent();
    setFont(myFont);
    // Seed image
    for(r=COLS*(ROWS-ROWS_SEED); r<(ROWS*COLS); ++r) {
      Buffer[r]=(byte)(Math.random()*(PALETTE_SIZE-1));
    }
  }

void MainLoop() {
  int r,a,i;
  for(r=COLS+1;r<(COLS*(ROWS-1))-1;++r) {
    a=Buffer[r-COLS-1]+Buffer[r-COLS]+Buffer[r-COLS+1]+Buffer[r-1]+Buffer[r+1]+
      Buffer[r+COLS-1]+Buffer[r+COLS]+Buffer[r+COLS+1];
//    a=(a>>3)%PALETTE_SIZE;
    a=(a>>3);
    // Cool flames
    if(a<COOLING_LIMIT) {
      if((r<COOLING_ROWS*COLS)&&(a>COOLING_FACTOR)) a-=COOLING_FACTOR;
    }
    Buffer2[r]=(byte)(a);
  }
  // Seed at base
  for(r=COLS*(ROWS_RESEED);r<COLS*(ROWS);++r) {
    a=Buffer2[r];
    Buffer2[r]=(byte)((a-(Math.random()*MAX_SEED))%(PALETTE_SIZE*1.1));
    //Buffer2[r]=(byte)((a-(Math.random()*MAX_SEED))%PALETTE_SIZE);
  }
  // Scroll image
  for(i=0;i<COLS*(ROWS-1);++i)
    Buffer[i]=Buffer2[i+COLS];
}

  public final synchronized void update(Graphics g) {
    // Setup off-screen buffer
    Dimension d=size();
    if((offScrImage==null)||(d.width!=offScrSize.width)||(d.height!=offScrSize.height)) {
      offScrImage=createImage(d.width,d.height);
      offScrSize=d;
      offScrGC=offScrImage.getGraphics();
      offScrGC.setFont(getFont());
    }
    if (offScrGC!=null) {
      offScrGC.fillRect(0,0,d.width,d.height);
      paint(offScrGC);
      g.drawImage(offScrImage,0,0,null);
    }
  }

public void paint(Graphics g) {
    int a;
    Color c;
    // Do main loop  
    MainLoop();
    // Copy buffer to off-screen buffer
    for(int y=0;y<(ROWS-HIDDEN);++y)
      for(int x=0;x<COLS;++x) {
        a=Buffer[y*COLS+x];
        a=a<0?-a:a; // Patch nasty bug
        a=a<(PALETTE_SIZE-1)?(a):(PALETTE_SIZE-1);
        c=palette[a];
        try {
          offScrGC.setColor(c);
          offScrGC.drawLine(x,y,x+1,y);
        } catch (Exception e) { }
      }
    try {
    // Write text
    Date dzisiaj=new Date();
      offScrGC.setColor(textcolor);
      offScrGC.drawString(dzisiaj.toString(),20,textY);
    // Paint off-screen buffer
      g.drawImage(offScrImage,0,0,this);
    } catch (Exception e) { }

  }

  public void start() {
    if (kicker==null) {
      kicker=new Thread(this);
      kicker.setPriority(kicker.MAX_PRIORITY);
      kicker.start();
    }
  }

  public void stop() {
    kicker=null;
  }

  public void run() {
    while(kicker!=null) {
      repaint();
      try {kicker.sleep(15);}
      catch (InterruptedException e) {}
    }
  }

  // Place fire bubble on click
  public boolean mouseDown(java.awt.Event evt, int x, int y) {
    int i;
    i=x+y*COLS;
    if(i>81) {
      Buffer[i]=(byte)255;
      Buffer[i-COLS]=(byte)255;
      Buffer[i+COLS]=(byte)255;
      Buffer[i-1]=(byte)255;
      Buffer[i+1]=(byte)255;
    }
    return true;
  }

  public Color hexColor(String hex, Color std) {
    try {
      Integer rgb=new Integer(0);
      hex.replace('#',' ');
      hex.trim();
      rgb=Integer.valueOf(hex,16);
      return new Color(rgb.intValue());
    } catch (Exception e) {
      return std;
    }
  }
      public static void main(String[]args)
    {
        RamkaApletu ramka=new RamkaApletu(new fire());
        ramka.setSize(400,80);
        ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ramka.show();    
    }
}








User is offlineProfile CardPM
+Quote Post

lordms12
RE: Convert Applet Into Standalone Application
6 Aug, 2008 - 11:16 AM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 322



Thanked: 17 times
Dream Kudos: 225
My Contributions
I am sure this question is asked before
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Convert Applet Into Standalone Application
6 Aug, 2008 - 11:23 PM
Post #3

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Look in the Java tutorial section (left column top)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:13AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month