Access denied.....
Alright so, im somewhat new to java, let alone programming, and i was just looking around for some things to program to help teach myself the language. Well, i was attempting to make a ball go across the screen, and bounce back. But I'm afraid that something is wrong with my code or something. The ball appears on the screen and moves accross it, but it does not double buffer, nor does it bounce back. After i close the applet i get this error:
Exception in thread "Thread-3" java.security.AccessControlException: access denied (java.lang.RuntimePermission modifyThread)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at sun.applet.AppletSecurity.checkAccess(AppletSecurity.java:214)
at java.lang.Thread.checkAccess(Thread.java:1263)
at java.lang.Thread.setPriority(Thread.java:1013)
at gamz0r1.run(gamz0r1.java:59)
at java.lang.Thread.run(Thread.java:619)
And here is my code:
/*COMMENT
*
*
*
*/
import java.applet.*;
import java.awt.*;
public class gamez0r1 extends Applet implements Runnable
{
int x_pos = 10;
int y_pos = 100;
int x_speed = 1;
int radius = 20;
int appletsize_x = 300;
int appletsize_y= 300;
private Image dbImage;
private Graphics dbg;
public void init()
{
setBackground(Color.blue);
}
public void start()
{
// define a new thread
Thread th = new Thread (this);
//start this thread
th.start();
}
public void stop() { }
public void destroy() { }
public void run ()
{
// lower THreadPriority
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// run a long while (true this means in our case "always"
while(true)
{
//Ball is bounced if its x- position reaches the right border of the applet
if (x_pos > appletsize_x - radius)
{
//Change direction of the ball movement
x_speed = -1;
}
//Ball is bounced if its x- position reaches the left border of the applet
else if (x_pos < radius)
{
//Change direction of ball movement
x_speed = +1;
}
x_pos += x_speed;
//repaint the applet
repaint();
try
{
// Stop thread for 20 milliseconds
Thread.sleep (20);
}
catch (InterruptedException ex)
{
//do nothing
}
//set THreadPriority to maxium value
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements foreground
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
//set color
g.setColor (Color.red);
//paint a filled colored circle
g.fillOval (x_pos - radius, y_pos - radius, 2*radius, 2*radius);
}
}
Please help! I'll greatly appreciate it! :D
-FTL
forthloss, (You'd be a pom then... he he he)
read [url=http://forum.java.sun.com/help.jspa?sec=formatting]this[/url], and repost your code using between some [code]code tags[/code].
My quick scan of your code didn't show up anything immediately and obviously mental.
keith.
Thanks for your input. I couldn't seem to find the Edit Post button (heh), so heres my code:
PS Sorry for the sloppiness, its kinda late :P
/*COMMENT
*
*
*
*/
import java.applet.*;
import java.awt.*;
public class gamez0r1 extends Applet implements Runnable
{
int x_pos = 10;
int y_pos = 100;
int x_speed = 1;
int radius = 20;
int appletsize_x = 300;
int appletsize_y = 300;
private Image dbImage;
private Graphics dbg;
public void init()
{
setBackground(Color.blue);
}
public void start()
{
// define a new thread
Thread th = new Thread (this);
//start this thread
th.start();
}
public void stop() { }
public void destroy() { }
public void run ()
{
// lower THreadPriority
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// run a long while (true this means in our case "always"
while(true)
{
//Ball is bounced if its x- position reaches the right border of the applet
if (x_pos > appletsize_x - radius)
{
//Change direction of the ball movement
x_speed = -1;
}
//Ball is bounced if its x- position reaches the left border of the applet
else if (x_pos < radius)
{
//Change direction of ball movement
x_speed = +1;
}
x_pos += x_speed;
//repaint the applet
repaint();
try
{
// Stop thread for 20 milliseconds
Thread.sleep (20);
}
catch (InterruptedException ex)
{
//do nothing
}
//set THreadPriority to maxium value
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements foreground
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
//set color
g.setColor (Color.red);
//paint a filled colored circle
g.fillOval (x_pos - radius, y_pos - radius, 2*radius, 2*radius);
}
}
> After i close the applet i get this error:
>
> Exception in thread "Thread-3"
> java.security.AccessControlException: access denied
> (java.lang.RuntimePermission modifyThread)
> at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
> at java.security.AccessController.checkPermission(AccessController.java:546)
> at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
> at sun.applet.AppletSecurity.checkAccess(AppletSecurity.java:214)
> at java.lang.Thread.checkAccess(Thread.java:1263)
> at java.lang.Thread.setPriority(Thread.java:1013)
> at gamz0r1.run(gamz0r1.java:59)
> at java.lang.Thread.run(Thread.java:619)
That's a stack trace. It shows you what methods were being executed when the error occurred.
It's telling you that your code can't change the thread details, probably because it's an applet and being run in a sandbox. The second-to-last line tells you that the offending statement took place on line 59 of the file gamz0r1.java. That's probably this:
> Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
So remove that line. It's not clear to me why you'd want to change this immediately prior to the thread ending anyway.
try this for a start:if (x_pos >= appletsize_x - radius) {
x_speed = -1;
} else if (x_pos <= radius) {
x_speed = +1;
}
Hmm, that makes sense, but line 59 appears to be the comment:
//Ball is bounced if its x- position reaches the left border of the applet
How could a simple comment cause this error?
-FTL
EDIT: corlettk, i tried your idea, but no luck :( Any other ideas?
Message was edited by:
forthelose
as paul suggested, get rid of the thread priority manipulation... it's possibly the problem, and definitely useless.
I tried deleting both thread manipulation lines, but it still gave me the same error. I'm beginning to thing that the problem does not have to do with the code, but with something else. Unfortunatly, i have no idea what that "something else" is. Do you?
frothy,
I got interested... this code & html works on this machine (AMD 64, XP, JDK&RE 1.6) in both firefox and IE.
c:\java\home\src\forums\gamez0r1.javaimport java.applet.*;
import java.awt.*;
public class gamez0r1 extends Applet implements Runnable
{
private static final long serialVersionUID = 984545621;
int x_pos = 10;
int y_pos = 100;
int x_speed = 1;
int radius = 20;
int appletsize_x = 300;
int appletsize_y = 300;
private Image dbImage;
private Graphics dbg;
public void init() {
setBackground(Color.blue);
}
public void start() {
Thread th = new Thread (this);
th.start();
}
public void stop() {
}
public void destroy() {
}
public void run ()
{
// run a long while (true this means in our case "always"
while(true) {
//Change direction of ball movement when it reaches the left/right border of the applet
if (x_pos >= appletsize_x - radius) {
//hit right border
x_speed = -1;
} else if (x_pos <= radius) {
//hit left border
x_speed = +1;
}
x_pos += x_speed;
repaint();
try { Thread.sleep (20); } catch (InterruptedException ignore) {}
}
}
public void update (Graphics g) {
// initialize dbg buffer
if (dbImage == null) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
// clear screen in background
dbg.setColor(getBackground ());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
// draw elements foreground
dbg.setColor(getForeground());
paint(dbg);
// draw image on the screen
g.drawImage(dbImage, 0, 0, this);
}
public void paint (Graphics g) {
g.setColor(Color.red);
//paint a filled colored circle
g.fillOval(x_pos - radius, y_pos - radius, 2*radius, 2*radius);
}
}
c:\java\home\src\forums\gamez0r1.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- C:\Java\home\src\forums\gamez0r1.html -->
<html>
<head>
<title>gamez0r1</title>
</head>
<body>
<applet
name="gamez0r1"
code="gamez0r1.class"
width="300"
height="300"
>
<param name="bgcolor" value="ffffff">
<param name="fontcolor" value="000000">
Your browser is not Java enabled.
</applet>
</body>
</html>
... but that doesn't necessarily mean that it'll work on your machine... maybe it is environmental, but mostly mental one suspects.
keith.
> I tried deleting both thread manipulation lines, but
> it still gave me the same error. I'm beginning to
> thing that the problem does not have to do with the
> code, but with something else. Unfortunatly, i have
> no idea what that "something else" is. Do you?
I find that hard to believe, that deleting those lines resulted in the same error.
Are you sure you recompiled the code?
Otherwise, please look at the stack trace again, and see what lines in your file it's referring to, and show us those lines.
>I find that hard to believe, that deleting those lines
>resulted in the same error.
>
>Are you sure you recompiled the code?
It may be hard to believe, but, yes, it still did not work. And, yes, I did recompile...
Like keith said, it may be enviromental. I'm going to try to run the code in class and see what happens.
EDIT: Alright, just ran it, and it worked. So it must be something wrong with my machine or enviroment. Do yuo guys know what it could be?
Message was edited by:
forthelose
> >I find that hard to believe, that deleting those lines
> >resulted in the same error.
> >
> >Are you sure you recompiled the code?
>
> It may be hard to believe, but, yes, it still did not
> work. And, yes, I did recompile...
Well, it's hard to believe that you got the exact same error message, one that says:
> java.security.AccessControlException: access denied
> (java.lang.RuntimePermission modifyThread)
After you've removed a line of code that changed the priority of a thread.
That's why I thought that either (a) you're actually getting a different error message, or (b) you didn't actually recompile.
> Like keith said, it may be enviromental. I'm going to
> try to run the code in class and see what happens.
Eh? What do you mean "run the code in class"? As opposed to what...?
> EDIT: Alright, just ran it, and it worked. So it must
> be something wrong with my machine or enviroment. Do
> yuo guys know what it could be?
I still think it might just be that previously you were running old code, pre-fixes.
