Results 1 to 9 of 9
  1. #1
    Rolf83's Avatar
    Rolf83 is offline Member
    Join Date
    Jun 2011
    Location
    Malmö, Sweden
    Posts
    18
    Rep Power
    0

    Default JApplet in JFrame...

    Hi

    I have issues with an Applet that I wish to run in a JFrame, as I understand there are some challenges with running applets in jframe whenever they use some specific get commands...

    The code is as following:


    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.JApplet;
    import javax.swing.JFrame;

    public class TheScene extends JApplet implements KeyListener {

    private static final long serialVersionUID = 1L;
    int width, height;
    int kx, ky;
    Image img;
    Image backbuffer;
    Graphics backg;

    public void init() {

    width = getSize().width;
    height = getSize().height;
    kx = 10;
    ky = 10;
    img = getImage(getDocumentBase(), "clouds.jpg");
    img = null;
    backbuffer = createImage( width, height );
    backg = backbuffer.getGraphics();
    backg.setColor( Color.black );

    //System.out.println(this.getDocumentBase());

    addKeyListener( this );
    }


    @Override
    public void keyPressed(KeyEvent e) {

    if (e.getKeyChar()=='d'){kx ++;}
    if (e.getKeyChar()=='s'){ky ++;}
    if (e.getKeyChar()=='a'){kx --;}
    if (e.getKeyChar()=='w'){ky --;}

    showStatus( "Coordinates (" + kx + "," + ky + ")" );
    backg.drawImage( img, 0, 0, this );
    backg.drawLine( kx, ky, kx, ky );
    repaint();
    e.consume();
    }
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}

    public void update( Graphics g ) {g.drawImage( backbuffer, 0, 0, this );}
    public void paint( Graphics g ) {update( g );}

    public static void main(String s[]) {
    JFrame f = new JFrame("TheScene");
    f.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {System.exit(0);}
    });
    JApplet applet = new TheScene();
    f.getContentPane().add("Center", applet);
    applet.init();
    f.pack();
    f.setSize(new Dimension(640,480));
    f.setVisible(true);
    }

    }


    I get a nullpointerexception in the init with:
    getDocumentBase()
    getGraphics()

    Is it fixable...?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Look at the setStub method. Underneath an applet is an AppletStub. It provides code for some of the methods used by an applet. You'll need to create one to provide code for those methods.

  3. #3
    Rolf83's Avatar
    Rolf83 is offline Member
    Join Date
    Jun 2011
    Location
    Malmö, Sweden
    Posts
    18
    Rep Power
    0

    Default

    where should I implement, in the init function?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    That might work. Or you could do it after you create the TheScene object.

    Why do you want to use an applet for a panel? This seems to be making a lot of work.

  5. #5
    Rolf83's Avatar
    Rolf83 is offline Member
    Join Date
    Jun 2011
    Location
    Malmö, Sweden
    Posts
    18
    Rep Power
    0

    Default

    Well I want an object inside a JFrame where I can draw some Graphic objects based on key inputs. I already have a separate class that implements JFrame with a JMenuBar, and the Idea was that it then should call this applet into a contentpane...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Why use an applet? There are other simpler classes for drawing on.

  7. #7
    Rolf83's Avatar
    Rolf83 is offline Member
    Join Date
    Jun 2011
    Location
    Malmö, Sweden
    Posts
    18
    Rep Power
    0

    Default

    Ahh I see, that must be the noob part kicking in :) Which classes will you suggest that I use, will I be able to use the class I already have and then just change the extension, or should I rebuild it to the class I choose to draw on?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Look at JPanel. Have a class that extends JPanel and overrides the paintComponent() method where you do your drawing.

    Do a Search on the forum for JPanel for many examples.

  9. #9
    Rolf83's Avatar
    Rolf83 is offline Member
    Join Date
    Jun 2011
    Location
    Malmö, Sweden
    Posts
    18
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 06-14-2010, 07:10 PM
  2. Making a JApplet into a JFrame into a .jar
    By aqualicy in forum Java Applets
    Replies: 2
    Last Post: 05-14-2010, 05:29 AM
  3. JApplet on MAC
    By ld_pvl in forum Java Applets
    Replies: 0
    Last Post: 08-23-2009, 02:40 PM
  4. JApplet.getGraphics() draws but JFrame doesn't
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 07-27-2009, 11:02 PM
  5. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 07:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •