Open a file in JApplet

J

Jenny

Hi,

I tried to open a file and read it into an JApplet. It does not
display the string in the file. Could tell me which part of code does
not run and why? Could you tell me how to fix it? Thanks a lot. Here
is the code:

Java file 1:

import java.awt.*;
import javax.swing.*;
import java.io.*;
public class PieApplet extends JApplet {
Color uneasyBeingGreen = new Color(0xCC, 0xCC, 0x99);
Color zuzusPetals = new Color(0xCC, 0x66, 0xFF);
Color zootSuit = new Color(0x66, 0x66, 0x99);
Color sweetHomeAvocado = new Color(0x66, 0x99, 0x66);
Color shrinkingViolet = new Color(0x66, 0x66, 0x99);
Color miamiNice = new Color(0x33, 0xFF, 0xFF);
Color inBetweenGreen = new Color(0x00, 0x99, 0x66);
Color norwegianBlue = new Color(0x33, 0xCC, 0xCC);
Color purpleRain = new Color(0x66, 0x33, 0x99);
Color freckle = new Color (0x99, 0x66, 0x33);

public void init() {
Container pane = getContentPane();
PiePanel pie = new PiePanel(10);
pie.addSlice(uneasyBeingGreen, 1284);
pie.addSlice(zuzusPetals, 1046);
pie.addSlice(zootSuit, 281);
pie.addSlice(sweetHomeAvocado, 232);
pie.addSlice(shrinkingViolet, 176);
pie.addSlice(miamiNice, 148);
pie.addSlice(inBetweenGreen, 143);
pie.addSlice(norwegianBlue, 133);
pie.addSlice(purpleRain,130);
pie.addSlice(freckle, 127);
pane.add(pie);
setContentPane(pane);
}
}

Java file 2:

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.io.*;
public class PiePanel extends JPanel {
private PieSlice[] slice;
private int current = 0;
private float totalSize = 0;
private Color background;

public PiePanel(int sliceCount) {
slice = new PieSlice[sliceCount];
background = getBackground();
}

public void addSlice(Color sColor, float sSize) {
if (current <= slice.length) {
slice[current] = new PieSlice(sColor, sSize);
totalSize += sSize;
current++;
}
}

public void paintComponent(Graphics comp) {
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D) comp;
int width = getSize().width - 10;
int height = getSize().height - 15;
int xInset = 5;
int yInset = 5;
if (width < 5)
xInset = width;
if (height < 5)
yInset = height;
comp2D.setColor(background);
comp2D.fillRect(0, 0, getSize().width, getSize().height);
comp2D.setColor(Color.lightGray);
Ellipse2D.Float pie = new Ellipse2D.Float(
xInset, yInset, width, height);
comp2D.fill(pie);
float start = 0;
for (int i = 0; i < slice.length; i++) {
float extent = slice.size * 360F / totalSize;
comp2D.setColor(slice.color);
Arc2D.Float drawSlice = new Arc2D.Float(
xInset, yInset, width, height, start, extent,
Arc2D.Float.PIE);
start += extent;
comp2D.fill(drawSlice);
try {
File f = new File("hello.txt");
FileInputStream file = new FileInputStream(f);
byte[] b = new byte[20];
file.read(b);
String s = new String(b);
comp2D.drawString(s,20,20);
file.close();
} catch (Exception e) {
}
}
}
}

class PieSlice {
Color color = Color.lightGray;
float size = 0;
PieSlice(Color pColor, float pSize) {
color = pColor;
size = pSize;
}
}

HTML file:

<applet code="PieApplet.class" width="300" height="200">
</applet>


in hello.txt: Hello World.
 
P

Paul Lutus

Jenny said:
Hi,

I tried to open a file and read it into an JApplet. It does not
display the string in the file. Could tell me which part of code does
not run and why?

No, not really. What error messages does your JApplet generate? Do you have
a shorter, more concise version of your program?
 
H

Hal Rosser

I tried to open a file and read it into an JApplet. It does not
display the string in the file. Could tell me which part of code does
not run and why? Could you tell me how to fix it? Thanks a lot. Here
is the code:
File f = new File("hello.txt");
FileInputStream file = new FileInputStream(f);
byte[] b = new byte[20];
file.read(b);
String s = new String(b);
comp2D.drawString(s,20,20);
file.close();

Try using URL instead of File - and the file needs to be on the server-side
unless its a signed applet
 
J

Jenny

Paul Lutus said:
No, not really. What error messages does your JApplet generate? Do you have
a shorter, more concise version of your program?

It does not give me any error msg. I do not have a short version.
But I can make one.
 
Z

zoopy

It does not give me any error msg.

That's most likely caused by your empty catch block:
} catch (Exception e) {
}

Change it to:
} catch (Exception e) {
e.printStackTrace();
}
 
A

Andrew Thompson

I tried to open a file and read it into an JApplet. It does not
display the string in the file. Could tell me which part of code does
not run and why?

Part of the problem is that this code ignores errors..
...Could you tell me how to fix it?

You might start by always ensuring you print an
exception even if, ..perhaps especially if, that
exception or error 'cannot' happen.
...Thanks a lot. Here
is the code:

I have not had time to try you complete example Jenny,
but here is the tip I have for you..
Java file 1:
....
try {
File f = new File("hello.txt");
FileInputStream file = new FileInputStream(f);
byte[] b = new byte[20];
file.read(b);
String s = new String(b);
comp2D.drawString(s,20,20);
file.close();
} catch (Exception e) {

// here is the better way to deal with this..
e.printStacktrace();

Once you do that, you should see errors appearing in the
Java console of your browser.. They might look complicated
and scary at first, but the info. on this page might help
you get used to reading and understanding them.
<http://www.physci.org/codes/javafaq.jsp#exact>

It is not just handy, but vital to be able to
start debugging Java problems by using the information
in the stack trace..

There is also information on a different Java group
further down that page, I suspect you might get better
help there for the moment if you are a beginner, it is
<http://www.physci.org/codes/javafaq.jsp#cljh>

People who post there generally take a little extra
time to help the people new to Java.

HTH
 
J

Jenny

Here is the part of message. It does not allow me to open the file.

Thank you all.

java.security.AccessControlException: access denied
(java.io.FilePermission hello.txt read)

at java.security.AccessControlContext.checkPermission(Unknown Source)
 
K

kjc

You need to modify your java.policy file to allow the JApplet to read
from your filesystem.
 
A

Andrew Thompson

Jenny wrote: ...
You need to modify your java.policy file to allow the JApplet to read
from your filesystem.

That will work for each PC on which it it done,
but you write an applet and wrap in HTML, it is
quite possibly bound for the internet where it will
hopefully be viewed and used by ..significantly
more than 1 PC.

For deployment to more than a handful of power
users, signing the applet and delivering it via
WebStart is the far better (quicker & more
convenient for the end user) solution.
 
K

kjc

Agreed, but. This person sounds like a newbie. And, there is nothing
better than getting something working using the path of least resistence.
 
A

Andrew Thompson

Agreed, but. This person sounds like a newbie. And, there is nothing
better than getting something working using the path of least resistence.

Good point. But the 'path of least resistance' is
actually an application. No security restrictions. ;-)

OTOH, I got that post out of context for a moment
and assumed the JApplet we were discussing actually
*needed* to have increased priviliges.

It does not. Use an URL to obtain the resource
from (for example) the same firectory, and to
load it, that will work just fine in an unsigned
applet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top