HILFE: Applet als Applikation starten!

Hi,

ich habe ein Applet und das würde ich gerne als Applikation starten und bekomme es einfach nicht hin.

Ich habe das ganze Programm (76kb) mal unter http://www.lennyrock.com/java.zip abgelegt.

Wäre nett wenn mir dabei einer helfen könnte!

Auch hallo.

ich habe ein Applet und das würde ich gerne als Applikation
starten und bekomme es einfach nicht hin.

Welches JDK (hier 1.5) ?

Ich habe das ganze Programm (76kb) mal unter
http://www.lennyrock.com/java.zip abgelegt.

…und die zugehörige HTML Seite zum Starten der Applets vergessen ? :wink:

Wäre nett wenn mir dabei einer helfen könnte!

Wie wäre es mit ‚appletviewer‘ ?

HTH
mfg M.L.

Welches JDK (hier 1.5) ?

http://www.lennyrock.com/java.zip abgelegt.

…und die zugehörige HTML Seite zum Starten der Applets
vergessen ? :wink:
Wie wäre es mit ‚appletviewer‘ ?

Ich nutze das aktuelle JDK und ich habe deswegen keine html-Seite angeben, weil ich den ganzen Spass gerne eine main-Methode verpassen würde, das man es entweder als applet oder als applikation nutzen kann.

Habe auch eine main in den Quelltext geschrieben, aber es erstmal wieder auskommentiert, weil es nicht lief.

Mal unter der Annahme, das Simulation.java die Klasse ist, die gestartet werden soll:

 // Aufruf beim Start als Applikation
 public static void main(String args[]) {
 Frame f = new Frame("An universal program");
 // Hinzufügen des Panels zum Frame
 f.add("Center", new BilderLadebildschirm(as, image, this));
 // Setzen der Größe und Anzeigen des Frames
 f.setSize(350, 200);
 f.show();
 }

Was spontan auffällt: Was sind as und image? Woher kommen diese Variablen und was soll da drinstecken?
Und: du kannst nicht this übergeben aus einem statischen Kontext.

Chris

Hi auch.

ich habe ein Applet und das würde ich gerne als Applikation
starten und bekomme es einfach nicht hin.

Ein Applet benötigt einiges an Initialisierung um es zum Leben
zu erwecken. Normalerweise übernimmt das alles der Browser für
Dich. Wenn Du das Applet allerdings Standalone betreiben möchtest,
musst Du Dich um die Initialisierung selbst kümmern.

Da das ganze etwas komplexer ist, bietet es sich an, die Arbeit
in eine eigene Klasse auszulagern. Damit musst Du die
eigentliche Anwendung nicht mit den Applet-Initialisierungsarbeiten
zumüllen und kannst diese Klasse für andere Projekte wiederverwenden.

Das Rezept lautet dann, erstelle einen JFrame

Es folgt eine funktionierende und ausbaufähige
Beispiel-Implementierung AppletFrame :

import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.applet.AudioClip;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JFrame;

/\*\*
 \* Provides a JFrame in which an applet can be embedded so it
 \* can be executed as a stand-alone application.
 \* 
 \* To start the applet as a stand alone application 
 \* place this code in the applet's main class:
 \* 
 \* public MyApplet extends JApplet {
 \* ...
 \* public static void main(String[] args) {
 \* JApplet myApplet = new MyApplet();
 \* AppletFrame appletFrame = new AppletFrame(myApplet);
 \* // init the applet context if desired:
 \* // appletFrame.setCodeBase(new URL(sUrl));
 \* // appletFrame.setAppletParameters(...);
 \* appletFrame.setSize(DEFAULT\_WIDTH, DEFAULT\_HEIGHT);
 \* //appletFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT\_ON\_CLOSE);
 \* //appletFrame.setIconImage(sigosIcon.getImage());
 \* appletFrame.show();
 \* }
 \* }
 \* 
 \* @author Andreas Brand 
 \*/
public class AppletFrame extends JFrame implements AppletContext, AppletStub {
 //////////////////////////////////////////////////////////////////////
 // Constants
 //////////////////////////////////////////////////////////////////////
 private static final long serialVersionUID = 3763095262918424630L;

 //////////////////////////////////////////////////////////////////////
 // Attributes
 //////////////////////////////////////////////////////////////////////
 private Vector \_\_vecApplets = null;
 private Applet \_\_embeddedApplet = null;

 private boolean \_\_blnIsAppletActive = false;
 private HashMap \_\_hAppletParameters = new HashMap();
 private URL \_\_urlDocumentBase = null;
 private URL \_\_urlCodeBase = null;

 //////////////////////////////////////////////////////////////////////
 // Constructors
 //////////////////////////////////////////////////////////////////////
 /\*\*
 \* 
 \* @throws java.awt.HeadlessException
 \*/
 public AppletFrame(Applet appletToEmbed) {
 super();
 String sAppletName = appletToEmbed.getName();
 if (sAppletName != null) {
 this.setTitle(sAppletName);
 }
 \_embedApplet(appletToEmbed);
 }

 /\*\*
 \* @param title
 \* @throws java.awt.HeadlessException
 \*/
 public AppletFrame(Applet appletToEmbed, String title) {
 super(title);
 \_embedApplet(appletToEmbed);
 }

 //////////////////////////////////////////////////////////////////////
 // Methods
 //////////////////////////////////////////////////////////////////////
 // Applet specific setters for the applet environment emulation:
 /\*\*
 \* Sets the URL where the applet code is from.
 \* 
 \* Normally this is retrieved from the codebase attribut in the HTML
 \* page. Here we let it set by the caller to emulate this.
 \*/
 public void setCodeBase(URL urlCodeBase) {
 \_\_urlCodeBase = urlCodeBase;
 }
 /\*\*
 \* Sets the URL of the HTML page where the applet is loaded from.
 \* 
 \* Normally this is retrieved from the calling URL of the web browser.
 \* Here we let it set by the caller to emulate this.
 \*/
 public void setDocumentBase(URL urlDocumentBase) {
 \_\_urlDocumentBase = urlDocumentBase;
 }

 /\*\*
 \* Sets the known applet parameters.
 \* 
 \* Normally these are retieved from the param-Tags in the HTML
 \* page. Here we let it set by the caller to emulate this.
 \*/
 public void setAppletParameters(HashMap hAppletParameters) {
 if (hAppletParameters == null) {
 // delete the old parameters:
 \_\_hAppletParameters = new HashMap(); 
 return;
 }
 \_\_hAppletParameters = (HashMap)hAppletParameters.clone();
 }

 /\*\*
 \* Shows the frame with the embedded applet.
 \* 
 \* The applet gets init()ialized and start()ed here.
 \*/
 public void show() {
 Applet embeddedApplet = getEmbeddedApplet();
 \_setIsAppletActive(true);
 embeddedApplet.init();
 super.show();
 embeddedApplet.start();
 }

 /\*\*
 \* Adds the applet to the frame's content pane
 \* and sets the applet's context (stub). 
 \* 
 \* @param appletToEmbed
 \*/
 protected void \_embedApplet(Applet appletToEmbed) {
 setEmbeddedApplet(appletToEmbed);
 java.awt.Container contentPane = getContentPane();
 contentPane.add(appletToEmbed);
 appletToEmbed.setStub(this);
 }

 /\*\*
 \* Sets the applet to embed in this frame.
 \* 
 \* @param embeddedApplet
 \*/
 public void setEmbeddedApplet(Applet embeddedApplet) {
 \_\_embeddedApplet = embeddedApplet;
 \_\_vecApplets = new Vector();
 \_\_vecApplets.add(embeddedApplet);
 }

 /\* 
 \* Returns the applet that is embedded in this frame.
 \* 
 \* @syntax
 \* Applet embeddedApplet = appletFrame.getEmbeddedApplet();
 \*/
 public Applet getEmbeddedApplet() {
 return \_\_embeddedApplet; 
 }

 /\*\*
 \* Internal setter for the active state of the applet.
 \* 
 \* @param blnIsAppletActive
 \*/
 protected void \_setIsAppletActive(boolean blnIsAppletActive) {
 \_\_blnIsAppletActive = blnIsAppletActive;
 }

 //////////////////////////////////////////////////////////////////////
 // Interface AppletContext implementation
 //////////////////////////////////////////////////////////////////////
 /\* 
 \* NOT YET IMPLEMENTED.
 \*
 \* @see java.applet.AppletContext#showStatus(java.lang.String)
 \*/
 public void showStatus(String sStatus) {
 // TODO: create an own status bar and show this string!
 }

 /\* 
 \* Tries to open the given URL in an external web browser.
 \* 
 \* @syntax
 \* appletFrame.showDocument(url);
 \*
 \* @see java.applet.AppletContext#showDocument(java.net.URL)
 \*/
 public void showDocument(URL url) {
 // TODO: open URL in external browser ...
 }

 /\* 
 \* Tries to open the given URL in an external web browser.
 \* The target argument is ignored here.
 \* 
 \* @see java.applet.AppletContext#showDocument(java.net.URL, java.lang.String)
 \*/
 public void showDocument(URL url, String target) {
 showDocument(url);
 }

