如何使图像在Java中从左向右移动

时间:2013-03-15 23:11:03

标签: java netbeans

到目前为止,我已经创建了下面的类,但是,现在我想插入一个代码,使图像从左到右,从右到左滑动。我通常会使用Sliding平台,但是当我尝试实现它时它不起作用。我仍然是java的初学者,所以我很感谢你的帮助。

这是我的Java类的代码:

package game;

import city.soi.platform.*;

public class Enemy extends Body implements CollisionListener{

private Game game ;

    public Enemy(Game g){
        super(g.getWorld(), new PolygonShape(-27.0f,25.0f, -27.0f,-24.0f, 25.0f,-24.0f, 26.0f,25.0f, -27.0f,25.0f));
        setImage(new BodyImage("images/enemy.png"));
        g.getWorld().addCollisionListener(this);    
        game = g;
    }

    public void collide(CollisionEvent e) {
        if (e.getOtherBody() == game.getPlayer()){
         game.getPlayer().subtractFromScore(75);
         System.out.println("You have just lossed 75 Points! Current Score =  " + game.getPlayer().getScore());
         this.destroy();
     }
  }
}

为了清楚,我希望每个人都将这个课程包含在一个从左到右移动的平台上。

非常感谢,

1 个答案:

答案 0 :(得分:2)

这很大程度上取决于您的个人要求,但基本概念将保持不变。

Swing中的任何类型的动画必须以不会阻止事件调度线程的方式执行。对EDT的任何阻止操作都将阻止处理任何重绘请求(以及其他内容)。

这个简单的例子使用javax.swing.Timer每40毫秒左右(大约25 fps)滴答并更新小“球”的位置

更复杂的迭代需要专用的Thread。这使整个过程变得更加复杂

  1. 您不应该从EDT以外的任何线程更新/创建/修改/更改任何UI组件(或UI可能需要执行绘制的属性)
  2. 您无法控制绘画过程。这意味着可以随时进行重新绘制,如果要修改绘制过程呈现游戏状态所需的任何属性/对象,则可能会导致不一致。
  3. public class SimpleBouncyBall {
    
        public static void main(String[] args) {
            new SimpleBouncyBall();
        }
    
        public SimpleBouncyBall() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new CourtPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class CourtPane extends JPanel {
    
            private Ball ball;
            private int speed = 5;
    
            public CourtPane() {
                Timer timer = new Timer(40, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Rectangle bounds = new Rectangle(new Point(0, 0), getSize());
                        if (ball == null) {
                            ball = new Ball(bounds);
                        }
                        speed = ball.move(speed, bounds);
                        repaint();
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(100, 100);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g); 
                if (ball != null) {
                    Graphics2D g2d = (Graphics2D) g.create();
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                    Point p = ball.getPoint();
                    g2d.translate(p.x, p.y);
                    ball.paint(g2d);
                    g2d.dispose();
                }
            }
    
        }
    
        public class Ball {
    
            private Point p;
            private int radius = 12;
    
            public Ball(Rectangle bounds) {
    
                p = new Point();
                p.x = 0;
                p.y = bounds.y + (bounds.height - radius) / 2;
    
            }
    
            public Point getPoint() {
                return p;
            }
    
            public int move(int speed, Rectangle bounds) {
    
                p.x += speed;
                if (p.x + radius >= (bounds.x + bounds.width)) {
    
                    speed *= -1;
                    p.x = ((bounds.x + bounds.width) - radius) + speed;
    
                } else if (p.x <= bounds.x) {
    
                    speed *= -1;
                    p.x = bounds.x + speed;
    
                }
    
                p.y = bounds.y + (bounds.height - radius) / 2;
    
                return speed;
    
            }
    
            public void paint(Graphics2D g) {
                g.setColor(Color.RED);
                g.fillOval(0, 0, radius, radius);
            }
    
        }
    
    }