Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #14049 > unrolled thread

Applet code conversion...

Started bylinus <linus@yahoo.com>
First post2012-04-30 08:50 +0200
Last post2012-04-30 07:08 -0700
Articles 10 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Applet code conversion... linus <linus@yahoo.com> - 2012-04-30 08:50 +0200
    Re: Applet code conversion... Jeff Higgins <jeff@invalid.invalid> - 2012-04-30 06:06 -0400
    Re: Applet code conversion... Lew <noone@lewscanon.com> - 2012-04-30 03:07 -0700
      Re: Applet code conversion... linus <linus@yahoo.com> - 2012-05-01 09:28 +0200
        Re: Applet code conversion... Knute Johnson <nospam@knutejohnson.com> - 2012-05-01 08:44 -0700
          Re: Applet code conversion... linus <linus@yahoo.com> - 2012-05-02 15:45 +0200
            Re: Applet code conversion... Knute Johnson <nospam@knutejohnson.com> - 2012-05-02 16:58 -0700
        Re: Applet code conversion... Roedy Green <see_website@mindprod.com.invalid> - 2012-05-02 15:51 -0700
    Re: Applet code conversion... Tsukino Usagi <usagi@tsukino.ca> - 2012-04-30 20:40 +0900
    Re: Applet code conversion... Roedy Green <see_website@mindprod.com.invalid> - 2012-04-30 07:08 -0700

#14049 — Applet code conversion...

Fromlinus <linus@yahoo.com>
Date2012-04-30 08:50 +0200
SubjectApplet code conversion...
Message-ID<4f9e356a$0$1390$4fafbaef@reader1.news.tin.it>
How an applet code code can be trasformed to an application code ?
I thought that adding a "main" would be enough .... but it is not so 
easy ! Is there an example about this my problem ?
Thank a lot.

[toc] | [next] | [standalone]


#14051

FromJeff Higgins <jeff@invalid.invalid>
Date2012-04-30 06:06 -0400
Message-ID<jnlnsu$bbb$1@dont-email.me>
In reply to#14049
On 04/30/2012 02:50 AM, linus wrote:
> How an applet code code can be trasformed to an application code ?
> I thought that adding a "main" would be enough .... but it is not so
> easy ! Is there an example about this my problem ?
> Thank a lot.

For the simplest case:

import java.awt.BorderLayout;
import java.awt.Frame;
import javax.swing.SwingUtilities;
import usenet.linus.MyApplet;

public class AppletFrame {

   public static void main(String[] args) {
     SwingUtilities.invokeLater(new Runnable() {
           public void run() {
             MyApplet myApplet = new MyApplet();
             Frame myFrame = new Frame("Linus MyApplet Frame");
             myApplet.init();
             myFrame.add(myApplet, BorderLayout.CENTER);
             myFrame.pack();
             myFrame.setVisible(true);
           }
         });
   }
}

[toc] | [prev] | [next] | [standalone]


#14052

FromLew <noone@lewscanon.com>
Date2012-04-30 03:07 -0700
Message-ID<jnlo8d$81j$1@news.albasani.net>
In reply to#14049
linus wrote:
> How an applet code code can be trasformed to an application code ?
> I thought that adding a "main" would be enough .... but it is not so easy ! Is
> there an example about this my problem ?

Did you try a web search?

I found this in five minutes or less:
<http://www.johnloomis.org/cpe101/notes/SwingGUI/combined/combined.html>

FWIW, my search string was "Java combining an applet and application".

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

[toc] | [prev] | [next] | [standalone]


#14071

Fromlinus <linus@yahoo.com>
Date2012-05-01 09:28 +0200
Message-ID<4f9f8fdf$0$1387$4fafbaef@reader1.news.tin.it>
In reply to#14052
Lew ha scritto:
> linus wrote:
>> How an applet code code can be trasformed to an application code ?
>> I thought that adding a "main" would be enough .... but it is not so 
>> easy ! Is
>> there an example about this my problem ?
> 
> Did you try a web search?
> 
> I found this in five minutes or less:
> <http://www.johnloomis.org/cpe101/notes/SwingGUI/combined/combined.html>
> 
> FWIW, my search string was "Java combining an applet and application".
> 

