Displaying an Applet in Java - java

So I have two classes.
One is a class called Main. The Main class is supposed to process some data.
Then I have another class called MainApplet, an Applet of course.
How can I display MainApplet from the Main class? This is what I have so far, but the applet does not show:
public class Main {
public static void main(String args[]) {
System.out.println("Starting application.");
MainApplet Main = new MainApplet();
Main.setVisible(true);
Main.show();
} }

How can I display MainApplet from the Main class?
You don't. You display an applet from HTML code.
Are you sure your GUI is in fact an applet? Does it extend JApplet or Applet? If so and you want to show it on the desktop through code, then don't make it an applet but instead display a JFrame. The Java Swing tutorials will show you how to do this: How to use Swing Components
Edit
you state:
Basically I have a Main class that is not an applet. It doesn't extend anything. Then I have another class named MainApplet that is an applet (extends JApplet). I want to run Main first, then display MainApplet after... but I can do it the other way around if needed.
You don't sound like you're running your code from a web page (for some reason you're still keeping this information from us), so the solution is not to use applets for this. Instead create a JFrame. Please check out the tutorials that I've linked to above since an applet is not appropriate for your needs.
You will have a main that creates your GUI, that passes any information into the GUI via constructor or method parameters, and then that tells the GUI to show itself (by calling setVisible(true) if it's a JFrame).

Your mainApplet class must extend Applet. You would then replace your main(...) method with init(...) as this is the method that gets called when you open an Applet. You can test this in most environments such as Eclipse. It order to actually put the Applet on a webpage, you must tell the applet to display using HTML in the page.

Related

Using Processing with Eclipse: close sketch without closing the rest of the windows?

I'm working on a Java project in Eclipse that has a class extending PApplet to run a Processing sketch.
First I have a JFrame login screen, and after the user logs in there, I call
PApplet.main("Game"); //"Game" is the class that extends PApplet
to start the sketch.
Now when the game ends I want to close the sketch window, but not the original JFrame window.
Normally I would call
exit();
in Processing but this closes the entire application (ie all windows).
I have also tried
dispose();
but this does nothing.
I guess I'm looking for something like
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
but for PApplet.
First, you have to get the instance holding your sketch. That means using the runSketch() function instead of calling main() directly:
Game game = new Game();
String[] args = {};
PApplet.runSketch(args, game);
Now that you have a reference to your sketch instance, you can use it to get to the internal window. How you do this depends on which renderer you're using, but you can figure it out using a mix of the Processing JavaDoc and the Processing source code.
Here's an untested example using the default renderer:
PSurface surface = game.getSurface();
SmoothCanvas smoothCanvas = (SmoothCanvas)surface.getNative();
JFrame frame = (JFrame) smoothCanvas.getFrame();
Now that you have the parent window, you can do whatever you want with it, including:
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(false);
Like I said I haven't tested this code, and this is going to depend on exactly which renderer you're using, but this process of using the source code and JavaDoc to figure out what's going on under the hood to get to the underlying window is what you have to do.

Creating screens(windows) without JFrame

I have been looking at the source code of Notch's game Metagun as a guide to making my own. I noticed he never implements JFrame as a means of making a screen for the game to run in. I can't make any sense of his Screen class because I only see a drawImage method for some strings. What other ways can you create a window besides using JFrame in Java?
There are several ways to create windows in Java, but for a game, I believe you're looking for a window without decoration. I'll point you then to JWindow, but it's more rudimentary...
Looking at the source I found on github, he extends Applet and Runnable and creates a generic JFrame within Metagun.main.

JApplet will not launch JComponent in browsers, works in Eclipse

I have coded a game proto-type in Java during my spare-time. This game was merely for my educational purposes only. I have it working fine via a JNLP launch file on the web, as well as on my main machine, via a JFrame.
My main intention is to make this proto-type playable in web-browsers via the use of a JApplet. I have coded a class, called AppletPlayer.java. The intention of this class is to essentially serve as a launcher for my Game's main class. The AppletPlayer.java file is pretty much as follows:
public class AppletPlayer extends JApplet {
private Game myGame_; // This is my game's main class
private boolean started_ = false;
public void init() {}
public void start() {
if (!started_) {
started_ = true;
myGame_ = new Game();
this.setContentPane(myGame_);
myGame_.start() // I set focusable, and enabled to 'true' in the Game's start method
// My Game class has no init method. Just a start method that spawns a new thread, that the game runs in
}
}
Now, the Game class itself extends JComponent, and implements Runnable, KeyListener, and FocusListener. If I launch AppletPlayer via Eclipse it works like a charm in its Applet Viewer. However, when I deploy to the web I see two things:
On a Windows XP machine the Applet loads, stays stuck on the main title screen, never receiving focus, hence never registering any type of user input.
On a Windows 7 Machine the Applet loads, I hear my game's music, but the Applet screen itself renders a plain white box and nothing else.
These issues occur in both IE and Firefox.
I have been perusing Google and StackOverFlow for awhile now, trying to dig up a solution but haven't had any luck. I am a bit unfamiliar with Applets, and was hoping for a nudge in the right direction.
One thing that may be the reason: Swing is not thread-safe, so all changes on the GUI (with includes your setContentPane) should occur in the AWT event dispatch thread. The start() method of an applet is not called on this thread.
Wrap all your GUI-related method calls in an EventQueue.invokeLater(...) call (or invokeAndWait, if you need some results, and SwingUtilities also has these methods, if you prefer) and look if you see some changes.

execute a JApplet from another class?

I'm new to Japplets.. I'm wondering if I have the following...
classA A = new classA();
//launch Japplet here
and classA extends Japplet how would I launch the Japplet?
If it's any help I'm using a Java Bean and I've tried calling classA.init(); although this class does execute when I call this method it just doesn't show my JApplet on display.
Perhaps the init() method?
I suspect you either need to use a browser or an applet viewer to see anything. During development I would recommend an applet viewer as it is easier to get going (fewer security issues).

Where do i implement the main() function in a Swing program?

I was just curious where i should place the main function in a Java Swing program. It seems as If it's just way too short too create a brand new class for.
I would not recommend putting the main method inside of any of your Swing components. It doesn't fit well inside a Swing component because it has nothing to do with the components themselves.
Just create a main method in a separate class. It is alright that it is short.
Mushing the logic for running your program into the display logic seems like too much coupling.
I would not put it in the View class. If you're using MVC, and Swing is the View, then main belongs with Controller. That's the class responsible for starting the app, instantiating the View, and collaborating with Model objects to fulfill the use cases.
The Controller should implement the Listener interfaces, because it responds to Swing events as they occur.
I would not have your View extend JFrame. Make the working bits of Swing extend JPanel. When the Controller instantiates the View, it should create a JFrame, add in the JPanels it needs, register itself as the Listener for all Swing events, and make the JFrame visible. At that point your app is up, running, and ready to go.
Put it into your main JFrame class
public class MyFrame extends JFrame {
public static void main(String args[]) {
new MyFrame();
}
MyFrame() {
// ...
}
}
You can put it in any class. It makes sense to put it in the class that represents your main dialog in your app. There is no need to create a class just for main.

Categories

Resources