This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Applets and the fly likes Convert Swing frame+label to Applet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Convert Swing frame+label to Applet?" Watch "Convert Swing frame+label to Applet?" New topic
Author

Convert Swing frame+label to Applet?

Jerry Kis
Greenhorn

Joined: Oct 12, 2011
Posts: 2
Hi all,

I'm a new visitor to this forum and have already learned some things. I have some (but not lots) of OOP programming experience and little with Swing and applets, although I'm learning.

Right now I'm trying to take some simple animations made with the help of the StdDraw library (http://introcs.cs.princeton.edu/java/stdlib/StdDraw.java.html) and make them into applets so they can be shared on the web. The author gives permission to modify StdDraw, and suggested I get the JLabel from the StdDraw frame and put it into a JApplet. I've tried several ways with no success yet and am stuck. I will appreciate any help.

I added this to StdDraw.java:

There is only one component on the JFrame, a JLabel.

Here is my applet:

Haina Minawa
Ranch Hand

Joined: Oct 13, 2011
Posts: 119
Hi Jerry Kis,


public static JLabel getJLabel() {
Component c = frame.getContentPane();
return (JLabel) c;
}


What's type of the frame object? I guess it is type of JFrame, right? So the above does not work because a JFrame cannot be cast to a JLabel. It will throw an exception at runtime.
What's the purpose of that function, if it is simply to return a JLabel, so the code looks really ugly.
Jerry Kis
Greenhorn

Joined: Oct 12, 2011
Posts: 2
Haina Soft wrote:Hi Jerry Kis,

What's type of the frame object? I guess it is type of JFrame, right? So the above does not work because a JFrame cannot be cast to a JLabel. It will throw an exception at runtime.
What's the purpose of that function, if it is simply to return a JLabel, so the code looks really ugly.


Thank you for your reply. The frame is a JFrame, it's the main window in the StdDraw library which is made to make simple stand-alone graphics. The JFrame only has one component on it, a JLabel that has all the drawing. Earlier I put the test below to confirm that the component being returned is in fact a JLabel.



It always prints that it's a JLabel. I want to somehow put the JLabel into a JApplet, and the getJLabel method is what I came up with. I am very new to Swing and JApplets.
Andrew Thompson
Ranch Hand

Joined: Aug 05, 2010
Posts: 77
    
    5
Jerry Kis wrote:Right now I'm trying to take some simple animations .. so they can be shared on the web. ...


Forget the applet. The perfect solution for this is Java Web Start. There is no code conversion necessary, since JWS can launch frames (as well as applets).
Haina Minawa
Ranch Hand

Joined: Oct 13, 2011
Posts: 119
Jerry Kis wrote:
The frame is a JFrame, it's the main window in the StdDraw library which is made to make simple stand-alone graphics


What does the variable frame come from? I don't see its declaration. Maybe you did not post all your code. Can you post the code of class StdDraw.java?
 
Consider Paul's rocket mass heater.
 
subject: Convert Swing frame+label to Applet?