动画背景java游戏
我正在编写一个类似于小行星的游戏,但我不明白如何在背景中产生小行星。
现在我在主类中产生了一颗小行星,但我想为小行星创造一个类吗?
主类
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; if(flag_img) { background(g2d); logo(g2d); menu(g2d); spaceship(g2d); crediti(g2d); } }
背景function(现在)
private void background(Graphics2D g2d) { asteroidi_g_x+=r.nextInt(4); asteroidi_g_y+=r.nextInt(1); g2d.drawImage(asteroidi_g[0], asteroidi_g_x,asteroidi_g_y,this); }
背景function(我想要的)
private void background(Graphics2D g2d) { asteroid asteroid = new asteroid[10]; }
和类小行星
public class asteroid extends JPanel implements ActionListener { private BufferedImage images_asteroid; private boolean flag_img; private JPanel jp; private int x,y; public asteroide_grande(JPanel jp) { flag_img = true; x = (jp.getWidth()/2); y = (jp.getHeight()/2); this.jp = jp; try { images_asterod = ImageIO.read(this.getClass().getResource("images/asteroid/a1.png")); } catch(IOException e){flag = false;} } @Override public void paintComponent(Graphics g) { super.paintComponent(g); if(flag_img) { g.drawImage(images_asteroid, 100, 100,this); } } @Override public void actionPerformed(ActionEvent e) { x=x-1; y=y+1; repaint(); }
类中的方法paintcomponent不起作用
-
没有你的
Asteroid
类扩展JPanel
。 而是将它作为模型的小行星数据并具有数据操作方法的类。 您还需要一个采用Graphic
上下文的draw
方法。 就像是public class Asteroid { Image asteroidImage; JPanel panel; int x, y; public Asteroid(JPanel panel, Image image, int x, int y) { this.panel = panel; this.asteroidImage = image; this.x = x; this.y = y; } public void drawAsteroid(Graphics g) { g.drawImage(asteroidImage, x, y, panel); } public void move() { x += 5; } }
-
现在你有了一个小行星模型,你可以创建一个
Asteriod
对象List
并迭代它们并使用它的drawAsteroid
方法来绘制它们。 就像是public class GamePanel extends JPanel { List
asteroids; Image asteroidImage; public GamePanel(){ asteroidImage = ... asteroids = new ArrayList<>(); asteroids.add(new Asteroid(GamePanel.this, asteroidImage, 100, 100)); // add more asteriods } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); for (Asteriod asteroid: asteriods) { asteriod.drawAsteroid(g); } } } -
要为它们设置动画,您需要使用
javax.swing.Timer
。 有关如何使用摆动计时器的更多信息,请参阅。 您需要在Timer
操作Asteriod
数据。 使用上面提供的代码,您可以调用它的move
方法,然后调用repaint()
。 就像是public GamePanel(){ ... Timer timer = new Timer(30, new ActionListener(){ public void actionPerformed(ActionEvent e) { Iterator it = asteroids.iterator(); while (it.hasNaext()) { Asteroid asteriod = (Asteroid)it.next(); asteroid.move(); } } }); }
你可以在这里 , 这里 , 这里 , 这里和这里看到更多完整的动画实例
这是一个完整的例子。 你会看到我在Astreroid
类中包含了一个Rectangle2D
对象。 那就是你想要检查碰撞检测。 你应该使用x
和y
的每个Asreroid
运动移动Rectangle2D
x
和/或y
。 然后你可以检查asteroid.rectangle.intersects(someOtherObject)
import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.Timer; public class AsteroidBackground extends JPanel { private static final int D_W = 400; private static final int D_H = 600; BufferedImage asteroidImage; BufferedImage background; List asteroids; Random random = new Random(); int countToAddAsteroid = 0; int y; public AsteroidBackground() { try { asteroidImage = ImageIO.read(getClass().getResource("/resources/small-asteroid.png")); background = ImageIO.read(getClass().getResource("/resources/space.png")); } catch (IOException ex) { Logger.getLogger(AsteroidBackground.class.getName()).log(Level.SEVERE, null, ex); } asteroids = new ArrayList<>(); y = 0 - asteroidImage.getHeight(); Timer timer = new Timer(40, new ActionListener(){ public void actionPerformed(ActionEvent e) { if (countToAddAsteroid >= 25) { int randX = random.nextInt(D_W); asteroids.add(new Asteroid(AsteroidBackground.this, asteroidImage, randX, y)); countToAddAsteroid = 0; } countToAddAsteroid++; Iterator it = asteroids.iterator(); while (it.hasNext()) { Asteroid asteroid = (Asteroid)it.next(); if (asteroid.y >= D_H) { it.remove(); } else { asteroid.move(); } } repaint(); } }); timer.start(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(background, 0, 0, this); for (Asteroid asteroid : asteroids) { asteroid.drawAsteroid(g); } } @Override public Dimension getPreferredSize() { return new Dimension(D_W, D_H); } public class Asteroid { Rectangle2D rectangle; Image asteroidImage; JPanel panel; int x, y; public Asteroid(JPanel panel, Image image, int x, int y) { this.panel = panel; this.asteroidImage = image; this.x = x; this.y = y; rectangle = new Rectangle2D.Double( x, y, image.getWidth(panel), image.getHeight(panel)); } public void drawAsteroid(Graphics g) { g.drawImage(asteroidImage, x, y, panel); } public void move() { y += 5; } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame(); frame.add(new AsteroidBackground()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } }