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.
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.