Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stopping Threads 2

Status
Not open for further replies.

psychoflea

Programmer
Dec 29, 2003
12
GB
I'm working on a traffic light system where users can input a time delay between light changes. The system is for 4 sets of lights each using its own thread process. I can start the threads but can't seem to get them to stop. the code is a bit rough at the mo

Any ideas would be very appreciated, THANKS

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;

public class Tlights extends JApplet implements ActionListener {


private JButton start1, start2, start3, start4, stop1, stop2, stop3, stop4;
private TextField input1, input2, input3, input4;
private int time1, time2, time3, time4;
private Trafa light;


public void init (){
setSize(675,650);
Container main = getContentPane();
main.setLayout(new BorderLayout());
Container titlebar = new Container();
titlebar.setLayout(new GridBagLayout());
Container startbut = new Container();
startbut.setLayout(new GridBagLayout());
Container userinput = new Container();
userinput.setLayout(new GridBagLayout());
GridBagConstraints a = new GridBagConstraints();
GridBagConstraints b = new GridBagConstraints();
Container stopbut = new Container();
stopbut.setLayout(new GridBagLayout());

String title = "Traffic Control System";
JLabel x = new JLabel(title, JLabel.RIGHT);
x.setForeground(Color.red);
b.insets = new Insets(10,100,0,0);
x.setFont(new Font("Serif", Font.BOLD, 50));
titlebar.add(x, b);


start1 = new JButton("Don't Walk");
a.gridx = 0;
a.gridy = 0;
a.insets = new Insets(0,100,10,30);
startbut.add(start1, a);
start1.addActionListener(this);

start2 = new JButton("Don't Walk");
a.gridx = 1;
a.gridy = 0;
a.insets = new Insets(0,30,10,30);
startbut.add(start2, a);
start2.addActionListener(this);

start3 = new JButton("Don't Walk");
a.gridx = 2;
a.gridy = 0;
a.insets = new Insets(0,30,10,30);
startbut.add(start3, a);
start3.addActionListener(this);

start4 = new JButton("Don't Walk");
a.gridx = 3;
a.gridy = 0;
a.insets = new Insets(0,30,10,100);
startbut.add(start4, a);
start4.addActionListener(this);

stop1 = new JButton("Walk");
a.gridx = 0;
a.gridy = 2;
a.insets = new Insets(0,100,10,30);
startbut.add(stop1, a);
stop1.addActionListener(this);

stop2 = new JButton("Walk");
a.gridx = 1;
a.gridy = 2;
a.insets = new Insets(0,30,10,30);
startbut.add(stop2, a);
stop2.addActionListener(this);

stop3 = new JButton("Walk");
a.gridx = 2;
a.gridy = 2;
a.insets = new Insets(0,30,10,30);
startbut.add(stop3, a);
stop3.addActionListener(this);

stop4 = new JButton("Walk");
a.gridx = 3;
a.gridy = 2;
a.insets = new Insets(0,30,10,100);
startbut.add(stop4, a);
stop4.addActionListener(this);

input1 = new TextField(2);
a.gridx = 0;
a.gridy = 1;
a.insets = new Insets(0,100,10,30);
startbut.add(input1, a);

input2 = new TextField(2);
a.gridx = 1;
a.gridy = 1;
a.insets = new Insets(0,30,10,30);
startbut.add(input2, a);

input3 = new TextField(2);
a.gridx = 2;
a.gridy = 1;
a.insets = new Insets(0,30,10,30);
startbut.add(input3, a);

input4 = new TextField(2);
a.gridx = 3;
a.gridy = 1;
a.insets = new Insets(0,30,10,100);
startbut.add(input4, a);

Graphics l = getGraphics();
Logo balls = new Logo(l);
balls.start();

main.add("North", titlebar);
main.add("Center", userinput);
main.add("South", startbut);



}

public void actionPerformed(ActionEvent event) {
if (event.getSource() == start1) {
time1 = Integer.parseInt (input1.getText());
start1.setEnabled(false);
Graphics g = getGraphics();
Trafa light = new Trafa(g, time1);
light.start();
}
if (event.getSource() == stop1) {
light.pleaseStop();
}
if (event.getSource() == start2) {
time2 = Integer.parseInt (input2.getText());
String string2 = input2.getText();
start2.setEnabled(false);
Graphics g = getGraphics();
Trafb t2 = new Trafb(g, time2);
t2.start();
}
if (event.getSource() == start3) {
time3 = Integer.parseInt (input3.getText());
String string3 = input3.getText();
start3.setEnabled(false);
Graphics g = getGraphics();
Trafc t3 = new Trafc(g, time3);
t3.start();
}
if (event.getSource() == start4) {
time4 = Integer.parseInt (input4.getText());
String string4 = input4.getText();
start4.setEnabled(false);
Graphics g = getGraphics();
Trafd t4 = new Trafd(g, time4);
t4.start();
}

}

}
class Logo extends Thread {

private Graphics l;
private int x = 30, xChange = 7;
private int y = 20, yChange = 5;
private int a = 30, aChange = 3;
private int b = 20, bChange = 2;
private int c = 30, cChange = 1;
private int d = 20, dChange = 8;
private int diameter = 15;

private int rectLeftX = 0, rectRightX = 100;
private int rectTopY = 0, rectBottomY = 100;

public Logo (Graphics graphics) {
l = graphics;
}

public void run() {


for (int n = 1; n< 1000; n++) {
l.setColor(Color.blue);
l.fillRect (1, 1, 99, 19);
l.setColor(Color.white);
l.drawString("T L C", 5, 15);
l.setColor(Color.black);
l.drawRect(rectLeftX, rectTopY, rectRightX-rectLeftX, rectBottomY-rectTopY);
l.setColor(Color.white);
l.fillRect (rectLeftX+1, rectTopY+20, rectRightX-1, rectBottomY-21);

if (x + xChange <= rectLeftX)
xChange = -xChange;
if (x + xChange + diameter >= rectRightX)
xChange = -xChange;

if (y + yChange <= rectTopY+20)
yChange = -yChange;
if (y + yChange + diameter >= rectBottomY)
yChange = -yChange;

x = x + xChange;
y = y + yChange;

l.setColor(Color.red);
l.fillOval (x, y, diameter, diameter);

//NEWBALL
if (a + aChange <= rectLeftX)
aChange = -aChange;
if (a + aChange + diameter >= rectRightX)
aChange = -aChange;

if (b + bChange <= rectTopY+20)
bChange = -bChange;
if (b + bChange + diameter >= rectBottomY)
bChange = -bChange;

a = a + aChange;
b = b + bChange;

l.setColor(Color.yellow);
l.fillOval (a, b, diameter, diameter);

//NEWBALL
if (c + cChange <= rectLeftX)
cChange = -cChange;
if (c + cChange + diameter >= rectRightX)
cChange = -cChange;

if (d + dChange <= rectTopY+20)
dChange = -dChange;
if (d + dChange + diameter >= rectBottomY)
dChange = -dChange;

c = c + cChange;
d = d + dChange;

l.setColor(Color.green);
l.fillOval (c, d, diameter, diameter);
try {
Thread.sleep(75);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
}
}
}
class Trafa extends Thread {

private Graphics g;
private int h;
private boolean keepGoing;

public Trafa (Graphics graphics, int aseconds) {

g = graphics;
h = aseconds;
keepGoing = true;
}

public void pleaseStop(){
keepGoing = false;
}

public void run() {

g.setColor(Color.black);
g.fillRect(47, 117, 131, 384);

while (keepGoing = true) {
g.setColor(Color.darkGray);
g.fillRect(50, 120, 125, 378);
g.setColor(Color.red);
g.fillOval(62, 132, 105, 105);

try {
Thread.sleep(h*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(50, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(62, 249, 105, 105);

try {
Thread.sleep(h*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(50, 120, 125, 378);
g.setColor(Color.green);
g.fillOval(62, 366, 105, 105);
try {
Thread.sleep(h*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(50, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(62, 249, 105, 105);

try {
Thread.sleep(h*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
}
}
}
class Trafb extends Thread {

private Graphics g;
public int i;


public Trafb (Graphics graphics, int bseconds) {

g = graphics;
i = bseconds;

}

public void run() {


g.setColor(Color.darkGray);
g.fillRect(200, 120, 125, 378);

for (int s = 1; s< 1000; s++) {
g.setColor(Color.darkGray);
g.fillRect(200, 120, 125, 378);
g.setColor(Color.red);
g.fillOval(212, 132, 105, 105);

try {
Thread.sleep(i*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(200, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(212, 249, 105, 105);

try {
Thread.sleep(i*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(200, 120, 125, 378);
g.setColor(Color.green);
g.fillOval(212, 366, 105, 105);
try {
Thread.sleep(i*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(200, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(212, 249, 105, 105);

try {
Thread.sleep(i*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
}
}
}
class Trafc extends Thread {

private Graphics g;
public int j;

public Trafc (Graphics graphics, int cseconds) {

g = graphics;
j = cseconds;

}

public void run() {


g.setColor(Color.darkGray);
g.fillRect(350, 120, 125, 378);

for (int u = 1; u< 1000; u++) {
g.setColor(Color.darkGray);
g.fillRect(350, 120, 125, 378);
g.setColor(Color.red);
g.fillOval(362, 132, 105, 105);

try {
Thread.sleep(j*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(350, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(362, 249, 105, 105);

try {
Thread.sleep(j*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(350, 120, 125, 378);
g.setColor(Color.green);
g.fillOval(362, 366, 105, 105);
try {
Thread.sleep(j*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(350, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(362, 249, 105, 105);

try {
Thread.sleep(j*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
}
}
}
class Trafd extends Thread {

private Graphics g;
public int k;

public Trafd (Graphics graphics, int dseconds) {

g = graphics;
k = dseconds;

}

public void run() {


g.setColor(Color.darkGray);
g.fillRect(500, 120, 125, 378);
g.setColor(Color.red);
g.fillOval(512, 132, 105, 105);
g.setColor(Color.yellow);
g.fillOval(512, 249, 105, 105);
g.setColor(Color.green);
g.fillOval(512, 366, 105, 105);

for (int u = 1; u< 1000; u++) {
g.setColor(Color.darkGray);
g.fillRect(500, 120, 125, 378);
g.setColor(Color.red);
g.fillOval(512, 132, 105, 105);

try {
Thread.sleep(k*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(500, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(512, 249, 105, 105);

try {
Thread.sleep(k*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(500, 120, 125, 378);
g.setColor(Color.green);
g.fillOval(512, 366, 105, 105);
try {
Thread.sleep(k*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(500, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(512, 249, 105, 105);

try {
Thread.sleep(k*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
}
}
}
 
// I have modified one of the traffic lights
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Tlights extends JApplet implements ActionListener {


private JButton start1, start2, start3, start4, stop1, stop2, stop3, stop4;
private TextField input1, input2, input3, input4;
private int time1, time2, time3, time4;
private Trafa light;


public void init (){
setSize(675,650);
Container main = getContentPane();
main.setLayout(new BorderLayout());
Container titlebar = new Container();
titlebar.setLayout(new GridBagLayout());
Container startbut = new Container();
startbut.setLayout(new GridBagLayout());
Container userinput = new Container();
userinput.setLayout(new GridBagLayout());
GridBagConstraints a = new GridBagConstraints();
GridBagConstraints b = new GridBagConstraints();
Container stopbut = new Container();
stopbut.setLayout(new GridBagLayout());

String title = "Traffic Control System";
JLabel x = new JLabel(title, JLabel.RIGHT);
x.setForeground(Color.red);
b.insets = new Insets(10,100,0,0);
x.setFont(new Font("Serif", Font.BOLD, 50));
titlebar.add(x, b);


start1 = new JButton("Don't Walk");
a.gridx = 0;
a.gridy = 0;
a.insets = new Insets(0,100,10,30);
startbut.add(start1, a);
start1.addActionListener(this);

start2 = new JButton("Don't Walk");
a.gridx = 1;
a.gridy = 0;
a.insets = new Insets(0,30,10,30);
startbut.add(start2, a);
start2.addActionListener(this);

start3 = new JButton("Don't Walk");
a.gridx = 2;
a.gridy = 0;
a.insets = new Insets(0,30,10,30);
startbut.add(start3, a);
start3.addActionListener(this);

start4 = new JButton("Don't Walk");
a.gridx = 3;
a.gridy = 0;
a.insets = new Insets(0,30,10,100);
startbut.add(start4, a);
start4.addActionListener(this);

stop1 = new JButton("Walk");
a.gridx = 0;
a.gridy = 2;
a.insets = new Insets(0,100,10,30);
startbut.add(stop1, a);
stop1.setEnabled(false);
stop1.addActionListener(this);

stop2 = new JButton("Walk");
a.gridx = 1;
a.gridy = 2;
a.insets = new Insets(0,30,10,30);
startbut.add(stop2, a);
stop2.addActionListener(this);

stop3 = new JButton("Walk");
a.gridx = 2;
a.gridy = 2;
a.insets = new Insets(0,30,10,30);
startbut.add(stop3, a);
stop3.addActionListener(this);

stop4 = new JButton("Walk");
a.gridx = 3;
a.gridy = 2;
a.insets = new Insets(0,30,10,100);
startbut.add(stop4, a);
stop4.addActionListener(this);

input1 = new TextField(2);
a.gridx = 0;
a.gridy = 1;
a.insets = new Insets(0,100,10,30);
startbut.add(input1, a);

input2 = new TextField(2);
a.gridx = 1;
a.gridy = 1;
a.insets = new Insets(0,30,10,30);
startbut.add(input2, a);

input3 = new TextField(2);
a.gridx = 2;
a.gridy = 1;
a.insets = new Insets(0,30,10,30);
startbut.add(input3, a);

input4 = new TextField(2);
a.gridx = 3;
a.gridy = 1;
a.insets = new Insets(0,30,10,100);
startbut.add(input4, a);

Graphics l = getGraphics();
Logo balls = new Logo(l);
balls.start();

main.add("North", titlebar);
main.add("Center", userinput);
main.add("South", startbut);



}

public void actionPerformed(ActionEvent event) {
if (event.getSource() == start1) {
input1.setText( (input1.getText()).trim() );
if ( (input1.getText()).length()>0 )
time1 = Integer.parseInt (input1.getText());
else
time1 = 2;

start1.setEnabled(false);
Graphics g = getGraphics();
light = new Trafa(g, time1);
light.start();
stop1.setEnabled(true);
}

if (event.getSource() == stop1) {
light.keepGoing=false;
light.interrupted();
}
/*
if (event.getSource() == start2) {
time2 = Integer.parseInt (input2.getText());
String string2 = input2.getText();
start2.setEnabled(false);
Graphics g = getGraphics();
Trafb t2 = new Trafb(g, time2);
t2.start();
}
if (event.getSource() == start3) {
time3 = Integer.parseInt (input3.getText());
String string3 = input3.getText();
start3.setEnabled(false);
Graphics g = getGraphics();
Trafc t3 = new Trafc(g, time3);
t3.start();
}
if (event.getSource() == start4) {
time4 = Integer.parseInt (input4.getText());
String string4 = input4.getText();
start4.setEnabled(false);
Graphics g = getGraphics();
Trafd t4 = new Trafd(g, time4);
t4.start();
}
*/
}

}
class Logo extends Thread {

private Graphics l;
private int x = 30, xChange = 7;
private int y = 20, yChange = 5;
private int a = 30, aChange = 3;
private int b = 20, bChange = 2;
private int c = 30, cChange = 1;
private int d = 20, dChange = 8;
private int diameter = 15;

private int rectLeftX = 0, rectRightX = 100;
private int rectTopY = 0, rectBottomY = 100;

public Logo (Graphics graphics) {
l = graphics;
}

public void run() {


for (int n = 1; n< 1000; n++) {
l.setColor(Color.blue);
l.fillRect (1, 1, 99, 19);
l.setColor(Color.white);
l.drawString("T L C", 5, 15);
l.setColor(Color.black);
l.drawRect(rectLeftX, rectTopY, rectRightX-rectLeftX, rectBottomY-rectTopY);
l.setColor(Color.white);
l.fillRect (rectLeftX+1, rectTopY+20, rectRightX-1, rectBottomY-21);

if (x + xChange <= rectLeftX)
xChange = -xChange;
if (x + xChange + diameter >= rectRightX)
xChange = -xChange;

if (y + yChange <= rectTopY+20)
yChange = -yChange;
if (y + yChange + diameter >= rectBottomY)
yChange = -yChange;

x = x + xChange;
y = y + yChange;

l.setColor(Color.red);
l.fillOval (x, y, diameter, diameter);

//NEWBALL
if (a + aChange <= rectLeftX)
aChange = -aChange;
if (a + aChange + diameter >= rectRightX)
aChange = -aChange;

if (b + bChange <= rectTopY+20)
bChange = -bChange;
if (b + bChange + diameter >= rectBottomY)
bChange = -bChange;

a = a + aChange;
b = b + bChange;

l.setColor(Color.yellow);
l.fillOval (a, b, diameter, diameter);

//NEWBALL
if (c + cChange <= rectLeftX)
cChange = -cChange;
if (c + cChange + diameter >= rectRightX)
cChange = -cChange;

if (d + dChange <= rectTopY+20)
dChange = -dChange;
if (d + dChange + diameter >= rectBottomY)
dChange = -dChange;

c = c + cChange;
d = d + dChange;

l.setColor(Color.green);
l.fillOval (c, d, diameter, diameter);
try {
Thread.sleep(75);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
}
}
}
class Trafa extends Thread {

private Graphics g;
private int h;
public volatile boolean keepGoing;

public Trafa (Graphics graphics, int aseconds) {

g = graphics;
h = aseconds;
keepGoing = true;
}

public void run() {

g.setColor(Color.black);
g.fillRect(47, 117, 131, 384);

while (keepGoing == true) {
g.setColor(Color.darkGray);
g.fillRect(50, 120, 125, 378);
g.setColor(Color.red);
g.fillOval(62, 132, 105, 105);

if (keepGoing == true)
{
try {
System.out.println("a");
Thread.sleep(h*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}

g.setColor(Color.darkGray);
g.fillRect(50, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(62, 249, 105, 105);
}
if (keepGoing == true)
{
try {
System.out.println("b");
Thread.sleep(h*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(50, 120, 125, 378);
g.setColor(Color.green);
g.fillOval(62, 366, 105, 105);
}
if (keepGoing == true)
{
try {
System.out.println("c");
Thread.sleep(h*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(50, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(62, 249, 105, 105);
}
if (keepGoing == true)
{
try {
System.out.println("d");
Thread.sleep(h*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
}// end if
}
}
}
class Trafb extends Thread {

private Graphics g;
public int i;


public Trafb (Graphics graphics, int bseconds) {

g = graphics;
i = bseconds;

}

public void run() {


g.setColor(Color.darkGray);
g.fillRect(200, 120, 125, 378);

for (int s = 1; s< 1000; s++) {
g.setColor(Color.darkGray);
g.fillRect(200, 120, 125, 378);
g.setColor(Color.red);
g.fillOval(212, 132, 105, 105);

try {
Thread.sleep(i*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(200, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(212, 249, 105, 105);

try {
Thread.sleep(i*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(200, 120, 125, 378);
g.setColor(Color.green);
g.fillOval(212, 366, 105, 105);
try {
Thread.sleep(i*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(200, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(212, 249, 105, 105);

try {
Thread.sleep(i*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
}
}
}
class Trafc extends Thread {

private Graphics g;
public int j;

public Trafc (Graphics graphics, int cseconds) {

g = graphics;
j = cseconds;

}

public void run() {


g.setColor(Color.darkGray);
g.fillRect(350, 120, 125, 378);

for (int u = 1; u< 1000; u++) {
g.setColor(Color.darkGray);
g.fillRect(350, 120, 125, 378);
g.setColor(Color.red);
g.fillOval(362, 132, 105, 105);

try {

Thread.sleep(j*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(350, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(362, 249, 105, 105);

try {
Thread.sleep(j*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(350, 120, 125, 378);
g.setColor(Color.green);
g.fillOval(362, 366, 105, 105);
try {

Thread.sleep(j*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(350, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(362, 249, 105, 105);

try {

Thread.sleep(j*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
}
}
}
class Trafd extends Thread {

private Graphics g;
public int k;

public Trafd (Graphics graphics, int dseconds) {

g = graphics;
k = dseconds;

}

public void run() {


g.setColor(Color.darkGray);
g.fillRect(500, 120, 125, 378);
g.setColor(Color.red);
g.fillOval(512, 132, 105, 105);
g.setColor(Color.yellow);
g.fillOval(512, 249, 105, 105);
g.setColor(Color.green);
g.fillOval(512, 366, 105, 105);

for (int u = 1; u< 1000; u++) {
g.setColor(Color.darkGray);
g.fillRect(500, 120, 125, 378);
g.setColor(Color.red);
g.fillOval(512, 132, 105, 105);

try {
Thread.sleep(k*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(500, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(512, 249, 105, 105);

try {
Thread.sleep(k*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(500, 120, 125, 378);
g.setColor(Color.green);
g.fillOval(512, 366, 105, 105);
try {
Thread.sleep(k*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
g.setColor(Color.darkGray);
g.fillRect(500, 120, 125, 378);
g.setColor(Color.yellow);
g.fillOval(512, 249, 105, 105);

try {
Thread.sleep(k*1000);
}

catch (InterruptedException e) {
System.err.println("Sleep exception");

}
}
}
}
 
Geee - what a spaghetti-code!

Did nobody tell you to use the code-tag?
Didn't you read the usage of this forum first?

Shame on you!

We here have a obvious smell-pattern which is known as: duplicate code.

Aren't Traf_a - Traf_d nearly identical?

And for your question, the funny logo is completely void.
But we nice forum-guys and -girls may find out on our self.
It's not the valuable time of the poster, which get's wasted!

Code:
// in the beginning, there was the member 'light'...
	private Trafa light;

	public void actionPerformed (ActionEvent event) 
	{
		if (event.getSource () == start1) 
		{
			time1 = Integer.parseInt (input1.getText ());
			start1.setEnabled (false);
			Graphics g = getGraphics ();
// Attention! here is a NEW definition of 'light'.
// - It's the same definition, but not the same ligth.
// It's only a local known light.
			Trafa light = new Trafa (g, time1);
			light.start ();
		}
/* here - the local known light is 'turned out'.
 Only the Membervariable ligth is known here, but that was nowhere initialised, and therefore we get this famous, seldom 'NullPointerException' by the runtime...
*/
		if (event.getSource () == stop1) 
		{
// ... here ...
			light.pleaseStop ();
		}
Change the line
Code:
	Trafa light = new Trafa (g, time1);
// to
	light = new Trafa (g, time1);
and your code should work.
But don't stop there.
Refactor your code, and only use one Traf - class.
Make 4 objects - trafa, trafb, trafc, trafd from it, or an Array of 4 Trafs.

Put the light and the corresponding button in a Panel, make an Class of this or at least a method, which you call 4 times.

And read some other posts in the forums.
How do experts ask question?
How do they show code?
What additional information do they give?
 
remove the line light.interrupted(); from my modified code
 
Yes your right, it was very sloppy of me. Sorry.

Thank you for all your help, I have done what you suggested and used 1 class to create the 4 objects

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top