Jeg er igang med at lave en 2D chat! Hvor man ikke kan skrive til hinanden! Men der kommer en fejl:
java.security.AccessControlException: access denied (java.io.FilePermission TWC.txt 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 java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at TWC_Hent.<init>(TWC_Hent.java:23)
at TWC.init(TWC.java:39)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
TWC.class:
import java.applet.*;
import java.awt.*;
import java.io.IOException;
import java.util.Vector;
public class TWC extends Applet implements Runnable
{
boolean o, n, v, h, loeb;
Thread tråd;
TWC_Hent Hent;
Image billede;
Vector punkter;
Image dethele;
Graphics gr;
int xx, xy;
public void start()
{
if(tråd == null)
{
tråd = new Thread(this);
tråd.start();
}
}
public void stop()
{
if(tråd != null)
{
tråd.stop();
tråd = null;
}
}
public void init()
{
/*try {
punkter = Hent.Placering();
} catch (IOException e) {
e.printStackTrace();
}*/
try {
Hent = new TWC_Hent("Test","TEST");
} catch (IOException e) {
e.printStackTrace();
}
}
public void paint(Graphics g)
{
update(g);
}
public void update(Graphics g)
{
if (gr == null)
{
dethele = createImage(500, 500);
gr = dethele.getGraphics();
}
g.drawImage(dethele, 0, 0, this);
}
public void run()
{
while(gr==null)
{
try
{
Thread.sleep(1);
}catch(InterruptedException e){}
}
while(tråd == Thread.currentThread())
{
try {
punkter = Hent.placering();
} catch (IOException e) {
e.printStackTrace();
}
Flytden();
gr.setColor( new Color(255,255,255) );
gr.fillRect(0,0,500,500);
gr.setColor( new Color(255,0,0) );
if(punkter != null)
for(int i = 0;i<=punkter.size()-1;i++)
{
punkter.elementAt(i);
Point punkt = (Point)(punkter.elementAt(i));
gr.fillRect(punkt.x, punkt.y,30,50);
}
else
gr.drawString("Forkert brugernavn/kodeord!!!",100,100);
repaint();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public boolean keyUp (Event e, int key)
{
switch(key)
{
case 1004: o=false;break;
case 1005: n=false;break;
case 1007: h=false;break;
case 1006: v=false;break;
case 97: loeb=false;break;
default:;
}
return true;
}
public boolean keyDown (Event e, int key)
{
switch(key)
{
case 1004: o=true;break;
case 1005: n=true;break;
case 1007: h=true;break;
case 1006: v=true;break;
case 97: loeb=true;break;
default:;
}
return true;
}
public void Flytden()
{
xx=0;
xy=0;
if(o)
xy--;
if(n)
xy++;
if(v)
xx--;
if(h)
xx++;
if(loeb)
{
xx*=3;
xy*=3;
}
try {
Hent.flyt(xx,xy);
} catch (IOException e) {
e.printStackTrace();
}
}
}
TWC_Hent.class:
import java.io.*;
import java.util.Vector;
import java.awt.*;
public class TWC_Hent
{
String fil;
BufferedReader ind;
PrintWriter ud;
String Brugernavn, Kodeord;
String linie;
String[] linie_array;
Vector alting;
int id;
boolean rigtig;
public TWC_Hent(String Brugernavnet, String Kodeordet) throws IOException
{
linie_array = new String[4];
rigtig = false;
id=0;
Brugernavn = Brugernavnet;
Kodeord = Kodeordet;
fil = "TWC.txt";
ind = new BufferedReader( new FileReader(fil) );
linie = ind.readLine();
while(linie != null)
{
if(!rigtig)
id++;
System.out.println("Der står: "+linie);
linie_array = linie.split(" ");
if(Brugernavn.equalsIgnoreCase(linie_array[0])
&& Kodeord.equalsIgnoreCase(linie_array[1]))
rigtig = true;
linie = ind.readLine();
}
System.out.println(rigtig);
}
public static void main(String[] args)
{
}
public Vector placering() throws IOException
{
if(rigtig)
{
alting = new Vector();
ind = new BufferedReader( new FileReader(fil) );
linie = ind.readLine();
while(linie != null)
{
linie_array = linie.split(" ");
alting.addElement((Point)(
new Point(Integer.parseInt(linie_array[2]),
Integer.parseInt(linie_array[3]))));
System.out.println(linie);
linie = ind.readLine();
}
return alting;
}
else
return null;
}
public void flyt(int x, int y) throws IOException
{
if(rigtig)
{
ind = new BufferedReader( new FileReader(fil) );
linie = ind.readLine();
int idet = 0;
Vector linier = new Vector();
while(linie != null)
{
idet++;
if(idet == id)
{
linie_array = linie.split(" ");
x+=Integer.parseInt(linie_array[2]);
y+=Integer.parseInt(linie_array[3]);
if(x>450) x=450;
if(x<0) x=0;
if(y>450) y=450;
if(y<0) y=0;
linie = linie_array[0]+" "+linie_array[1]+" "+x+" "+y;
linier.addElement(linie);
}
else
linier.addElement(linie);
linie = ind.readLine();
}
ud = new PrintWriter( new FileWriter(fil) );
for(int i = 0;i<=linier.size()-1;i++)
ud.println(linier.elementAt(i));
if(ud.checkError())
System.out.print("FEJL!!!!!!");
ud.close();
}
}
}
Hilsen Mathias