Thread in Java Applet. Not Working?

Discussion in 'Java' started by GRoll21, Nov 6, 2005.

  1. GRoll21

    GRoll21 Guest

    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;
    }
    }
    }
     
    GRoll21, Nov 6, 2005
    #1
    1. Advertising

  2. GRoll21

    Benji Guest

    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.
     
    Benji, Nov 6, 2005
    #2
    1. Advertising

  3. GRoll21

    GRoll21 Guest

    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.
     
    GRoll21, Nov 6, 2005
    #3
  4. GRoll21

    zero Guest

    "GRoll21" <> 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.
     
    zero, Nov 6, 2005
    #4
  5. GRoll21

    Benji Guest

    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.
     
    Benji, Nov 6, 2005
    #5
  6. 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).
     
    Andrew Thompson, Nov 6, 2005
    #6
  7. GRoll21

    GRoll21 Guest

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

    where would i declare that and how?
     
    GRoll21, Nov 6, 2005
    #7
  8. GRoll21

    GRoll21 Guest

    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!
     
    GRoll21, Nov 6, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. webster
    Replies:
    0
    Views:
    877
    webster
    Jul 20, 2003
  2. Krista
    Replies:
    3
    Views:
    2,345
    Andrew Thompson
    Sep 15, 2004
  3. Archana
    Replies:
    1
    Views:
    1,398
    Andrew Thompson
    Oct 25, 2004
  4. Replies:
    0
    Views:
    579
  5. Cyron
    Replies:
    3
    Views:
    903
    Roedy Green
    Oct 5, 2007
Loading...

Share This Page