Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Trouble With getting Applet to work with browser  (Read 790 times)
0 Members and 1 Guest are viewing this topic.
Offline Epo

Senior Newbie


Projects: 1


Green Tea and swords


« Posted 2005-01-13 22:54:09 »

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.<init>(Unknown Source)
     at javax.swing.ImageIcon.<init>(Unknown Source)
     at sprite.<init>(sprite.java:16)
     at pong.<init>(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)
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #1 - Posted 2005-01-14 00:40:43 »

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

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Epo

Senior Newbie


Projects: 1


Green Tea and swords


« Reply #2 - Posted 2005-01-14 01:46:23 »


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

}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #3 - Posted 2005-01-14 11:00:54 »

you should try holding a BufferedImage instead of an ImageIcon.  Check out these options:
1  
2  
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:
1  
2  
3  
4  
5  
6  
7  
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.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!