Can't load image into JApplet

Discussion in 'Java' started by Paul Lee, May 11, 2004.

  1. Paul Lee

    Paul Lee Guest

    Dear All,
    I'm having problems loading (a) graphic(s) into a JApplet I am
    designing.
    I was wondering if anyone could help - I am trying to use get the
    image
    from the codebase directory, but I get a null reference.

    I have included my source code with the line indicated.

    Thanks!

    Paul

    ------------------------------------------------

    ---------------------------- MADapplet.java
    ----------------------------------
    import java.applet.*;
    import java.net.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;

    /*

    called in a browser using:
    <applet code=MADapplet.class width=300 height=300 archive=MAD.jar>
    </applet>

    */

    public class MADapplet extends JApplet
    {

    private MADviewer view;
    private Image deck, JCA, Merlin, MASC, Tug;
    private Vector MADaircraft_and_tug_datalist;
    private MADaircraft_and_tugdata aircraft1, aircraft2, aircraft3;

    private MADaircraft_and_tugdata tug1, tug2, tug3;

    public void init()
    {
    MADaircraft_and_tug_datalist = new Vector();

    // And add all the aircraft to the list..

    aircraft1= new MADaircraft_and_tugdata("JCA", 5, 5);
    aircraft2= new MADaircraft_and_tugdata("JCA", 5, 10);
    aircraft3= new MADaircraft_and_tugdata("JCA", 20, 15);

    tug1 = new MADaircraft_and_tugdata("Tug", 25, 15);
    tug2 = new MADaircraft_and_tugdata("Tug", 5, 25);
    tug3 = new MADaircraft_and_tugdata("Tug", 35, 45);

    MADaircraft_and_tug_datalist.add( aircraft1 );
    MADaircraft_and_tug_datalist.add( aircraft2 );
    MADaircraft_and_tug_datalist.add( aircraft3 );

    MADaircraft_and_tug_datalist.add( tug1 );
    MADaircraft_and_tug_datalist.add( tug2 );
    MADaircraft_and_tug_datalist.add( tug3 );

    deck = getImage( getDocumentBase(), "deck.jpg"); // <---- deck is
    null after this is called.
    JCA = getImage( getDocumentBase(), "JCA.gif");
    Merlin = getImage( getDocumentBase(), "Merlin.gif");
    MASC = getImage( getDocumentBase(), "MASC.gif");
    Tug = getImage( getDocumentBase(), "Tug.gif");

    setLayout(new BorderLayout());

    view = new MADviewer( this );

    add("Center", view);
    }

    public Image getDeckImage()
    {
    return deck;
    }

    public Image getJCAImage()
    {
    return JCA;
    }

    public Image getMerlinImage()
    {
    return Merlin;
    }

    public Image getMASCImage()
    {
    return MASC;
    }

    public Image getTugImage()
    {
    return Tug;
    }

    public Vector getVehicleData()
    {
    return MADaircraft_and_tug_datalist;
    }

    }

    -------------------------------------------------- MADviewer.java
    -----------------------------------

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

    public class MADviewer extends JFrame
    {

    private MADappletpanel panel;

    public MADviewer( MADapplet MADa )
    {
    super( "Aircraft co-ordinator" );

    Container container = getContentPane();

    panel = new MADappletpanel( MADa );

    container.add( panel );

    setSize( 425, 150 );
    setVisible( true );

    }
    }

    ---------------------------- MADappletpanel.java
    -----------------------------------

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.net.*;
    import java.awt.*;
    import java.applet.Applet;
    import java.util.*;

    public class MADappletpanel extends JPanel
    {
    private Image deck, JCA, Merlin, MASC, Tug, icon;

    private Vector vehicle_data;

    private Enumeration vehicle_items;

    private MADaircraft_and_tugdata selected_vehicle;

    public MADappletpanel( MADapplet MADa )
    {
    deck = MADa.getDeckImage();
    JCA = MADa.getJCAImage();
    Merlin = MADa.getMerlinImage();
    MASC = MADa.getMASCImage();
    Tug = MADa.getTugImage();

    vehicle_data = MADa.getVehicleData();

    Enumeration items = vehicle_data.elements();

    addMouseMotionListener(

    new MouseMotionListener() {

    public void mouseDragged( MouseEvent event )
    {
    // Get co-ords of dragged mouse with button down.
    // update the selected icon's x/y position.

    selected_vehicle.setX( event.getX() );
    selected_vehicle.setY( event.getY() );

    repaint(); // draw all the icons and background image
    }

    public void mouseReleased( MouseEvent event )
    {
    // get final co-ordinate
    }

    public void mouseMoved( MouseEvent event )
    {

    }
    }

    );

    addMouseListener(

    new MouseAdapter() {

    public void mousePressed( MouseEvent event )
    {
    // Click on an icon and gets its position
    // Compare the location with the list of icons' position
    // Get a "handle" on the selected icon if the location
    // of the icon is within a square of 10 pixels of the
    // "click" location

    int x = event.getX();
    int y = event.getY();

    MADaircraft_and_tugdata current_vehicle;

    while (vehicle_items.hasMoreElements() )
    {
    current_vehicle = ( MADaircraft_and_tugdata )
    vehicle_items.nextElement();

    if( x >= current_vehicle.getX() - 10 &&
    x <= current_vehicle.getX() + 10 &&
    y >= current_vehicle.getY() - 10 &&
    y <= current_vehicle.getY() + 10 )
    {
    selected_vehicle = current_vehicle;
    break;
    }
    }
    }

    }

    );

    }

    public void paint( Graphics g )
    {

    // Draw images on JPanel

    g.drawImage( deck, 0, 0, this );

    // add icons
    // just use g.drawImage to superimpose images
    // icon images need to have a transparent background.

    // loop through icons list, get x, y and put
    // them on screen at the location
    // NEED TO FIND OUT WHAT TYPE OF VEHICLE THEY ARE FIRST

    while (vehicle_items.hasMoreElements() )
    {
    MADaircraft_and_tugdata vehicle = ( MADaircraft_and_tugdata )
    vehicle_items.nextElement();

    if ( vehicle.getType().equals( "JCA" ) )
    {
    icon = JCA;
    }
    else if ( vehicle.getType().equals( "Merlin" ) )
    {
    icon = Merlin;
    }
    else if ( vehicle.getType().equals( "MASC" ) )
    {
    icon = MASC;
    }
    else if ( vehicle.getType().equals( "Tug" ) )
    {
    icon = Tug;
    }

    g.drawImage( icon, vehicle.getX() , vehicle.getY() , this);

    }
    }

    }

    ------------------------------------- MADaircraft_and_tugdata.java
    -----------------------------

    public class MADaircraft_and_tugdata
    {

    private String type;
    private int x, y;

    public MADaircraft_and_tugdata( String type, int x, int y)
    {
    this.type = type;
    this.x = x;
    this.y = y;
    }

    public String getType()
    {
    return type;
    }

    public int getX()
    {
    return x;
    }

    public int getY()
    {
    return y;
    }

    public void setX( int x )
    {
    this.x = x;
    }

    public void setY( int y )
    {
    this.y = y;
    }

    }
     
    Paul Lee, May 11, 2004
    #1
    1. Advertising

  2. On 11 May 2004 09:39:19 -0700, Paul Lee wrote:

    > Dear All,
    > I'm having problems loading (a) graphic(s) into a JApplet I am
    > designing.


    ...and you thought it should take 325 lines
    to demonstrate how you can stuff that up?

    <http://www.physci.org/codes/sscce.jsp>
    Please pay particular attention to the
    first word. Short.

    > I was wondering if anyone could help - I am trying to use get the
    > image
    > from the codebase directory, but I get a null reference.

    ....
    > called in a browser using:
    > <applet code=MADapplet.class


    codebase='.'

    > width=300 height=300 archive=MAD.jar>
    > </applet>

    ....
    > deck = getImage( getDocumentBase(), "deck.jpg"); // <---- deck is
    > null after this is called.


    ...should do it.

    Please consider posting to c.l.j.help for the moment.
    <http://www.physci.org/codes/javafaq.jsp#cljh>

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, May 11, 2004
    #2
    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. John C. Bollinger

    Re: JApplet + API question

    John C. Bollinger, Jun 27, 2003, in forum: Java
    Replies:
    0
    Views:
    1,093
    John C. Bollinger
    Jun 27, 2003
  2. Eric Olander

    Re: JApplet + API question

    Eric Olander, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    484
    Sudsy
    Jul 8, 2003
  3. Andrew Thompson
    Replies:
    0
    Views:
    436
    Andrew Thompson
    Aug 5, 2003
  4. Cheok Yan Cheng

    ImageIcon.paintIcon wont work in JApplet

    Cheok Yan Cheng, Aug 13, 2003, in forum: Java
    Replies:
    0
    Views:
    1,782
    Cheok Yan Cheng
    Aug 13, 2003
  5. Replies:
    3
    Views:
    3,185
    Andrew Thompson
    Aug 12, 2006
Loading...

Share This Page