With the code

http://www.johnloomis.org/cpe101/notes/SwingGUI/combined/combined.html

  I get  >>

:174: width is not public in java.awt.Component; cannot be accessed from 
outside package
       { width = ( w >= 0 ? w : 300 ); }

:177: height is not public in java.awt.Component; cannot be accessed 
from outside package
       { height = ( h >= 0 ? h : 200 ); }


Wath have I to do ?
Many thanks .



[toc] | [prev] | [next] | [standalone]


#14076

FromKnute Johnson <nospam@knutejohnson.com>
Date2012-05-01 08:44 -0700
Message-ID<jnp0c4$p3q$1@dont-email.me>
In reply to#14071
On 5/1/2012 12:28 AM, linus wrote:
> Lew ha scritto:
>> linus wrote:
>>> How an applet code code can be trasformed to an application code ?
>>> I thought that adding a "main" would be enough .... but it is not so
>>> easy ! Is
>>> there an example about this my problem ?
>>
>> Did you try a web search?
>>
>> I found this in five minutes or less:
>> <http://www.johnloomis.org/cpe101/notes/SwingGUI/combined/combined.html>
>>
>> FWIW, my search string was "Java combining an applet and application".
>>
>
> With the code
>
> http://www.johnloomis.org/cpe101/notes/SwingGUI/combined/combined.html
>
> I get >>
>
> :174: width is not public in java.awt.Component; cannot be accessed from
> outside package
> { width = ( w >= 0 ? w : 300 ); }
>
> :177: height is not public in java.awt.Component; cannot be accessed
> from outside package
> { height = ( h >= 0 ? h : 200 ); }
>
>
> Wath have I to do ?
> Many thanks .
>
>
>
>

Linus:

The code you provide does run as an application.  Just compile it and 
run it.  It is however way too complex for the task it is attempting.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JPanel implements ActionListener {
     private String state = "";

     public test() {
         setPreferredSize(new Dimension(400,300));
     }

     public void actionPerformed(ActionEvent ae) {
         state = ae.getActionCommand();
         repaint();
     }

     public void paintComponent(Graphics g) {
         g.setColor(getBackground());
         g.fillRect(0,0,getWidth(),getHeight());
         g.setColor(getForeground());
         if (state.equals("Line"))
             g.drawLine(0,0,getWidth(),getHeight());
         else if (state.equals("Oval"))
             g.drawOval(0,0,getWidth(),getHeight());
         else if (state.equals("Rect"))
             g.drawRect(5,5,getWidth()-10,getHeight()-10);
     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 test t = new test();
                 JFrame f = new JFrame("test");
                 f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                 f.add(t,BorderLayout.CENTER);
                 JPanel p = new JPanel();
                 JButton b = new JButton("Line");
                 b.addActionListener(t);
                 p.add(b);
                 b = new JButton("Oval");
                 b.addActionListener(t);
                 p.add(b);
                 b = new JButton("Rect");
                 b.addActionListener(t);
                 p.add(b);
                 f.add(p,BorderLayout.NORTH);
                 f.pack();
                 f.setVisible(true);
             }
         });
     }
}

I deliberately left out the random code because I felt like it.

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#14135

