-
Java applet problem!
Hi,
I've been writing a java applet as part of an assignment at college, its purpose is to draw out numbers 1-9 when a value in this range is entered in a textfield and a button clicked. I'm using data string arrays to do this and i'm using the Eclipse development platform, the code is all correct from what i can see and the applet runs but for some reason it freezes when I try and get it to draw out a value? Any help much appreciated.
Code:
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Assign2 extends Applet implements ActionListener {
Button mybut;
TextField tf1;
String dataString[];
public void init ()
{
dataString = new String[10];
mybut = new Button ("draw");
tf1 = new TextField (10);
add (tf1);
add (mybut);
dataString[0]="E,S,W,N";
dataString[1]="S";
dataString[2]="W,S,E,W,N";
dataString[3]="E,S,W,E,S,W";
dataString[4]="S,E,N,S,S,N,E";
dataString[5]="W,S,E,S,W";
dataString[6]="W,S,E,S,W,N";
dataString[7]="W,S";
dataString[8]="N,W,S,E,S,W,N";
dataString[9]="E,N,W,S,S";
mybut.addActionListener (this);
}
public void actionPerformed (ActionEvent e)
{
int xpos, ypos;
xpos = 100;
ypos = 100;
String strArray[];
int numberSelected = Integer.parseInt(tf1.getText());
strArray = dataString[numberSelected].split(",");
for (int t=0;t<strArray.length;t++)
{
if (strArray[t].compareTo("N")==0)
{
drawNorth(xpos, ypos);
xpos-=100;
}
if (strArray[t].compareTo("S")==0)
{
drawSouth(xpos, ypos);
xpos-=100;
}
if (strArray[t].compareTo("E")==0)
{
drawEast(xpos, ypos);
xpos+=100;
}
if (strArray[t].compareTo("W")==0)
{
drawWest(xpos, ypos);
xpos+=100;
}
}
}
public void drawNorth(int x, int y)
{
Graphics g = getGraphics();
for (int n=y; n<x+100; n--)
{
g.drawLine (n, x, n, x-1);
try {
Thread.sleep (20);
} catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void drawEast (int x, int y)
{
Graphics g = getGraphics();
for (int n=x; n<x+100; n++)
{
g.drawLine (n-1, y, n, y);
try {
Thread.sleep (20);
} catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void drawWest (int x, int y)
{
Graphics g = getGraphics();
for (int n=x; n<x+100; n++)
{
g.drawLine (n-1, y, n, y);
try {
Thread.sleep (20);
} catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void drawSouth (int x, int y)
{
Graphics g = getGraphics();
for (int n=x; n<x+100; n++)
{
g.drawLine (n-1, y, n, y);
try {
Thread.sleep (20);
} catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
-
I've fixed freeze...
...but I think you must put more work in the drawing algorithms, they're not
quite up to scratch., kinda flat. And you may want to "wipe" the display
prior to each drawing.
Btw. this is a hybrid, an applet and an application
Code:
import java.awt.*;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.*;
public class Assign2
extends Applet
implements ActionListener, Runnable {
boolean isStandalone = false;
Button mybut;
TextField tf1;
String dataString[];
Thread runner=null;
public void init() {
dataString = new String[10];
mybut = new Button("draw");
tf1 = new TextField(10);
add(tf1);
add(mybut);
dataString[0] = "E,S,W,N";
dataString[1] = "S";
dataString[2] = "W,S,E,W,N";
dataString[3] = "E,S,W,E,S,W";
dataString[4] = "S,E,N,S,S,N,E";
dataString[5] = "W,S,E,S,W";
dataString[6] = "W,S,E,S,W,N";
dataString[7] = "W,S";
dataString[8] = "N,W,S,E,S,W,N";
dataString[9] = "E,N,W,S,S";
mybut.addActionListener(this);
}
public void run () {
int xpos, ypos;
xpos = 100;
ypos = 100;
int numberSelected = Integer.parseInt(tf1.getText());
String strArray[] = dataString[numberSelected].split(",");
for (int t = 0; t < strArray.length; t++) {
if (strArray[t].compareTo("N") == 0) {
drawNorth(xpos, ypos);
xpos -= 100;
}
if (strArray[t].compareTo("S") == 0) {
drawSouth(xpos, ypos);
xpos -= 100;
}
if (strArray[t].compareTo("E") == 0) {
drawEast(xpos, ypos);
xpos += 100;
}
if (strArray[t].compareTo("W") == 0) {
drawWest(xpos, ypos);
xpos += 100;
}
}
}
public void actionPerformed(ActionEvent e) {
if (runner!=null && runner.isAlive()) {
runner.interrupt();
}
runner=new Thread(this);
runner.start();
}
public void drawNorth(int x, int y) {
Graphics g = getGraphics();
for (int n = y; n < x + 100; n--) {
g.drawLine(n, x, n, x - 1);
try {
runner.sleep(20);
}
catch (InterruptedException e) {
break;
}
}
}
public void drawEast(int x, int y) {
Graphics g = getGraphics();
for (int n = x; n < x + 100; n++) {
g.drawLine(n - 1, y, n, y);
try {
runner.sleep(20);
}
catch (InterruptedException e) {
break;
}
}
}
public void drawWest(int x, int y) {
Graphics g = getGraphics();
for (int n = x; n < x + 100; n++) {
g.drawLine(n - 1, y, n, y);
try {
runner.sleep(20);
}
catch (InterruptedException e) {
break;
}
}
}
public void drawSouth(int x, int y) {
Graphics g = getGraphics();
for (int n = x; n < x + 100; n++) {
g.drawLine(n - 1, y, n, y);
try {
runner.sleep(20);
}
catch (InterruptedException e) {
break;
}
}
}
//Main method
public static void main(String[] args) {
Assign2 applet = new Assign2();
applet.isStandalone = true;
Frame frame = new Frame();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400, 320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation( (d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
}
eschew obfuscation
-
Hi, thanks for replying and amending the code, its definitely something I can learn from. However I can't really use it as its for an assignment, did you manage to get the code for the applet I originally posted working so it draws out numbers? I think theres something wrong with the threads because when I try and run the applet now it freezes up and Eclipse crashes? Any help much appreciated, thanks.
-
It runs w.out freezing here, so if it doesn't freeze on me, I have no way of detecting the error. Seems like an Eclipse thing to me. 
...well, apart from the fact that the drawing algorithm sux bigtime Is that
the real problem, or... ?
Btw, there is max one local thread running at any time in my code; runner.
Last edited by sjalle; 05-10-2005 at 10:51 AM.
eschew obfuscation
-
This code runs and draws out numbers 1-9 for you?:
Code:
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Assign2 extends Applet implements ActionListener {
Button mybut;
TextField tf1;
String dataString[];
public void init ()
{
dataString = new String[10];
mybut = new Button ("draw");
tf1 = new TextField (10);
add (tf1);
add (mybut);
dataString[0]="E,S,W,N";
dataString[1]="S";
dataString[2]="W,S,E,W,N";
dataString[3]="E,S,W,E,S,W";
dataString[4]="S,E,N,S,S,N,E";
dataString[5]="W,S,E,S,W";
dataString[6]="W,S,E,S,W,N";
dataString[7]="W,S";
dataString[8]="N,W,S,E,S,W,N";
dataString[9]="E,N,W,S,S";
mybut.addActionListener (this);
}
public void actionPerformed (ActionEvent e)
{
int xpos, ypos;
xpos = 100;
ypos = 100;
String strArray[];
int numberSelected = Integer.parseInt(tf1.getText());
strArray = dataString[numberSelected].split(",");
for (int t=0;t<strArray.length;t++)
{
if (strArray[t].compareTo("N")==0)
{
drawNorth(xpos, ypos);
xpos-=100;
}
if (strArray[t].compareTo("S")==0)
{
drawSouth(xpos, ypos);
xpos-=100;
}
if (strArray[t].compareTo("E")==0)
{
drawEast(xpos, ypos);
xpos+=100;
}
if (strArray[t].compareTo("W")==0)
{
drawWest(xpos, ypos);
xpos+=100;
}
}
}
public void drawNorth(int x, int y)
{
Graphics g = getGraphics();
for (int n=y; n<x+100; n--)
{
g.drawLine (n, x, n, x-1);
try {
Thread.sleep (20);
} catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void drawEast (int x, int y)
{
Graphics g = getGraphics();
for (int n=x; n<x+100; n++)
{
g.drawLine (n-1, y, n, y);
try {
Thread.sleep (20);
} catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void drawWest (int x, int y)
{
Graphics g = getGraphics();
for (int n=x; n<x+100; n++)
{
g.drawLine (n-1, y, n, y);
try {
Thread.sleep (20);
} catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void drawSouth (int x, int y)
{
Graphics g = getGraphics();
for (int n=x; n<x+100; n++)
{
g.drawLine (n-1, y, n, y);
try {
Thread.sleep (20);
} catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
;
Seems strange, what development environment are you using? Thanks.
-
Just to clarify, It doesn't even run, it freezes. But JBuilder has a red little button in
bottim left that kills anything.
So I posted my corrections and comments, mkay ? Why the code above doesn't execute right is not very interesting, mainly because there are a zillion**2 ways to do things wrong, and the code above is one of them.
No, my corrected version doesn't draw number-like shapes. The algorithm for drawing
lines is wrong. You should consentrate on that part, - in the code snippet i posted.
I cann't see any reason for eclipse being unable to handle a thread like
the one I've coded.
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks