Results 1 to 2 of 2
  1. #1
    fernando is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default How do erase image drawn

    Hi, How do i erase an image drawn in a window in java after a certain amount of time?

    Thanks.

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

    Default

    A "drawn" image suggests you are drawing the image in a paint-type method vis-a-vis showing it in a JLabel. AWT or Swing? I'll assume Swing since it's easier and more fun.
    Control the image visibility with a member variable boolean and use either a Swing Timer or a thread for the timing. Change the boolean after the elapsed time and call repaint.
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ImageErasure extends JPanel implements Runnable {
        BufferedImage image;
        boolean showImage = true;
    
        public ImageErasure(BufferedImage image) {
            this.image = image;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if(showImage)
                g.drawImage(image, 0, 0, this);
        }
    
        public void run() {
            int count = 5;
            while(count-- > 0) {
                try {
                    Thread.sleep(1000);
                } catch(InterruptedException e) {
                    System.out.println("interrupted");
                }
                System.out.println("count = " + count);
            }
            showImage = false;
            repaint();
        }
    
        private void start() {
            Thread thread = new Thread(this);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        public static void main(String[] args) throws IOException {
            String path = "images/Bird.gif";
            BufferedImage image = ImageIO.read(new File(path));
            ImageErasure test = new ImageErasure(image);
            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. Image denoising
    By sharonpriya in forum Advanced Java
    Replies: 2
    Last Post: 02-14-2011, 07:01 AM
  2. Image Verification
    By peiceonly in forum Java Servlet
    Replies: 2
    Last Post: 04-04-2009, 08:38 AM
  3. Replies: 0
    Last Post: 01-08-2008, 06:29 PM
  4. how to set an image size
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:27 PM
  5. 2D Array to image
    By fred in forum Java 2D
    Replies: 1
    Last Post: 07-24-2007, 02:52 AM

Posting Permissions

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