Fromlinus <linus@yahoo.com>
Date2012-05-02 15:45 +0200
Message-ID<4fa139ac$0$1375$4fafbaef@reader2.news.tin.it>
In reply to#14076
Knute Johnson ha scritto:
> On 5/1/2012 12:28 AM, linus wrote:
>> Lew ha scritto:
>>> linus wrote:
>>>> How an applet code code can be trasformed to an application code ?
>>>> I thought that adding a "main" would be enough .... but it is not so
>>>> easy ! Is
>>>> there an example about this my problem ?
>>>
>>> Did you try a web search?
>>>
>>> I found this in five minutes or less:
>>> <http://www.johnloomis.org/cpe101/notes/SwingGUI/combined/combined.html>
>>>
>>> FWIW, my search string was "Java combining an applet and application".
>>>
>>
>> With the code
>>
>> http://www.johnloomis.org/cpe101/notes/SwingGUI/combined/combined.html
>>
>> I get >>
>>
>> :174: width is not public in java.awt.Component; cannot be accessed from
>> outside package
>> { width = ( w >= 0 ? w : 300 ); }
>>
>> :177: height is not public in java.awt.Component; cannot be accessed
>> from outside package
>> { height = ( h >= 0 ? h : 200 ); }
>>
>>
>> Wath have I to do ?
>> Many thanks .
>>
>>
>>
>>
> 
> Linus:
> 
> The code you provide does run as an application.  Just compile it and 
> run it.  It is however way too complex for the task it is attempting.
> 
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> 
> public class test extends JPanel implements ActionListener {
>     private String state = "";
> 
>     public test() {
>         setPreferredSize(new Dimension(400,300));
>     }
> 
>     public void actionPerformed(ActionEvent ae) {
>         state = ae.getActionCommand();
>         repaint();
>     }
> 
>     public void paintComponent(Graphics g) {
>         g.setColor(getBackground());
>         g.fillRect(0,0,getWidth(),getHeight());
>         g.setColor(getForeground());
>         if (state.equals("Line"))
>             g.drawLine(0,0,getWidth(),getHeight());
>         else if (state.equals("Oval"))
>             g.drawOval(0,0,getWidth(),getHeight());
>         else if (state.equals("Rect"))
>             g.drawRect(5,5,getWidth()-10,getHeight()-10);
>     }
> 
>     public static void main(String[] args) {
>         EventQueue.invokeLater(new Runnable() {
>             public void run() {
>                 test t = new test();
>                 JFrame f = new JFrame("test");
>                 f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
>                 f.add(t,BorderLayout.CENTER);
>                 JPanel p = new JPanel();
>                 JButton b = new JButton("Line");
>                 b.addActionListener(t);
>                 p.add(b);
>                 b = new JButton("Oval");
>                 b.addActionListener(t);
>                 p.add(b);
>                 b = new JButton("Rect");
>                 b.addActionListener(t);
>                 p.add(b);
>                 f.add(p,BorderLayout.NORTH);
>                 f.pack();
>                 f.setVisible(true);
>             }
>         });
>     }
> }
> 
> I deliberately left out the random code because I felt like it.
> 




It should be that adding the code below to an applet code ...this will 
be  also an application ...but if java.awt.Component is used will have
the error >>>  width is not public in java.awt.Component; cannot be 
accessed from outside package .


public static void main( String args[] )
    {
       int width, height;

       if ( args.length != 2 ) {  // no command-line arguments
          width = 300;
          height = 200;
       }
       else {
          width = Integer.parseInt( args[ 0 ] );
          height = Integer.parseInt( args[ 1 ] );
       }

       // create window in which applet will execute
       JFrame applicationWindow =
          new JFrame( "An applet running as an application" );

       applicationWindow.addWindowListener(
          new WindowAdapter() {
             public void windowClosing( WindowEvent e )
             {
                System.exit( 0 );
             }
          }
       );

       // create one applet instance
       DrawShapes appletObject = new DrawShapes();
       appletObject.setWidth( width );
       appletObject.setHeight( height );

       // call applet's init and start methods
       appletObject.init();
       appletObject.start();

       // attach applet to center of window
       applicationWindow.getContentPane().add( appletObject );

       // set the window's size
       applicationWindow.setSize( width, height );

       // showing the window causes all GUI components
       // attached to the window to be painted
       applicationWindow.show();
    }

