随机移动图像多次

时间:2017-04-30 12:35:59

标签: java swing random

我在这里多次移动图像找到了一个很好的答案:

https://stackoverflow.com/a/21534549/5485718

这对我想要在我的计划中实现的目标非常有帮助,但我还需要一件事。火球存储在列表中,当我们按空格键时它们被绘制。我想随机生成它们。有没有简单的方法呢?

主题代码:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import java.util.List;
import java.util.logging.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.Timer;

public class WannaBeStreetFighter extends JPanel {

private static final int D_W = 700;
private static final int D_H = 250;
private static final int X_INC = 10;

List<Fireball> fireBalls;
BufferedImage ryu;
BufferedImage fireball;
BufferedImage background;

public WannaBeStreetFighter() {

    try {
        ryu = ImageIO.read(new URL("http://www.sirlin.net/storage/street_fighter/ryu_hadoken_pose.png?__SQUARESPACE_CACHEVERSION=1226531909576"));
        background = ImageIO.read(new URL("http://fightingstreet.com/folders/variousinfofolder/ehondasbath/hondasfz3stage.gif"));
        fireball = ImageIO.read(new URL("http://farm6.staticflickr.com/5480/12297371495_ec19ded155_o.png"));
    } catch (IOException ex) {
        Logger.getLogger(WannaBeStreetFighter.class.getName()).log(Level.SEVERE, null, ex);
    }

    fireBalls = new LinkedList<>();

    Timer timer = new Timer(40, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Iterator<Fireball> it = fireBalls.iterator();
            while (it.hasNext()) {
                Fireball ball = it.next();
                if (ball.x > D_W) {
                    it.remove();
                    System.out.println(fireBalls.size());
                } else {
                    ball.x += X_INC;
                    repaint();
                }
            }
        }
    });
    timer.start();

    InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(KeyStroke.getKeyStroke("SPACE"), "hadouken");
    getActionMap().put("hadouken", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            fireBalls.add(new Fireball(fireball));
        }
    });

}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(background, 0, 0, D_W, D_H, this);
    g.drawImage(ryu, 50, 125, 150, 115, this);
    for (Fireball ball : fireBalls) {
        ball.drawFireball(g);
    }
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(D_W, D_H);
}

private class Fireball {

    Image fireball;
    int x = 150;
    int y = 125;

    public Fireball(Image image) {
        fireball = image;
    }

    public void drawFireball(Graphics g) {
        g.drawImage(fireball, x, y, 75, 50, null);
    }
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame("Best Street Fighter ever");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new WannaBeStreetFighter());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}
}

1 个答案:

答案 0 :(得分:3)

以下是一种随机创建球的方法:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class WannaBeStreetFighter extends JPanel {

    private static final int D_W = 700, D_H = 250, X_INC = 10;

    private final List<Fireball> fireBalls;
    private BufferedImage ryu, background;
    private final Random rand = new Random();

    public WannaBeStreetFighter() {

        try {
            //fighter image
            ryu = ImageIO.read(new URL("https://static1.squarespace.com/static/50f14d35e4b0d70ab5fc4f24/t/53f3260de4b0a13f9746a07c/1408443920310/ryu_hadoken_pose.png"));
            background = ImageIO.read(new URL("http://fightingstreet.com/folders/variousinfofolder/ehondasbath/hondasfz3stage.gif"));
        } catch (IOException ex) {  ex.printStackTrace();   }

        fireBalls = new LinkedList<>();

        Timer timer = new Timer(40, e -> {

            Iterator<Fireball> it = fireBalls.iterator();

            while (it.hasNext()) {
                Fireball ball = it.next();
                if (ball.x > D_W) {
                    it.remove();
                } else {
                    ball.x += X_INC;
                    repaint();
                }
            }

            addBalls();
        });

        timer.start();
    }

    private void addBalls() {

        if(rand.nextInt(100) > 5)  return; //reduce the frequency of adding balls
        fireBalls.add(new Fireball());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(background, 0, 0, D_W, D_H, this);
        g.drawImage(ryu, 50, 125, 150, 115, this);
        for (Fireball ball : fireBalls) {
            ball.drawFireball(g);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(D_W, D_H);
    }

    private class Fireball {

        private final Image fireball;
        private int x = 180;       
        private final int y = 140;
        private static final int SIZE =25;

        public Fireball() {
            fireball = getBall();
        }

        public void drawFireball(Graphics g) {
            g.drawImage(fireball, x, y, 25, 25, null);
        }

        private BufferedImage getBall() {
            BufferedImage img = new BufferedImage(SIZE , SIZE,
                                                BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = img.createGraphics();
            g2.setColor(Color.RED);
            g2.fillOval(1, 1, SIZE - 2, SIZE - 2);
            g2.dispose();
            return img;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Best Street Fighter ever");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new WannaBeStreetFighter());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}