1

With Swing applications I can use an external class to instantiate and view them.

I'd like to do the same with an Applet, outside of Eclipse, without using the appletviewer.

I want to be able to Run a class MyappletRunner and have its main method kick off the following applet for viewing.

Given the following source code:

import java.applet.*;
import java.awt.*;
public class Myapplet extends Applet{
  String str;
  public void init(){
    str = "This is my first applet";
  }
  public void paint(Graphics g){
    g.drawString(str, 50,50);
  }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
hawkeye
  • 34,745
  • 30
  • 150
  • 304

5 Answers5

2

There is an example shown on If I embed a third-party JApplet in a Swing GUI, how do I pass it parameters?.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I'm tempted to flag it as "not an answer" since it is really just a link to another question + answer, but it simply is the best answer here. – Paŭlo Ebermann Jun 26 '11 at 22:17
  • @My apologies: I was having [trouble](http://meta.stackexchange.com/questions/96373/external-javascript-code-failed-to-load/96477#96477) posting comments until after this was posted. After you get this message, I *will* delete the answer. – Andrew Thompson Jun 27 '11 at 05:44
  • No, let it here. As I said, it is the best answer here (and the other question is not a total duplicate, so we can't really close it). – Paŭlo Ebermann Jun 27 '11 at 12:56
  • I actually wanted to do it without modifying the original applet - I think that's clear in the question. – hawkeye Jun 28 '11 at 09:56
  • Modify the applet? Who said that was necessary? The only reason I added a main to the applet in the linked code was to help with making an SSCCE. It is meant to work with no modification to the applet at all. And as an aside, it uses essentially the same technique outlined @ the java2s link. – Andrew Thompson Jun 28 '11 at 10:06
1

The basic idea is to create your own Swing Frame, add the Applet to your frame, and then pass your Applet an instance of the AppletStub interface.

The best example of it appears to be here: http://www.java2s.com/Code/JavaAPI/java.applet/implementsAppletStub.htm

hawkeye
  • 34,745
  • 30
  • 150
  • 304
0

Appletviewer is the tool you want (if you are just testing that is): http://download.oracle.com/javase/1.3/docs/tooldocs/win32/appletviewer.html

If you want to launch it as a standalone app, the following tutorial works: http://java.sun.com/developer/technicalArticles/Programming/TurningAnApplet/

Mikola
  • 9,176
  • 2
  • 34
  • 41
  • Could you explain the following line on the second link: for ( int i = 0; i *lt; argv.length; i++ ) { – hawkeye Jun 26 '11 at 05:16
  • @hawkeye: argv are the arguments to main which get passed in on the command line. That is a loop which is iterating over each of the inputs to the function. Again, here is the relevant link: http://download.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html – Mikola Jun 26 '11 at 05:18
  • You're saying it should be for ( int i = 0; i < argv.length; i++ ) { – hawkeye Jun 26 '11 at 05:22
  • Hahah, whoops! You are right! I guess they made a typo there when they moved all the pages from sun to oracle. Yes, that is what they mean. – Mikola Jun 26 '11 at 05:30
  • I got that to work - but not without significant rewrites to the main method. I think there are better references online. – hawkeye Jun 26 '11 at 06:25
  • That one is the official version from sun/oracle. I don't really know what else to say other than that maybe you should write them an email with some suggestions. – Mikola Jun 26 '11 at 07:13
0

Not exactly sure why you are trying to do this... what exactly are you going to accomplish by doing this? You say you want to run a java applet... within a java application... AFAIK, it isn't possible anyway. If you just want to view the applet, use appletviewer.

Keelx
  • 907
  • 3
  • 17
  • 26
  • It is possible, the application just needs to pretend to be a browser/Java plugin, i.e. implement the AppletContext interface. See [If I embed a third-party JApplet in a Swing GUI, how do I pass it parameters?](http://stackoverflow.com/questions/6129825/if-i-embed-a-third-party-japplet-in-a-swing-gui-how-do-i-pass-it-parameters/6131260#6131260) (e.g. Andrew's answer) for an example. – Paŭlo Ebermann Jun 26 '11 at 22:16
0

write the folloing code in the begining of the xyz.java file:

/*<applet code="xyz.class" height=30 width=50></applet>*/

Save and compile the xyz.java file.

Now, Execute the file using the appletviewer as following:

c:\jdk\bin>appletviewer xyz.java
cha0site
  • 10,517
  • 3
  • 33
  • 51