[toc] | [prev] | [next] | [standalone]


#14177

FromKnute Johnson <nospam@knutejohnson.com>
Date2012-05-02 16:58 -0700
Message-ID<jnshn7$2ik$1@dont-email.me>
In reply to#14135
On 5/2/2012 6:45 AM, linus wrote:
> Knute Johnson ha scritto:
>> On 5/1/2012 12:28 AM, linus wrote:
>>> Lew ha scritto:
>>>> linus wrote:
>>>>> How an applet code code can be trasformed to an application code ?
>>>>> I thought that adding a "main" would be enough .... but it is not so
>>>>> easy ! Is
>>>>> there an example about this my problem ?
>>>>
>>>> Did you try a web search?
>>>>
>>>> I found this in five minutes or less:
>>>> <http://www.johnloomis.org/cpe101/notes/SwingGUI/combined/combined.html>
>>>>
>>>>
>>>> FWIW, my search string was "Java combining an applet and application".
>>>>
>>>
>>> With the code
>>>
>>> http://www.johnloomis.org/cpe101/notes/SwingGUI/combined/combined.html
>>>
>>> I get >>
>>>
>>> :174: width is not public in java.awt.Component; cannot be accessed from
>>> outside package
>>> { width = ( w >= 0 ? w : 300 ); }
>>>
>>> :177: height is not public in java.awt.Component; cannot be accessed
>>> from outside package
>>> { height = ( h >= 0 ? h : 200 ); }
>>>
>>>
>>> Wath have I to do ?
>>> Many thanks .
>>>
>>>
>>>
>>>
>>
>> Linus:
>>
>> The code you provide does run as an application. Just compile it and
>> run it. It is however way too complex for the task it is attempting.
>>
>> import java.awt.*;
>> import java.awt.event.*;
>> import javax.swing.*;
>>
>> public class test extends JPanel implements ActionListener {
>> private String state = "";
>>
>> public test() {
>> setPreferredSize(new Dimension(400,300));
>> }
>>
>> public void actionPerformed(ActionEvent ae) {
>> state = ae.getActionCommand();
>> repaint();
>> }
>>
>> public void paintComponent(Graphics g) {
>> g.setColor(getBackground());
>> g.fillRect(0,0,getWidth(),getHeight());
>> g.setColor(getForeground());
>> if (state.equals("Line"))
>> g.drawLine(0,0,getWidth(),getHeight());
>> else if (state.equals("Oval"))
>> g.drawOval(0,0,getWidth(),getHeight());
>> else if (state.equals("Rect"))
>> g.drawRect(5,5,getWidth()-10,getHeight()-10);
>> }
>>
>> public static void main(String[] args) {
>> EventQueue.invokeLater(new Runnable() {
>> public void run() {
>> test t = new test();
>> JFrame f = new JFrame("test");
>> f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
>> f.add(t,BorderLayout.CENTER);
>> JPanel p = new JPanel();
>> JButton b = new JButton("Line");
>> b.addActionListener(t);
>> p.add(b);
>> b = new JButton("Oval");
>> b.addActionListener(t);
>> p.add(b);
>> b = new JButton("Rect");
>> b.addActionListener(t);
>> p.add(b);
>> f.add(p,BorderLayout.NORTH);
>> f.pack();
>> f.setVisible(true);
>> }
>> });
>> }
>> }
>>
>> I deliberately left out the random code because I felt like it.
>>
>
>
>
>
> It should be that adding the code below to an applet code ...this will
> be also an application ...but if java.awt.Component is used will have
> the error >>> width is not public in java.awt.Component; cannot be
> accessed from outside package .
>
>
> public static void main( String args[] )
> {
> int width, height;
>
> if ( args.length != 2 ) { // no command-line arguments
> width = 300;
> height = 200;
> }
> else {
> width = Integer.parseInt( args[ 0 ] );
> height = Integer.parseInt( args[ 1 ] );
> }
>
> // create window in which applet will execute
> JFrame applicationWindow =
> new JFrame( "An applet running as an application" );
>
> applicationWindow.addWindowListener(
> new WindowAdapter() {
> public void windowClosing( WindowEvent e )
> {
> System.exit( 0 );
> }
> }
> );
>
> // create one applet instance
> DrawShapes appletObject = new DrawShapes();
> appletObject.setWidth( width );
> appletObject.setHeight( height );
>
> // call applet's init and start methods
> appletObject.init();
> appletObject.start();
>
> // attach applet to center of window
> applicationWindow.getContentPane().add( appletObject );
>
> // set the window's size
> applicationWindow.setSize( width, height );
>
> // showing the window causes all GUI components
> // attached to the window to be painted
> applicationWindow.show();
> }
>

