• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert Swing frame+label to Applet?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 77
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic