Thread in Java Applet. Not Working?

G

GRoll21

I will show my code. This is my first time working with threads. So I
just thought I would do something simple and i want the MERRY
CHRISTMAS! .. to be painted green. then using a thread I want to make
it flash red. with a delay of 1 second. so it will flash red and green
every 1 second. I tried looking up examples but I can't get it to work.
It compiles just fine but it just stays green. if someone can tell me
what I have to do so it will use the thread and flash red and green
that would be great. Thanks!

// program to demonstrate the use of sound in an applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;

public class snow extends Applet implements Runnable
{

Thread thread1;

// initialize an array of descriptions

Image bear, penguin, bird, zebra;

Button button1;
Button button2;


// override init method to display the images

public void init()
{

if (thread1==null)
{
thread1 = new Thread(this);
thread1.start();
}

setLayout(null);
setBackground(new Color(135,206,250));

// create buttons

//button1 = new Button("Smile");
// button1.setLocation(150, 500);
// button1.setSize(60,40);
// button1.setForeground(Color.black);
// button1.setBackground(Color.red);
// button1.addActionListener(this);
// add(button1);

// button2 = new Button("Frown");
// button2.setLocation(375, 500);
// button2.setSize(60,40);
// button2.setForeground(Color.black);
// button2.setBackground(Color.red);
// button2.addActionListener(this);
// add(button2);


Font font = new Font("Monospaced",Font.BOLD,34);
}

// display images on the screen

public void paint(Graphics g)
{

Font font = new Font("Monospaced",Font.BOLD,34);
g.setColor(Color.white);
g.fill3DRect(0,560,600,50,true);
g.setColor(Color.white);
g.fillArc(0, 555, 50, 50, 0, 360);
g.setColor(Color.white);
g.fillArc(100, 555, 400, 400, 0, 360);
g.setColor(Color.white);
g.fillArc(200, 555, 500, 500, 0, 360);
g.setColor(Color.white);
g.fillArc(100, 375, 200, 200, 0, 360);
g.setColor(Color.white);
g.fillArc(110, 225, 175, 175, 0, 360);
g.setColor(Color.white);
g.fillArc(120, 95, 150, 150, 0, 360);
g.setColor(Color.black);
g.fillArc(160, 130, 10, 10, 0, 360);
g.setColor(Color.black);
g.fillArc(215, 130, 10, 10, 0, 360);
g.setColor(Color.ORANGE);
g.fillArc(187, 165, 10, 10, 0, 180);
g.setColor(Color.black);
g.fillArc(187, 190, 10, 10, 0, 360);
g.setColor(Color.black);
g.fillArc(177, 188, 10, 10, 0, 360);
g.setColor(Color.black);
g.fillArc(197, 188, 10, 10, 0, 360);

g.setColor(Color.black);
g.fillArc(167, 186, 10, 10, 0, 360);
g.setColor(Color.black);
g.fillArc(207, 186, 10, 10, 0, 360);


g.setColor(new Color(139,101,8));
g.drawLine(280,300,350,225);
g.drawLine(325,250,350,280);
g.drawLine(325,250,300,220);

g.drawLine(115,300,45,225);
g.drawLine(70, 250,45,280);
g.drawLine(70, 250,95,220);
g.setColor(new Color(34, 139, 34));
g.setFont(font);
g.drawString("MERRY CHRISTMAS!", 275,125);
}

// use an implementation of the actionPerformed method taken
// from the ActionListener abstract class



public void run()
{
while (true)
{
Graphics g = getGraphics();
g.setColor(Color.red);
g.drawString("MERRY CHRISTMAS!", 275,125);

repaint();

try{Thread.sleep(1000);}
catch(InterruptedException i) {System.exit(1);}
}



}

public void destroy()
{
if (thread1 !=null)
{
thread1.stop();
thread1 = null;
}
}
}
 
B

Benji

GRoll21 said:
I will show my code. This is my first time working with threads. So I
just thought I would do something simple and i want the MERRY
CHRISTMAS! .. to be painted green. then using a thread I want to make
it flash red. with a delay of 1 second. so it will flash red and green
every 1 second. I tried looking up examples but I can't get it to work.
It compiles just fine but it just stays green. if someone can tell me
what I have to do so it will use the thread and flash red and green
that would be great. Thanks!

1) you shouldn't be making blinking red and green text. it's an eyesore.
2) you should always do your drawing inside of the paintComponent method.
so, have like a Color variable, and draw the text in that color. Then,
in the thread, set the color to either red or green, and then call
repaint().
3) you shouldn't be making blinking red and green text. it's an eyesore.
 
G

GRoll21

i understand 2 and 3. ha.. but can you give me an example for what i
would dof or #2? i appreciate the help. thank you for your time.
 
Z

zero

I will show my code. This is my first time working with threads. So I
just thought I would do something simple and i want the MERRY
CHRISTMAS! .. to be painted green. then using a thread I want to make
it flash red. with a delay of 1 second. so it will flash red and green
every 1 second. I tried looking up examples but I can't get it to work.
It compiles just fine but it just stays green. if someone can tell me
what I have to do so it will use the thread and flash red and green
that would be great. Thanks!

public void paint(Graphics g)
{

g.setColor(new Color(34, 139, 34));
g.setFont(font);
g.drawString("MERRY CHRISTMAS!", 275,125);
}

public void run()
{
while (true)
{
Graphics g = getGraphics();
g.setColor(Color.red);
g.drawString("MERRY CHRISTMAS!", 275,125);

repaint();

try{Thread.sleep(1000);}
catch(InterruptedException i) {System.exit(1);}
}
}

Have a good look at your run method. You start by drawing a red string.
Then you call repaint(), which in turn calls paint(g). paint draws a
green string. This happens immediately after drawing the red string -
it's so fast that you never see the red. That's why it appears to stay
green.

Instead, follow Benji's advice. In paint use g.setColor(currentColor);
and in run use currentColor = Color.RED or Color.GREEN.

oh btw
you shouldn't be making blinking red and green text. it's an eyesore.
 
A

Andrew Thompson

Benji wrote:
....
1) you shouldn't be making blinking red and green text. it's an eyesore.

Good point, but sometimes it's best to make 'effects' in test
GUI's bleedingly, garishly obvious for debugging purposes
(that, and sigalling alien ships).
2) you should always do your drawing inside of the paintComponent method.

Not in AWT, for AWT you should override paint(Graphics) [ as
the OP did ] whereas in a Swing based JApplet, you should override
paintComponent(Graphics).
 
G

GRoll21

i understand what im trying to do but like the variable currentColor

where would i declare that and how?
 
G

GRoll21

nevermind. i figured it out. just had to create that bool variable.
works like a charm and i understand now. thanks everyone for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top