View Java Applet without html and without browser and without Appletviewer? - java

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);
}
}

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

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

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/

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.

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

Related

How To Initialize JavaFX Tookit?

I have a question.
How does one initialize the JavaFX toolkit with the method I found in an earlier StackOverflow Question? The topic can be found here: JavaFX 2.1: Toolkit not initialized
I am trying to use a solution similar to this solution from that thread:
Problem: Non-trivial Swing GUI application needs to run JavaFX components. Application's startup process initializes the GUI after starting up a dependent service layer.
Solutions
Subclass JavaFX Application class and run it in a separate thread e.g.:*
public class JavaFXInitializer extends Application {
#Override
public void start(Stage stage) throws Exception {
// JavaFX should be initialized
someGlobalVar.setInitialized(true);
}
}
The only problem I have is: What do I do with
someGlobalVar.setInitialized(true); ?
I don't know what to fill in there, and some tips would be appreciated :)
I am writing this answer for the comment you have done about how to support (.mp3,.wav,.flac,.ogg) etc in java.For .mp3 you can use JLayer http://www.javazoom.net/projects.html search on web for examples.
About (.mp3,.wav.flac,.ogg) and some more you can use JavaZoom BasicPlayer which uses some external libraries to support them you can download the zip folder here(download without installer and you open the zip folder).
Then go on the folder lib and copy all the .jars except kj_dsp which can be used for visual representation on audio data and contains also a class about fast fourier transform(FFT).Also change MP3_SPI1.9.3 with MP3SPI1.9.4
Then add these .jars into your project libraries and just use:
BasicPlayer player = new BasicPlayer();
The whole thing uses Service Provider Interface (SPI) mechanism.
It runs on a separate thread so you don't have to worry.It works really well but the project is a little bit old.It's a good start!About docs check the website.
import com.sun.javafx.application.PlatformImpl;
public class JavaFXInitializer
{
public JavaFXInitializer()
{
initFx();
}
private synchronized static void initFx() {
PlatformImpl.startup(() -> {
});
}
}

running a processing applet in eclipse

I have downloaded and installed the processing IDE from their official website, then I followed the instructions on how to import the libraries into eclipse. I have core.jar, gluegen-rt.jar and jogl-all.jar added to path. I have the code below which I'm trying to run but when I go to the run menu there is no run options whatsoever. I only see 'run configurations'. Is there anything I'm missing?
package week2;
import processing.core.*;
public class ProcessingTest extends PApplet{
private String URL = "https://c2.staticflickr.com/6/5254/5428199232_c3678ed2ac.jpg";
private PImage backgroundImg;
public void setup()
{
size(800, 800);
backgroundImg = loadImage(URL, "jpg");
backgroundImg.resize(800, 800);
background(backgroundImg);
}
public void draw()
{
}
//public static void main(String args[]){
// PApplet.main(new String[]{"--present", "week2.ProcessingTest"});
//}
}
As of Processing 3, PApplet no longer extends Applet (more info here). In other words, you can't run sketches as an applet anymore.
You'll have to put your main method back in and run it as an application instead.
If you really want to run as an applet, you'll have to create your own class that extends Applet or JApplet, and then add the Processing component to that. That can be pretty convoluted, plus applets are pretty much dead now anyway, so you're probably much better of deploying as an application- or even better, as JavaScript using Processing.js.
As a side note, you shouldn't have to rely on eclipse automatically detecting the run configuration. You should be able to go into the run configurations and create one yourself. That won't work for this specific case, but it's not a bad idea to get more comfortable with the "behind the scenes" stuff so cases like this are less confusing.

Java UNIXProcess not visible in Eclipse?

I tried to use the UNIXProcess.class, but the eclipse keeps warning me that UNIXProcess is not visible.
The code is simple:
import java.lang.Process;
public class Simple {
UNIXProcess process;
}
Can anyone know to use the UNIXProcess?
Thanks
java.lang.UNIXProcess is declared as default visibility which means that it cannot be seen by classes outside of the package java.lang.*. Use ProcessBuilder or Runtime.exec(), instead, to create a valid Process object.

How to run Java .class file from another .class file? (java newb)

I've been running different individual Java .java files in the Netbeans IDE by right-clicking the .java files themselves in the Project Explorer of Netbeans (the portion normally at the upper left part of Netbeans).
However, i've been googling on how to make a class file run another class file using code, but to no avail.
I have a project named "loadanotherfile" with 2 files, namely: Loadanotherfile.java and otherfile.java
I'm trying to make Loadanotherfile.java run otherfile.java, but I'm not exactly sure how. I read about Classloaders and URLClassloaders however these methods don't seem suitable for my purpose of running another .java file.
Below is the code of the 2 files i mentioned.
Loadanotherfile.java
package loadanotherfile;
public class Loadanotherfile {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
// TODO code application logic here
}
}
otherfile.java
package loadanotherfile;
public class otherfile {
public static void main(String args[])
{
System.out.println("This is the other file.");
}
}
I have a feeling that the task has something to do with using the "import" syntax (namely something like import loadanotherfile.* but even if my guess is correct, I'm still not sure on how to make my Loadanotherfile.java run otherfile.java using code.
How can I load otherfile.java using Loadanothefile.java?
Cheers
In Loadanotherfile.java
otherfile.main(args);
Compile the two together, and then from Loadanotherfile,
otherfile.main(args);
will do the trick. You don't need to import since you're in the same package. Note the linked tutorial.
I would investigate (however) class instantiation, and creating an instance of a new class to invoke upon. Invoking static methods from static methods isn't very OO.
Try This:
className.main(Args){
}
This works! ive tested it myself.
Check the public void main line. If there IOException and not there then insert
in Loadanotherfile.java
use this
otherfile.main(args);{
}

Putting custom widget to the Eclipse palette

I am fully aware, this is a very trivial question for many. I have made my research on the web, skimmed hundreds of pages to find a very trivial example, but no success.
All I want is make this simple widget an put it on the eclipse palette.
import javax.swing.JSpinner;
import org.jdom.Element;
public class JxmlJSpinner extends JSpinner{
Element element;
public JxmlJSpinner(Element Aelement) {
super ();
element = Aelement;
}
}
when I export it in the .jar file and try to load it in the "Custom" palette, it throws an exception (some error in class loader).
May anybody take a swift look and correct code above, to make it loadable?
Thanx a lot...

Categories

Resources