Smily Cat
2013-06-06 03:53:32 UTC
It is quite an old book, from 1996 but most of the information in there still is relevant I think.
Only with some parts of applet codes they give you in the book there are some errors, like ".... cannot be resolved to a variable", and since I am still a newb when it comes to programming (in Java) I have no idea how to fix this, also the codes they give you are complete and (probably back in 1996) working without errors. Can someone paste this code in Eclipse and check the errors it gives? When I try to run the code it gives:
java.lang.VerifyError: Method expects a return value in method TickerApplet.paint(Ljava/awt/Graphics;)V at offset 8
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Can somebody tell me what I can do to fix this code because I want to see what the applet looks like when it's running.
//Here is the code
import java.awt.*;
import java.applet.Applet;
import java.net.*;
import java.io.*;
public class TickerApplet extends Applet {
private final static char URLSeperator = '/';
private String messageFile = "message.txt";
private TickerTape tickerTape = new TickerTape();
private String fetchMessageString() {
String messageString = new String();
String urlString = getCodeBase().toString();
urlString = urlString.substring(0,urlString.lastIndexOf(URLSeperator)) + URLSeperator + messageFile;
try {
URL url = new URL(urlString);
InputStream inStream = url.openStream();
DataInputStream dataStream = new DataInputStream(new BufferedInputStream(inStream));
String inLine = null;
while ((inLine = dataStream.readLine()) != null) {
messageString += inLine;
}
}
catch(MalformedURLException e) {
showStatus("Invalid URL: " + urlString);
}
catch(IOException e) {
showStatus("Error " + e);
}
return messageString;
}
public void init() {
add(tickerTape);
tickerTape.setText(fetchMessageString());
tickerTape.start();
}
public void stop() {
tickerTape.stop();
}
class TickerTape extends Canvas implements Runnable {
private Dimension preferredDimension = new Dimension(400,10);
private int messageWidth;
private int messageX;
private int messageY;
private int currentX;
private int sleepyTime = 50;
private int scrollStep = 2;
private final static int INSET = 2;
private String messageString = "Default Message";
private Thread thread;
private boolean isRunning = false;
public TickerTape() {
thread = null;
setBackground(Color.blue);
setForeground(Color.white);
setFont(new Font("TimesRoman", Font.BOLD, 14));
}
public void run() {
while (isRunning) {
scroll();
try {
Thread.sleep(sleepyTime);
}
catch(InterruptedException e) {
}
}
}
public void stop() {
if((thread != null) && thread.isAlive()) {
thread.stop();
{thread = null;
isRunning = false;
}
}
public void start() {
resize(preferredDimension);
messageX = bounds().width;
//System.out.println('startx='+message;//db
if (thread == null) {
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
}
isRunning = true;
thread.start();
}
private void scroll() {
if(messageX < (-messageWidth)) {
messageX = bounds().width ;
}
else {
messageX = messageX - scrollStep;
}
repaint();
}
}
public void setText(String s) {
messageString = new String(s);
FontMetrics fontMetrics = getGraphics().getFontMetrics();
messageWidth = fontMetrics.stringWidth(messageString);
messageY = fontMetrics.getHeight() - fontMetrics.getDescent() + INSET;
preferredDimension.height = fontMetrics.getHeight() + (INSET * 2);
}
public Dimension preferredSize() {
return preferredDimension;
}
public Dimension minimumSize() {
return preferredDimension;
}
public finalvoid paint(Graphics g) {
g.drawString(messageString,messageX,messageY);
}
}