Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Thread in Java Applet. Not Working?

Reply
Thread Tools

Thread in Java Applet. Not Working?

 
 
GRoll21
Guest
Posts: n/a
 
      11-06-2005
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,);
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;
}
}
}

 
Reply With Quote
 
 
 
 
Benji
Guest
Posts: n/a
 
      11-06-2005
GRoll21, while high on whiteboard markers, wrote:
> 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.

--
Of making better designs there is no end,
and much refactoring wearies the body.
 
Reply With Quote
 
 
 
 
GRoll21
Guest
Posts: n/a
 
      11-06-2005
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.

 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      11-06-2005
"GRoll21" <(E-Mail Removed)> 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
> 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!
>


<snip>

> public void paint(Graphics g)
> {


<snip>

> 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.
 
Reply With Quote
 
Benji
Guest
Posts: n/a
 
      11-06-2005
GRoll21, while high on whiteboard markers, wrote:
> 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

--
Of making better designs there is no end,
and much refactoring wearies the body.
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-06-2005
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).
 
Reply With Quote
 
GRoll21
Guest
Posts: n/a
 
      11-06-2005
i understand what im trying to do but like the variable currentColor

where would i declare that and how?

 
Reply With Quote
 
GRoll21
Guest
Posts: n/a
 
      11-06-2005
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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
html5lib not thread safe. Is the Python SAX library thread-safe? John Nagle Python 5 03-12-2012 04:07 PM
os.ChDir() not thread-safe; was : Is tempfile.mkdtemp() thread-safe? Gabriel Rossetti Python 0 08-29-2008 08:30 AM
why we should call Thread.start(),not directly call Thread.run()? junzhang1983@gmail.com Java 5 06-20-2008 03:12 PM
To thread or not to thread Sidd Python 4 07-30-2005 09:08 AM
Terminating program run from thread (not just the thread) Jeffrey Barish Python 0 05-28-2004 02:02 AM



Advertisments