Bhushankumar Lilapara
Bhushankumar Lilapara

Reputation: 780

how to change color of Circle in every second

import java.applet.*;
import java.awt.*;      // Graphics, Shape
import java.awt.geom.*; //Graphics2D
/*
<applet code = Oval1.class height=300 width=300 >
</applet>
*/
public class Oval1 extends Applet implements Runnable {
    Shape circle;
    Color c;
    public void init() {
        circle = new Ellipse2D.Float(90,100, 90, 90);
        repaint();
        Thread th = new Thread(this);
        th.start();
    }
    public void run() {
        try {
            while(true) {
                System.out.println(1);
                c = Color.cyan;
                repaint();
                Thread.sleep(1000);
                System.out.println(2);
                c = Color.gray;
                repaint();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public void update(Graphics g) {
        paint(g);
    }
    public void paint(Graphics g) {
        Graphics2D d = (Graphics2D) g;
        d.setColor(c);
        d.fill(circle);
    }
}

I am trying to create an applet that has a filled circle in middle of the applet & changing the color of circle in every second, means I want to show the circle like it's blinking.

I want to change color of circle in every second.

For this I use Shape class & Thread, but it's still not working.

I already try by using paint(g) by overriding update method..

This will not also effect

Upvotes: 2

Views: 10697

Answers (3)

Vijay
Vijay

Reputation: 8451

try out this..

Just need to put Thread.sleep(1000); after second repaint(), currently after second repaint immediately first repaint call so it over paint on second repaint();

import java.applet.*;
import java.awt.*;      // Graphics, Shape
import java.awt.geom.*; //Graphics2D
/*
<applet code = Oval1.class height=300 width=300 >
</applet>
*/
public class Oval1 extends Applet implements Runnable {
    Shape circle;
    Color c;
    public void init() {
        circle = new Ellipse2D.Float(90,100, 90, 90);
        repaint();
        Thread th = new Thread(this);
        th.start();
    }
    public void run() {
        try {
            while(true) {
                System.out.println(1);
                c = Color.cyan;
                repaint();
                Thread.sleep(1000);
                System.out.println(2);
                c = Color.gray;
                repaint();
                Thread.sleep(1000); 
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public void update(Graphics g) {
        paint(g);
    }
    public void paint(Graphics g) {
        Graphics2D d = (Graphics2D) g;
        d.setColor(c);
        d.fill(circle);
    }
}

Upvotes: 1

user1326628
user1326628

Reputation:

Add Thread.sleep(1000) after second repaint().

        while(true) {

            System.out.println(1);
            c = Color.cyan;
            repaint();
            Thread.sleep(1000);
            System.out.println(2);
            c = Color.gray;
            repaint();
            Thread.sleep(1000);
        }

Upvotes: 3

umert
umert

Reputation: 138

you can use Java Timer in place of Thread.sleep(). For example using of Timer, http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html. Thread.sleep() block paint method of applet.

Upvotes: 2

Related Questions