如何使图像在随机位置闪烁?

发布于 2024-12-05 03:51:08 字数 610 浏览 1 评论 0原文

我在 JApplet 中有一个图像,我希望它出现在随机位置。它会在 1 秒后消失,并在另一个随机位置再次出现。

如何实现“随机位置闪烁”?

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);
      }
    }
}

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);
      }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从此见与不见 2024-12-12 03:51:08

图像闪烁器

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Random;

class ImageBlinker extends JComponent {

    BufferedImage image;
    boolean showImage;
    int x = -1;
    int y = -1;
    Random r;

    ImageBlinker() {
        // put your image reading code here..
        image = new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.createGraphics();
        g.setColor(Color.ORANGE);
        g.fillOval(0,0,32,32);
        // END - image read

        r = new Random();
        ActionListener listener = new ActionListener(){
            public void actionPerformed(ActionEvent ae) {
                if (image!=null) {
                    if (!showImage) {
                        int w = image.getWidth();
                        int h = image.getHeight();
                        int rx = getWidth()-w;
                        int ry = getHeight()-h;
                        if (rx>-1 && ry>-1) {
                            x = r.nextInt(rx);
                            y = r.nextInt(ry);
                        }
                    }
                    showImage = !showImage;
                    repaint();
                }
            }
        };
        Timer timer = new Timer(600,listener);
        timer.start();

        setPreferredSize(new Dimension(150,100));
        JOptionPane.showMessageDialog(null, this);
        timer.stop();
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.BLUE);
        g.fillRect(0,0,getWidth(),getHeight());
        if (showImage && image!=null) {
            g.drawImage(image,x,y,this);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ImageBlinker();
            }
        });
    }
}

Image Blinker

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Random;

class ImageBlinker extends JComponent {

    BufferedImage image;
    boolean showImage;
    int x = -1;
    int y = -1;
    Random r;

    ImageBlinker() {
        // put your image reading code here..
        image = new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.createGraphics();
        g.setColor(Color.ORANGE);
        g.fillOval(0,0,32,32);
        // END - image read

        r = new Random();
        ActionListener listener = new ActionListener(){
            public void actionPerformed(ActionEvent ae) {
                if (image!=null) {
                    if (!showImage) {
                        int w = image.getWidth();
                        int h = image.getHeight();
                        int rx = getWidth()-w;
                        int ry = getHeight()-h;
                        if (rx>-1 && ry>-1) {
                            x = r.nextInt(rx);
                            y = r.nextInt(ry);
                        }
                    }
                    showImage = !showImage;
                    repaint();
                }
            }
        };
        Timer timer = new Timer(600,listener);
        timer.start();

        setPreferredSize(new Dimension(150,100));
        JOptionPane.showMessageDialog(null, this);
        timer.stop();
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.BLUE);
        g.fillRect(0,0,getWidth(),getHeight());
        if (showImage && image!=null) {
            g.drawImage(image,x,y,this);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ImageBlinker();
            }
        });
    }
}
苏大泽ㄣ 2024-12-12 03:51:08

为了我的钱,我将图像放在 ImageIcon 中,将 ImageIcon 放在 JLabel 中,然后使用 Swing Timer 和 Random 对象随机移动 JLabel。您必须将其移动到布局已设置为 null 的容器中(contentPane 即可),并且您必须将 JLabel 的大小指定为它的首选大小才能正常工作。

For my money, I'd put the Image in an ImageIcon, the ImageIcon in a JLabel, and then use a Swing Timer and a Random object to randomly move the JLabel about. You'd have to move it in a container (the contentPane will do) whose layout has been set to null, and you'll have to specify the size of the JLabel as its preferredSize for this to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文