 /\* 
 \* @see java.applet.AppletContext#getApplets()
 \*/
 public Enumeration getApplets() {
 if (\_\_vecApplets == null) {
 return null;
 }
 return \_\_vecApplets.elements();
 }

 /\* 
 \* Unsupported.
 \* 
 \* @see java.applet.AppletContext#getStreamKeys()
 \*/
 public Iterator getStreamKeys() {
 return null;
 }

 /\* 
 \* Returns the embedded applet if the given name matches
 \* that of the applet.
 \* 
 \* @see java.applet.AppletContext#getApplet(java.lang.String)
 \*/
 public Applet getApplet(String sAppletName) {
 if (sAppletName == null) {
 return null;
 }
 // Up to now we support only one applet per frame, 
 // so see if the current applet is to return:
 Applet embeddedApplet = getEmbeddedApplet();
 if (embeddedApplet == null) {
 return null;
 }

 String sEmbeddedAppletName = embeddedApplet.getName();
 if (sEmbeddedAppletName == null) {
 return null;
 }

 if (sEmbeddedAppletName.equals(sAppletName)) {
 return embeddedApplet;
 }

 // name not found:
 return null;
 }

 /\* 
 \* Unsupported.
 \* 
 \* @see java.applet.AppletContext#getAudioClip(java.net.URL)
 \*/
 public AudioClip getAudioClip(URL url) {
 return null;
 }

 /\* 
 \* Unsupported.
 \* 
 \* @see java.applet.AppletContext#getImage(java.net.URL)
 \*/
 public Image getImage(URL url) {
 return null;
 }

 /\* 
 \* Unsupported.
 \* 
 \* @see java.applet.AppletContext#getStream(java.lang.String)
 \*/
 public InputStream getStream(String key) {
 return null;
 }

 /\* 
 \* Unsupported.
 \* 
 \* @see java.applet.AppletContext#setStream(java.lang.String, java.io.InputStream)
 \*/
 public void setStream(String key, InputStream stream) throws IOException {
 // TODO Auto-generated method stub

 }

 //////////////////////////////////////////////////////////////////////
 // Interface AppletStub implementation
 //////////////////////////////////////////////////////////////////////
 /\*
 \* @see java.applet.AppletStub#isActive()
 \*/
 public boolean isActive() {
 // TODO: react on init, start, stop, destroy calls on the applet ... 
 return \_\_blnIsAppletActive;
 }

 /\*
 \* Resizes the applet frame with the embedded applet.
 \* 
 \* @see java.applet.AppletStub#appletResize(int, int)
 \*/
 public void appletResize(int iWidth, int iHeight) {
 // will also redraw the frame if it needs to: 
 this.setSize(iWidth, iHeight);
 }

 /\* 
 \* @see java.applet.AppletStub#getAppletContext()
 \*/
 public AppletContext getAppletContext() {
 return this;
 }

 /\* 
 \* @see java.applet.AppletStub#getCodeBase()
 \*/
 public URL getCodeBase() {
 return \_\_urlCodeBase;
 }

 /\* 
 \* @see java.applet.AppletStub#getDocumentBase()
 \*/
 public URL getDocumentBase() {
 return \_\_urlDocumentBase;
 }

 /\* 
 \* @see java.applet.AppletStub#getParameter(java.lang.String)
 \*/
 public String getParameter(String sParameterName) {
 if (sParameterName == null) {
 return null;
 }
 if (!\_\_hAppletParameters.containsKey(sParameterName)) {
 return null;
 }
 return (String)\_\_hAppletParameters.get(sParameterName);
 }
}

Gruß,
-Andreas.

Erst einmal vielen Dank für deine Hilfe, allerdings bekomme ich nun diese Fehlermeldungen:

Exception in thread „main“ java.lang.NullPointerException
at BilderLadebildschirm.(BilderLadebildschirm.java:19)
at Simulation.init(Simulation.java:34)
at AppletFrame.show(AppletFrame.java:137)
at Simulation.main(Simulation.java:319)

Es kann auch gut sein, dass ich einfach noch zu wenig Erfahrung habe.

Hier der Link zu dem Programmpaket:
http://www.lennyrock.com/java.zip

Hi Lenny.

Erst einmal vielen Dank für deine Hilfe, allerdings bekomme
ich nun diese Fehlermeldungen:

Exception in thread "main" java.lang.NullPointerException
 at BilderLadebildschirm.(BilderLadebildschirm.java:19)
 at Simulation.init(Simulation.java:34)
 at AppletFrame.show(AppletFrame.java:137)
 at Simulation.main(Simulation.java:319)

Es kann auch gut sein, dass ich einfach noch zu wenig
Erfahrung habe.

