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;
}
}
}
Benji - 06 Nov 2005 21:15 GMT
> 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
[quoted text clipped - 4 lines]
> 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.

Signature
Of making better designs there is no end,
and much refactoring wearies the body.
GRoll21 - 06 Nov 2005 21:34 GMT
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.
Benji - 06 Nov 2005 21:40 GMT
> 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.
http://www.seasite.niu.edu/cs580java/Animjava.html

Signature
Of making better designs there is no end,
and much refactoring wearies the body.
Andrew Thompson - 06 Nov 2005 21:49 GMT
...
> 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).
zero - 06 Nov 2005 21:36 GMT
"GRoll21" <mr.rygh@gmail.com> wrote in news:1131311075.242789.127740
@f14g2000cwb.googlegroups.com:
> 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
[quoted text clipped - 4 lines]
> what I have to do so it will use the thread and flash red and green
> that would be great. Thanks!
<snip>
> public void paint(Graphics g)
> {
<snip>
> g.setColor(new Color(34, 139, 34));
> g.setFont(font);
[quoted text clipped - 15 lines]
> }
> }
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.
GRoll21 - 06 Nov 2005 22:05 GMT
i understand what im trying to do but like the variable currentColor
where would i declare that and how?
GRoll21 - 06 Nov 2005 22:13 GMT
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!