Linus you really need to get a more modern book that doesn't use the 
deprecated code.  Also we can't tell where your problem is without an 
actual compilable code example and the actual error messages.  Google SSCCE.

I suggest you take a look at the Java Tutorial here;

http://docs.oracle.com/javase/tutorial/

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#14164

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-05-02 15:51 -0700
Message-ID<f9e3q79d1ljhncjdq22ksi8kr9g59b2foe@4ax.com>
In reply to#14071
On Tue, 01 May 2012 09:28:33 +0200, linus <linus@yahoo.com> wrote,
quoted or indirectly quoted someone who said :


>:174: width is not public in java.awt.Component; cannot be accessed from 
>outside package

What can you do in those situations?

1. see if what you want is declared protected. If so, you can extend
the class as use it just as if were package default.

2. look around for some accessors, perhaps in some other place in the
class hierarchy.

3. look for another class with similar functionality.  E.g.
JComponent.

4. Roll your own taking inspiration from the recalcitrant class.

But doing this is not necessary to run an Applet as an Application.

One easy way is to have a width and height constant to put put in
Applet tags or twith an adjust in the surrounding Frame when run as an
Applet.

Please read the material already given you before you ask for more.




-- 
Roedy Green Canadian Mind Products
http://mindprod.com
Programmers love to create simplified replacements for HTML. 
They forget that the simplest language is the one you 
already know. They also forget that their simple little 
markup language will bit by bit become even more convoluted 
and complicated than HTML because of the unplanned way it grows.
.

[toc] | [prev] | [next] | [standalone]


#14054

FromTsukino Usagi <usagi@tsukino.ca>
Date2012-04-30 20:40 +0900
Message-ID<jnltod$97q$1@dont-email.me>
In reply to#14049
On 4/30/2012 3:50 PM, linus wrote:
> How an applet code code can be trasformed to an application code ?
> I thought that adding a "main" would be enough .... but it is not so
> easy ! Is there an example about this my problem ?
> Thank a lot.

Here's what I did. There are 2 files. First, the main class, which has 3 
important properties:

a) main() sets the global variable "applet" to be false.
b) there is an appletEntryPoint() method.
c) There is a managed content pane (vs) which I didn't include code for, 
but basically is responsible for drawing everything. As you see from the 
main.java setup code it's swing, so no big deal here. So I was obviously 
able to use one gui codebase for both app and applet. In fact this used 
to be just an application until I made the above changes to turn it into 
an applet.
public class Screen extends JPanel implements KeyListener, Runnable, 
Serializable {...} is what I used in my game.

Getting the applet on the web was another story. This is just the code 
side. It was hell getting it up and running on the web but thats a 
HTML/JNLP/whatever issue.

Basically the key to doing this is understanding how the two entry 
points can be combined. The main() entry point in the main application 
class (say main() in class Main in Main.java) and the Applet entry 
point. Both entry points must set up a content pane, say, and this 
content pane will contain the gui code. So create your gui, then call 
the code which creates the content pane, this code is the same for both 
classes. Say put it in it's own class MyContentPane() or something. Then 
once the gui code is running in that content pane you are good to go and 
it doesn't matter what entry point you used. The only thing I keep an 
"applet" global for is for things like saving; the applet version can't 
save a file to disk, so this item is greyed out on the menus (etc).

Main.java <--- contains the main *application* class (see below)
MainApplet.java <--- contains the main *applet* class (see below Main.java):

// Main.java
package netwhack;

import ...

public class Main {

     // Execute game
     public static void main(String args[]) {
         applet = false; // global variable

         // Frame setup code.
         try {
             SwingUtilities.invokeAndWait(new Runnable() {
                 public void run() {
                     //Make sure we have nice window decorations.
                     //JFrame.setDefaultLookAndFeelDecorated(true);

                     //Create and set up the window.
                     JFrame frame = new JFrame("Netwhack");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                     //Create and set up the content pane.
                     vs.setOpaque(true); //content panes must be opaque
                     frame.setContentPane(vs);

                     //Display the window.
                     frame.setLocation(100, 100);
                     frame.pack();
                     frame.setVisible(true);
                     frame.requestFocus();
                 }
             });
         } catch (Exception e) {
             System.out.println("Failed to initialize Screen:" + e);
             System.out.flush();
             System.exit(0);
         }

         rungame(engine);

     }

// note; vs and the gui is set up by the applet prior to calling this.
// We just pass the instance of the content pane to our game engine so 
we know which content pane we're updating.
     public static void applet_entry_point(Screen vs) {
         applet = true;
         Engine engine = new Engine();
         engine.vs = vs;
         rungame(engine);
     }

     public static void rungame(Engine engine) {
         //snip
     }
}

=============================================
== Now for MainApplet.java ==================
=============================================

// MainApplet.java
//
// launch Main from an applet context.
//

package whatever;

import ...

public class MainApplet extends JApplet {

     static Screen vs = null;
     /**
      * Initialization method that will be called after the applet
      * is loaded
      * into the browser.
      */

     public void init() {
         //Execute a job on the event-dispatching thread
         //creating this applet's GUI.
         try {
             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {

                 public void run() {
                     createGUI();
                 }
             });
         } catch (Exception e) {
             System.err.println("createGUI didn't successfully complete");
         }

         program_thread(); // run the program.
         setPreferredSize(vs.getPreferredSize());
         setSize(vs.getPreferredSize());
     }

     private void createGUI() {
         vs = new Screen(80, 25, Main.font_filename, 16);
         Container c = getContentPane();
         c.add(vs);
         setPreferredSize(vs.getPreferredSize());
         setSize(vs.getPreferredSize());
     }

     public int main(String args[]) {
         init();
         return 0;
     }

     // runs our procedure in it's own thread so we can exit.
     public void program_thread() {
         Runnable r = new Runnable() {

             public void run() {
                 Netwhack.applet_entry_point(vs);
             }
         };
         Thread t = new Thread(r);
         t.start();
     }

     public void start() {
         setPreferredSize(vs.getPreferredSize());
         setSize(vs.getPreferredSize());
     }

     public void stop() {
     }

     public void destroy() {
     }
}

[toc] | [prev] | [next] | [standalone]


#14056

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-04-30 07:08 -0700
Message-ID<a67tp7dv5n8ag8eij5tqrekv3ohnbul4hg@4ax.com>
In reply to#14049
On Mon, 30 Apr 2012 08:50:20 +0200, linus <linus@yahoo.com> wrote,
quoted or indirectly quoted someone who said :

>How an applet code code can be trasformed to an application code ?
>I thought that adding a "main" would be enough 

see http://mindprod.com/jgloss/applet.html

It will show you how to convert it to a "hybrid" that will run either
way.
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
Programmers love to create simplified replacements for HTML. 
They forget that the simplest language is the one you 
already know. They also forget that their simple little 
markup language will bit by bit become even more convoluted 
and complicated than HTML because of the unplanned way it grows.
.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web