Fehlende Erfahrung hat jeder zu Beginnn.
Wirksame Gegenmittel: Fleiss, Logik und Neugier
(letzteres ist der Treibstoff für die stete
persönliche Weiterbildung).

Fleiss sollte Dich erst mal soweit bringen, Dir die
beanstandeten Zeile mit der NullPointerException
näher anzusehen:

class BilderLadebildschirm extends Panel implements Runnable {
 ...
 BilderLadebildschirm(String as[], Image image, Simulation simulation) {
 numDateien = as.length;
 images = new Image[numDateien];
 img = image;
 main = simulation;
 imageTracker = new MediaTracker(this);
**setSize(img.getWidth(this), img.getHeight(this));**

Die Logik sagt Dir dann wegen der NullPointerException,
dass entweder die Methoden img.getWidth(this) und img.getHeight(this)
null zurück geben oder Dein img erst gar nicht richtig erstellt wird,
also null ist.

Je nachdem, was nun der Fall ist, musst Du weiter
fleissig debuggen und Deine Logik anwenden :wink:

Prinzipiell macht es Sinn alle Input-Parameter einer Methode
zu überprüfen bevor(!) man sie verwendet und bei Problemen
direkt vor Ort eine aussagekräftige Fehlermeldung zu werfen.
Das vereinfacht die Fehlereingrenzung ungemein.

Bsp.

 BilderLadebildschirm(String as[], Image image, Simulation simulation) {
 // check arguments:
 if (as == null || image == null || simulation == null) {
 throw new RuntimeException("BilderLadebildschirm: At least one mandatory argument is missing: as=" + as + ", image=" + image + ", simulation=" + simulation);
 }
 // Argumente sind OK, wir können also damit arbeiten ...
}

Gruß,
-Andreas.

Fehlende Erfahrung hat jeder zu Beginnn.
Wirksame Gegenmittel: Fleiss, Logik und Neugier
(letzteres ist der Treibstoff für die stete
persönliche Weiterbildung).

Das ist auch alles gut und wirklich nett von Euch allen.

Allerdings bekomme ich es trotzdem einfach nicht hin.

Bislang sieht es jetzt so aus: http://www.lennyrock.com/start.html

Bislang sieht es jetzt so aus:
http://www.lennyrock.com/start.html

und die Sources sind unter http://www.lennyrock.com/java.zip

Hi nochmals.

Allerdings bekomme ich es trotzdem einfach nicht hin.

Don’t panic.

Wenn Du mal die Code-Zeile mit der NullPointerException
näher untersucht hättest, wie ich Dir im letzten Posting
geraten habe, hättest Du sehen können, dass

BilderLadebildschirm(String as[], Image image, Simulation simulation)

mit image=null aufgerufen wird.

Das Bild erstellst Du in der Applet-Klasse Simulation:

public void init() {
 ...
 Image image = createImage(350, 200);

Die Java-Doku zu createImage (http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Com…)
sagt wiederum:

Creates an off-screen drawable image 
...
@return an off-screen drawable image, which can be used for double
buffering. **The return value may be null if the  
component is not displayable.** This will always happen if
GraphicsEnvironment.isHeadless() returns true.
@see #isDisplayable
@see GraphicsEnvironment#isHeadless

Im Klartext: So lange Dein Applet noch gar nicht am
Bildschirm angezeigt wird, kannst Du auch keine Bilder
erzeugen!

Lösung:
Die Bilderzeugung von init() (hier ist das Applet noch nicht
geezeichnet!) nach start() auslagern. Damit ist sichergestellt,
dass Dein createImage funktioniert:

public class Simulation 
 extends Applet implements ActionListener, ItemListener {

 public static void main(String[] args) {
 Applet simulation = new Simulation();
 final AppletFrame appletFrame = new AppletFrame(simulation);
 appletFrame.setTitle("Standalone simulation");
 appletFrame.setSize(800, 600);
appletFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT\_ON\_CLOSE);
 appletFrame.show();
 }

 public void init() {
 setBackground(Color.lightGray);
 }

 public void start() {
 String as[] = { "haseWinzig.gif", "haseKlein.gif", "haseMittel.gif",
 "haseGross.gif", "haseRiesig.gif", "wolfWinzig.gif",
 "wolfKlein.gif", "wolfMittel.gif", "wolfGross.gif",
 "wolfRiesig.gif", "gras.gif" };
 Image image = createImage(350, 200);
 monitor = new BilderLadebildschirm(as, image, this);
 monitor.setSize(350, 200);
 add(monitor);
 invalidate();
 repaint();
 }

Und schon klappt’s.

Gruß,
-Andreas.