Home:ALL Converter>How to make an image blink in a random position?

How to make an image blink in a random position?

Ask Time:2011-09-19T09:47:40         Author:papski

Json Formatter

I have an image inside the JApplet and I want it to appear in a random position. It will disappear after 1 second and appear again, in another random position.

How do I implement 'blinking in a random position'?

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class Random extends JApplet
{

 Image ball;

  public void init()
  {
    try
    {
        URL pic = new URL(getDocumentBase(), "ball.gif");
        ball = ImageIO.read(pic);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
   }

    public void paint(Graphics g)
    {
       if (ball != null)
      {
        g.drawImage(ball,50,50,50,50,this);
      }
    }
}

Author:papski,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7465737/how-to-make-an-image-blink-in-a-random-position
Andrew Thompson :

\n\nimport java.awt.*;\nimport java.awt.event.*;\nimport java.awt.image.*;\nimport javax.swing.*;\nimport java.util.Random;\n\nclass ImageBlinker extends JComponent {\n\n BufferedImage image;\n boolean showImage;\n int x = -1;\n int y = -1;\n Random r;\n\n ImageBlinker() {\n // put your image reading code here..\n image = new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB);\n Graphics g = image.createGraphics();\n g.setColor(Color.ORANGE);\n g.fillOval(0,0,32,32);\n // END - image read\n\n r = new Random();\n ActionListener listener = new ActionListener(){\n public void actionPerformed(ActionEvent ae) {\n if (image!=null) {\n if (!showImage) {\n int w = image.getWidth();\n int h = image.getHeight();\n int rx = getWidth()-w;\n int ry = getHeight()-h;\n if (rx>-1 && ry>-1) {\n x = r.nextInt(rx);\n y = r.nextInt(ry);\n }\n }\n showImage = !showImage;\n repaint();\n }\n }\n };\n Timer timer = new Timer(600,listener);\n timer.start();\n\n setPreferredSize(new Dimension(150,100));\n JOptionPane.showMessageDialog(null, this);\n timer.stop();\n }\n\n public void paintComponent(Graphics g) {\n g.setColor(Color.BLUE);\n g.fillRect(0,0,getWidth(),getHeight());\n if (showImage && image!=null) {\n g.drawImage(image,x,y,this);\n }\n }\n\n public static void main(String[] args) {\n SwingUtilities.invokeLater(new Runnable() {\n public void run() {\n new ImageBlinker();\n }\n });\n }\n}\n",
2011-09-19T05:56:32
yy