Results 1 to 4 of 4
  1. #1
    gleb is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default Please Help Me FLASH

    Hello, I hope someone can help my get an image in my JPanel flash (on/off) at a predetermined time interval.
    For instance, I want it to flash on/off every second. I tried writing the code within a thread like:

    class..... extends Thread
    .
    .
    try{
    for(int i = 0; i < 10000; i++){
    panel.add(image);
    Thread.sleep(1000);
    panel.remove(image); //panel.add(noImage)
    }
    }catch InterruptedException e)......

    But this only seems to wait the one (1) second until the program runs, and it doesn't actually paint the image. Also, as the image would be removed, I know that all other components would collapse and get funky, so I created another image that is the color of the background, and tried the same thing with it (it's commented) as well as removed it. But the program still idles, and doesn't paint images. I hope someone has a snippet of code or at least share a pointer with me to get me going. I'm a beginner, and any help would be appreciated. Thank you, Gleb

  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.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class Blinking implements Runnable {
        ImageIcon icon;
        JLabel label;
        long delay = 2000;
    
        public void run() {
            int counter = 0;
            while(true) {
                try {
                    Thread.sleep(delay);
                } catch(InterruptedException e) {
                    break;
                }
                if(counter++ % 2 == 0)
                    label.setIcon(null);
                else
                    label.setIcon(icon);
                label.repaint();
            }
        }
    
        private void start() {
            Thread thread = new Thread(this);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        private JLabel getContent(BufferedImage image) {
            icon = new ImageIcon(image);
            label = new JLabel(icon, JLabel.CENTER);
            label.setBorder(BorderFactory.createEtchedBorder());
            label.setPreferredSize(label.getPreferredSize());
            return label;
        }
    
        public static void main(String[] args) throws IOException {
            String path = "images/hawk.jpg";
            BufferedImage image = ImageIO.read(new File(path));
            Blinking test = new Blinking();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLayout(new GridBagLayout());
            f.add(test.getContent(image), new GridBagConstraints());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            test.start();
        }
    }

  3. #3
    gleb is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default Thank you hardwired

    I will try this code. That you hardwired!!! Your prompt reply is nice.

  4. #4
    gleb is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default That is THANK YOU

    THANK YOU hardwired, not "That you". There we go. By the way, the method "createEtchedBorder()", I am assuming addresses the issue for when the image is removed, and screen is repainted(cool), it won't distort the way everythink else is packed. Really Cool.

Similar Threads

  1. JAVA, Ajax, Flash
    By Digital Dispatching in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-06-2008, 08:49 AM
  2. put a flash file in java programming?
    By notherand in forum New To Java
    Replies: 2
    Last Post: 01-23-2008, 05:36 AM
  3. Flash toolbar
    By lenny in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:17 AM
  4. Replies: 0
    Last Post: 07-25-2007, 10:04 AM
  5. 123 Flash Chat 6.6
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-05-2007, 01:07 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
  •