Question:
Java for Dummies: Who can help me with this code?
Smily Cat
2013-06-06 03:53:32 UTC
I started learning Java from this Dutch book called "Programmeren in Java voor Dummies".
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);
}

}
Three answers:
AnalProgrammer
2013-06-06 10:10:58 UTC
Try using this 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 + 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;



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 final void paint(Graphics g) {

g.drawString(messageString, messageX, messageY);



}

}

}



Have fun.
cryer
2016-12-26 19:10:31 UTC
No. video games are deceptively complicated purposes. video games of any considerable length (or relaxing) generally require extra advantageous than merely one language. no longer merely do you want an application programming language (jointly with Java), you apart from might want issues like a database language (sq., Oracle, and so on.) to administration persistent guidance (ranges, rankings, and so on.) in a database. there are a number of different issues that would desire to be seen besides. in case you want to realize the point of programming required to construct your individual activity from scratch (for the main area), you are going to might desire to appreciate so plenty extra advantageous than merely Java. there are finished books written merely on what you are able to desire to appreciate with the intention to application video games. choosing up the technical understanding to do those issues isn't any ordinary activity. Hell, even information the time-honored technique itself is confusing and not employing a proper laptop technology (or appropriate) degree.
Tech
2013-06-06 04:16:33 UTC
thread.setPriority(Thread.MIN_PRI… Use semicolon here


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...