Can't load image into JApplet

P

Paul Lee

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;
}

}
 
A

Andrew Thompson

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top