Re: Applet code conversion...

Discussion in 'Java' started by Lew, Apr 30, 2012.

  1. Lew

    Lew Guest

    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
     
    Lew, Apr 30, 2012
    #1
    1. Advertising

  2. 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
     
    Knute Johnson, May 1, 2012
    #2
    1. Advertising

  3. Lew

    Roedy Green Guest

    On Tue, 01 May 2012 09:28:33 +0200, linus <> 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.
    ..
     
    Roedy Green, May 2, 2012
    #3
  4. 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
     
    Knute Johnson, May 3, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Love

    applet vs. canvas in applet

    Rob Love, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    653
    Liu, Chunyen
    Jul 7, 2003
  2. Rune Andresen
    Replies:
    2
    Views:
    1,974
    Phil Powell
    Sep 17, 2003
  3. Jeff Higgins

    Re: Applet code conversion...

    Jeff Higgins, Apr 30, 2012, in forum: Java
    Replies:
    0
    Views:
    381
    Jeff Higgins
    Apr 30, 2012
  4. Tsukino Usagi

    Re: Applet code conversion...

    Tsukino Usagi, Apr 30, 2012, in forum: Java
    Replies:
    0
    Views:
    410
    Tsukino Usagi
    Apr 30, 2012
  5. Roedy Green

    Re: Applet code conversion...

    Roedy Green, Apr 30, 2012, in forum: Java
    Replies:
    0
    Views:
    385
    Roedy Green
    Apr 30, 2012
Loading...

Share This Page