Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with images in jar file for an applet

Status
Not open for further replies.

clax99

Programmer
Oct 29, 2001
23
0
0
US
Im writing a small applet and I'm having problems with the 2 gif images. I can get the applet to run with images using the appletviewer. However, when I try to use the web browser I can only get it to work if I take out all imaging before compilation. I am using a jar file (BoneJarFile.jar) which consists of the Bone.class file and two gif's. Here is the HTML code:

<HTML>

<HEAD>

<TITLE> BONE THUGS </TITLE>

</HEAD>

<CENTER>

<H1> THE GREATEST </H1>

<P>

<APPLET CODE = Bone.class ARCHIVE = BoneJarFile.jar width = 400 height = 360>
</APPLET>

</CENTER>

</BODY>

</HTML>

Here's a clip of the source, its short:
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;


public class Bone extends JApplet implements Runnable{

Image bone = new javax.swing.ImageIcon(&quot;boneThugs.gif&quot;).getImage();
Image boneName = new javax.swing.ImageIcon(&quot;bone.gif&quot;).getImage();

String yo = &quot;THE GREATEST RAP GROUP OF ALL TIME&quot;;
Thread t;

public void init(){
setBackground(new Color(10,130,115));
t = new Thread(this, &quot;Bone sequence&quot;);
}
public void start(){

}
public void run(){}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;

try{
g2.setColor(new Color(10,130,115));
g2.fillRect(0,0,400,360);
t.sleep(1000);
g2.setColor(Color.blue);
g2.drawString(yo, 75, 25);
t.sleep(1000);
//g2.drawImage(bone,95,45,this);
t.sleep(1000);
//g2.drawImage(boneName,120,265,this);
t.sleep(1000);
}catch(InterruptedException e){}

repaint();
}
}


I get a security error when I try to initialize the first image; I know applets cant access client machines, but this is coming from the JAR file, so it should be okay? But its not, help!
 
You'll have more luck getting the image as a resource:

URL imageUrl = this.getClass().getResource(&quot;myimage.gif&quot;);

Then use the URL to create the image:

Image i = new ImageIcon(imageUrl).getImage();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top