I am having a hard time opening a text file from an applet. I remembered after an attempt or two that there were obvious security issues here, so I switched the code to pull a text file off the internet, and there is obviously some naming or reference issue that I can't get my head around. Here is my code:
[code=java]import java.awt.*;
import java.awt.Font;
import java.awt.Graphi cs;
import java.awt.FontMe trics;
import java.io.*;
import java.net.*;
public class solong17 extends java.applet.App let {
String letter = new String();
public void paint(Graphics screen) {
//Color bk = new Color(0,0,0);
//Color tx = new Color(255,255,2 55);
Font f = new Font("Arial", Font.BOLD, 18);
FontMetrics fm = getFontMetrics( f);
setBackground(C olor.black);
screen.setColor (Color.white);
screen.setFont( f);
String s = "So long, and thanks for all the fish.";
int x = (size().width - fm.stringWidth( s)) / 2;
int y = size().height / 2;
screen.drawStri ng(s, x, y);
}
TextArea lt;
public void init() {
try {
URL page = new
URL("http://www.worksonline .org/works/default.htm");
}
catch (MalformedURLEx ception e) {
System.out.prin tln("Bad URL: http://www.worksonline .org" +
"/works/default.htm");
}
URLConnection conn = null;
InputStreamRead er in;
BufferedReader data;
StringBuffer buf = new StringBuffer();
String line;
//System.out.prin tln(page);
try {
conn = this.page.openC onnection(); //error is here
conn.connect();
in = new InputStreamRead er(conn.getInpu tStream());
data = new BufferedReader( in);
/*FileReader file = new FileReader("sol ong16.java");
BufferedReader buff = new
BufferedReader( file);
boolean eof = false;*/
while ((line = data.readLine() ) != null) {
buf.append(line + "\n");
}
//buff.close();
} catch (IOException e) {
System.out.prin tln("Error -- " + e.toString());
}
letter = buf.toString();
lt = new TextArea(letter , 5,50, 1);
Color bk = new Color(10,10,25) ;
lt.setBackgroun d(bk);
lt.setForegroun d(Color.white);
lt.setEditable( false);
Font tf = new Font("Arial", Font.BOLD, 18);
lt.setFont(tf);
add(lt);
}
}[/code]The error (line 49) says "page" isn't found, so I think there is some naming or reference convention I'm missing.
I'm just trying this task to "get my feet wet" with java. I'm heavily entrenched in Visual Basic, and really wanted something completely different, and I'm having a hard time understanding what must be some real basics.
Jared
[code=java]import java.awt.*;
import java.awt.Font;
import java.awt.Graphi cs;
import java.awt.FontMe trics;
import java.io.*;
import java.net.*;
public class solong17 extends java.applet.App let {
String letter = new String();
public void paint(Graphics screen) {
//Color bk = new Color(0,0,0);
//Color tx = new Color(255,255,2 55);
Font f = new Font("Arial", Font.BOLD, 18);
FontMetrics fm = getFontMetrics( f);
setBackground(C olor.black);
screen.setColor (Color.white);
screen.setFont( f);
String s = "So long, and thanks for all the fish.";
int x = (size().width - fm.stringWidth( s)) / 2;
int y = size().height / 2;
screen.drawStri ng(s, x, y);
}
TextArea lt;
public void init() {
try {
URL page = new
URL("http://www.worksonline .org/works/default.htm");
}
catch (MalformedURLEx ception e) {
System.out.prin tln("Bad URL: http://www.worksonline .org" +
"/works/default.htm");
}
URLConnection conn = null;
InputStreamRead er in;
BufferedReader data;
StringBuffer buf = new StringBuffer();
String line;
//System.out.prin tln(page);
try {
conn = this.page.openC onnection(); //error is here
conn.connect();
in = new InputStreamRead er(conn.getInpu tStream());
data = new BufferedReader( in);
/*FileReader file = new FileReader("sol ong16.java");
BufferedReader buff = new
BufferedReader( file);
boolean eof = false;*/
while ((line = data.readLine() ) != null) {
buf.append(line + "\n");
}
//buff.close();
} catch (IOException e) {
System.out.prin tln("Error -- " + e.toString());
}
letter = buf.toString();
lt = new TextArea(letter , 5,50, 1);
Color bk = new Color(10,10,25) ;
lt.setBackgroun d(bk);
lt.setForegroun d(Color.white);
lt.setEditable( false);
Font tf = new Font("Arial", Font.BOLD, 18);
lt.setFont(tf);
add(lt);
}
}[/code]The error (line 49) says "page" isn't found, so I think there is some naming or reference convention I'm missing.
I'm just trying this task to "get my feet wet" with java. I'm heavily entrenched in Visual Basic, and really wanted something completely different, and I'm having a hard time understanding what must be some real basics.
Jared
Comment