Trouble With getting Applet to work with browser

Hello. I made a Pong game in Java and it works fine in appletviewer, but when ever I try to run it as a Applet in IE it just says it failed. I checked the console and I got this. Do you think it could be because I am using Image Icons.

java.security.AccessControlException: access denied (java.io.FilePermission sword.png read)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkRead(Unknown Source)
at sun.awt.SunToolkit.getImageFromHash(Unknown Source)
at sun.awt.SunToolkit.getImage(Unknown Source)
at javax.swing.ImageIcon.(Unknown Source)
at javax.swing.ImageIcon.(Unknown Source)
at sprite.(sprite.java:16)
at pong.(pong.java:27)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

show us the code you’re using to load ‘sword.png’ and we’ll tell you how to change it to make it work.

Here is the pong code

import java.applet.Applet;
import java.awt.;
import java.awt.event.
;
import java.util.Random;

public class pong extends Applet implements Runnable, KeyListener{

Thread thr = new Thread(this);

Image buffer;
int ya=89;
int yb=89;
int yc=105;
int xa=165;

int rightScore=0;
int leftScore=0;

int ycAdd=0;
char ballDir=‘c’;

boolean fps = true;
char dir = ‘r’;

Random num = new Random();

sprite sa = new sprite(“sword.png”,1);
sprite sb = new sprite(“sword.png”,1);
sprite ba= new sprite(“ball.png”,1);

public void init(){
addKeyListener(this);
setBackground(Color.black);
thr.start();
}

public void paint(Graphics g){

sa.pasteSprite(this,g,0,ya);
sb.pasteSprite(this,g,325,yb);
ba.pasteSprite(this,g,xa,yc);

g.setColor(Color.green);
g.drawString(“SCORE”,120,10);
g.drawString(""+leftScore+" // "+rightScore,125,30);

}

public void update(Graphics g){

if (buffer == null)
buffer=createImage(350,250);
Graphics ga= buffer.getGraphics();
ga.clearRect(0,0,350,250);
paint(ga);
g.drawImage(buffer,0,0,this);
ga.dispose();

}//END UPDATE METHOD

public void run(){
while(fps){

//AI CODE START HERE
if (yb>yc+20 && num.nextInt(2)==1)
yb=yb-8;
if (yb+72<yc && num.nextInt(2)==1)
yb=yb+8;
//AI CODE ENDS HERE

if (hit.spriteHitTest(sa,ba)){
dir=‘r’;
if (ballDir==‘u’)
ycAdd=num.nextInt(3)+2*-1;
if (ballDir==‘d’)
ycAdd=num.nextInt(3)+2;
}//ENF IF
else if (hit.spriteHitTest(sb,ba)){
dir=‘l’;
}//End ELSE IF

if (yc >=230)
ycAdd=-4;
else if (yc <= 0 )
ycAdd=4;

if (xa >=360){
ballDir=‘c’;
ycAdd=0;
yc=105;
xa=165;
leftScore=leftScore+1;
}else if (xa <= -30 ){
ballDir=‘c’;
ycAdd=0;
yc=105;
xa=165;
rightScore=rightScore+1;
}//END ELSE IF

if (dir == ‘r’)
xa=xa+4;
else if (dir == ‘l’)
xa=xa-4;

yc=yc+ycAdd;

try{thr.sleep(10);}catch(Exception e){}//SLEEP METHOD

repaint();
}//END WHILE LOOP

}//END RUN METHOD

public void keyPressed(KeyEvent e){
int codeNum= e.getKeyCode();

if (codeNum==e.VK_UP){

if (ya>0)
ya=ya-8;
ballDir=‘u’;

repaint();
}else if (codeNum==e.VK_DOWN){

if (ya<178)
ya=ya+8;
ballDir=‘d’;

repaint();
}//End IF

}//END METHOD

public void keyTyped(KeyEvent e){
int codeNum= e.getKeyCode();

}//END METHOD

public void keyReleased(KeyEvent e){
int codeNum=e.getKeyCode();

}//END METHOD

}//end class

--------Here is the sprite code-------

import javax.swing.;
import java.awt.
;

public class sprite{

private ImageIcon[] frameSprite;
private int gx;
private int gy;
int currentFrame=0;
boolean _switch=true;

public sprite(String frame, int max){
frameSprite=new ImageIcon[max];
frameSprite[0]=new ImageIcon(frame);
gx=0;
gy=0;
}

public sprite(String[] frames){
frameSprite=new ImageIcon[frames.length];

for (int a=0; a<frames.length; a++)
frameSprite[a]=new ImageIcon(frames[a]);

gx=0;
gy=0;
}

public final void addFrame(String frame,int seg){
frameSprite[seg]=new ImageIcon(frame);
}

public final int returnSpriteX(){
return gx;
}

public final int returnSpriteY(){
return gy;
}

public final int returnSpriteWidth(int frame){
return frameSprite[frame].getIconWidth();
}

public final int returnSpriteHeight(int frame){
return frameSprite[frame].getIconHeight();
}

public final void pasteSprite(Component c,Graphics g,int x, int y,int frame){
frameSprite[frame].paintIcon(c,g,x,y);
gx=x;
gy=y;
currentFrame=frame;
}

public final void pasteSprite(Component c,Graphics g,int x, int y){
frameSprite[currentFrame].paintIcon(c,g,x,y);
gx=x;
gy=y;
}

}

you should try holding a BufferedImage instead of an ImageIcon. Check out these options:


URL resource = sprite.class.getClassLoader().getResource(string);
BufferedImage image = ImageIO.read(resource);

However I’ve had some problems with ImageIO in Applets. You could also try:


Image src = Toolkit.getDefaultToolkit().createImage(resource);
MediaTracker mt = new MediaTracker(new Applet()); //or if you already have a Component somewhere, pass that in instead of a new Applet()
mt.addImage(src, 0);
try {mt.waitForAll();}
catch (InterruptedException e){
      e.printStackTrace();
}

The first method doesn’t return until the image is loaded. The Toolkit method starts the loading process but doesn’t necessarily finish for a little while later. To ensure it’s loaded immediately, you use the MediaTracker code afterwards to wait until it’s done.