Results 1 to 2 of 2
  1. #1
    angel_eyez is offline Member
    Join Date
    Jan 2008
    Posts
    2
    Rep Power
    0

    Default Flicker flicker!

    //christmas tree
    window.setColor(Color.green);
    int[] tCoordinates = {210, 190, 200, 189, 195, 185, 175, 180, 165, 175, 155};
    int[] sCoordinates = {230, 210, 210, 195, 195, 185, 195, 195, 210, 210, 230};
    window.fillPolygon(tCoordinates, sCoordinates, tCoordinates.length);

    Color newColor1 = new Color(88,58,31);
    window.setColor(newColor1);
    window.fillRect(173, 230, 15, 27);

    //christmas tree ornaments
    window.setColor(Color.red);
    window.fillOval(178, 200, 07, 05);

    Color newColor2 = new Color(15,89,4);
    window.setColor(newColor2);
    window.fillOval(181, 210, 07, 05);


    window.setColor(Color.red);
    window.fillOval(184, 220, 07, 05);



    How do I make the ornaments flicker a different color? (red, green, blue, yellow, etc. [animation]) Using Thread.sleep?
    Last edited by angel_eyez; 01-12-2008 at 08:26 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.util.Random;
    import javax.swing.*;
    
    public class FlickerTest extends JPanel implements Runnable {
        int[] tCoordinates = {
            210, 190, 200, 189, 195, 185, 175, 180, 165, 175, 155
        };
        int[] sCoordinates = {
            230, 210, 210, 195, 195, 185, 195, 195, 210, 210, 230
        };
        Color[] colors = {
            Color.red, Color.orange, Color.blue, Color.yellow,
            new Color(15,89,4)
        };
        Random seed = new Random();
        int[] indices = { 1, 2, 3 };
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Color.green);
            g2.fillPolygon(tCoordinates, sCoordinates, tCoordinates.length);
    
            g2.setColor(new Color(88,58,31));
            g2.fillRect(173, 230, 15, 27);
    
            //christmas tree ornaments
            g2.setColor(colors[indices[0]]);
            g2.fillOval(178, 200, 07, 05);
    
            g2.setColor(colors[indices[1]]);
            g2.fillOval(181, 210, 07, 05);
    
            g2.setColor(colors[indices[2]]);
            g2.fillOval(184, 220, 07, 05);
        }
    
        private void updateIndices() {
            for(int j = 0; j < indices.length; j++) {
                int index = indices[j];
                do {
                    index = seed.nextInt(colors.length);
                } while(index == indices[j]);
                indices[j] = index;
            }
        }
    
        private void start() {
            new Thread(this).start();
        }
    
        public void run() {
            boolean flickering = true;
            while(flickering) {
                try {
                    Thread.sleep(1000);
                } catch(InterruptedException e) {
                    flickering = false;
                }
                updateIndices();
                repaint();
            }
        }
    
        public static void main(String[] args) {
            FlickerTest test = new FlickerTest();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            test.start();
        }
    }

Similar Threads

  1. Alpha Fade Flicker
    By jamesfrize in forum Java Applets
    Replies: 3
    Last Post: 04-02-2008, 03:02 PM
  2. flicker
    By angel_eyez in forum New To Java
    Replies: 3
    Last Post: 01-14-2008, 11:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •