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;
}
}
}
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;
}
}
}