有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在屏幕上移动对象

我试图在屏幕上同时移动两个球体。然而,它们并不是相互独立移动的。相反,一个依赖于另一个的运动。请帮帮我。代码如下:-

Draw

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class Draw extends JPanel implements ActionListener
    {
        Timer timer[] = new Timer[2];
        int velX = 2, velY = 2;
        Sphere sphere[] = new Sphere[2];
        public Draw()
        {
            for(int i=0;i<2;i++)
            {
                sphere[i] = new Sphere();
                timer[i] = new Timer(5,this);
            }
        }
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            for(int i=0;i<2;i++)
                drawing(g,sphere[i].color,sphere[i].radius);
        } 
        public void drawing(Graphics g,Color color, int radius)
        {
             for(int i=0;i<2;i++){
                g.setColor(color);
                g.fillOval(sphere[i].x,sphere[i].y,radius*2,radius*2);
                timer[i].start();
            }
        }
        public void actionPerformed(ActionEvent evt)
        {
            for(int i=0;i<2;i++){
            if(sphere[i].x<0 || sphere[i].x>970)
            {
                velX = -velX;
            }
            if(sphere[i].y<0 || sphere[i].y>650)
            {
                velY = -velY;
            }
            sphere[i].x += velX;
            sphere[i].y += velY;
            repaint();
        }
        }
        public static void main(String args[])
        {
            JFrame jf = new JFrame("Renderer");
            jf.getContentPane().add(new Draw(),BorderLayout.CENTER);
            jf.setBounds(0,0,1024,720);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.setVisible(true);
        }
    }

Sphere

    import java.awt.Color;
    class Sphere
    {
        public int x;
        public int y;
        public int radius;
        Color color;
        public Sphere()
        {
            this.x = (int)(Math.random()*800);
            this.y = (int)(Math.random()*600);
            this.radius = (int)(Math.random()*50);
            this.color = new Color((int)(Math.random()*255),
    (int)(Math.random()*255),(int)(Math.random()*255));
        }
        public Sphere(int x,int y,int radius, Color color)
        {
            this.x = x;
            this.y = y;
            this.radius = radius;
            this.color = color;
        }
    }

共 (3) 个答案

  1. # 1 楼答案

    两个球体的速度相同,所以它们的运动会相似

    int velX = 2, velY = 2;
    

    如果在球体创建过程中,为每个球体设置一个随机速度,则运动将不同

  2. # 2 楼答案

    两个对象的x/y增量相同,例如,需要在Sphere对象中包含这些值

    class Sphere {
    
        int velX;
        int velY;
    
        public int x;
        public int y;
        public int radius;
        Color color;
    
        public Sphere(int xDelta, int yDelta) {
            velX = xDelta;
            velY = yDelta;
            this.x = (int) (Math.random() * 800);
            this.y = (int) (Math.random() * 600);
            this.radius = (int) (Math.random() * 50);
            this.color = new Color((int) (Math.random() * 255),
                                                         (int) (Math.random() * 255), (int) (Math.random() * 255));
        }
    
        void move(Dimension size) {
            x += velX;
            y += velY;
    
            if (x < 0) {
                x = 0;
                velX *= -1;
            } else if (x + (radius * 2) > size.width) {
                x = size.width - (radius * 2);
                velX *= -1;
            }
            if (y < 0) {
                y = 0;
                velY *= -1;
            } else if (y + (radius * 2) > size.height) {
                y = size.height - (radius * 2);
                velY *= -1;
            }
        }
    
        public Sphere(int x, int y, int radius, Color color) {
            this.x = x;
            this.y = y;
            this.radius = radius;
            this.color = color;
        }
    
    }
    

    然后,在actionPerformed方法中,只需调用update

    @Override
    public void actionPerformed(ActionEvent evt) {
        for (int i = 0; i < 2; i++) {
            sphere[i].move(getSize());
        }
        repaint();
    }
    

    此外,您不需要两个Timer,只需要一个,每次勾选时都可以使用它来更新两个球体

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    class Draw extends JPanel implements ActionListener {
    
        Timer timer;
        Sphere sphere[] = new Sphere[2];
    
        public Draw() {
            Random rnd = new Random();
            for (int i = 0; i < 2; i++) {
                sphere[i] = new Sphere(rnd.nextInt(4) + 1, rnd.nextInt(4) + 1);
            }
            timer = new Timer(16, this);
            timer.start();
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (int i = 0; i < 2; i++) {
                drawing(g, sphere[i].color, sphere[i].radius);
            }
        }
    
        public void drawing(Graphics g, Color color, int radius) {
            for (int i = 0; i < 2; i++) {
                g.setColor(color);
                g.fillOval(sphere[i].x, sphere[i].y, radius * 2, radius * 2);
            }
        }
    
        @Override
        public void actionPerformed(ActionEvent evt) {
            for (int i = 0; i < 2; i++) {
                sphere[i].move(getSize());
            }
            repaint();
        }
    
        public static void main(String args[]) {
            JFrame jf = new JFrame("Renderer");
            jf.getContentPane().add(new Draw(), BorderLayout.CENTER);
            jf.setBounds(0, 0, 1024, 720);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.setVisible(true);
        }
    
        class Sphere {
    
            int velX;
            int velY;
    
            public int x;
            public int y;
            public int radius;
            Color color;
    
            public Sphere(int xDelta, int yDelta) {
                velX = xDelta;
                velY = yDelta;
                this.x = (int) (Math.random() * 800);
                this.y = (int) (Math.random() * 600);
                this.radius = (int) (Math.random() * 50);
                this.color = new Color((int) (Math.random() * 255),
                                                             (int) (Math.random() * 255), (int) (Math.random() * 255));
            }
    
            void move(Dimension size) {
                x += velX;
                y += velY;
    
                if (x < 0) {
                    x = 0;
                    velX *= -1;
                } else if (x + (radius * 2) > size.width) {
                    x = size.width - (radius * 2);
                    velX *= -1;
                }
                if (y < 0) {
                    y = 0;
                    velY *= -1;
                } else if (y + (radius * 2) > size.height) {
                    y = size.height - (radius * 2);
                    velY *= -1;
                }
            }
    
            public Sphere(int x, int y, int radius, Color color) {
                this.x = x;
                this.y = y;
                this.radius = radius;
                this.color = color;
            }
    
        }
    }
    

    此外,你不应该在“画画”关卡中启动计时器,它们应该提前启动

  3. # 3 楼答案

    velXvelY移动到Sphere类中。所以每个Sphere都会保持自己的速度。然后这些球体将独立移动

    不需要两个Timer,我已经把它清理干净了。 我还清理了paintComponent方法。在两个地方不需要循环

    以下是我尝试过的,似乎对我有用:


    public class Draw extends JPanel implements ActionListener {
        Sphere sphere[] = new Sphere[2];
    
        public Draw() {
            for (int i = 0; i < 2; i++) {
                sphere[i] = new Sphere();
            }
            Timer timer = new Timer(5, this);
            timer.start();
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            drawing(g);
        }
    
        public void drawing(Graphics g) {
            for (int i = 0; i < 2; i++) {
                g.setColor(sphere[i].color);
                g.fillOval(sphere[i].x, sphere[i].y, sphere[i].radius * 2, sphere[i].radius * 2);
            }
        }
    
        public void actionPerformed(ActionEvent evt) {
            for (int i = 0; i < 2; i++) {
                if (sphere[i].x < 0 || sphere[i].x > 970) {
                    sphere[i].velX = -sphere[i].velX;
                }
                if (sphere[i].y < 0 || sphere[i].y > 650) {
                    sphere[i].velY = -sphere[i].velY;
                }
                sphere[i].x += sphere[i].velX;
                sphere[i].y += sphere[i].velY;
                repaint();
            }
        }
    
        public static void main(String args[]) {
            JFrame jf = new JFrame("Renderer");
            jf.getContentPane().add(new Draw(), BorderLayout.CENTER);
            jf.setBounds(0, 0, 1024, 720);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.setVisible(true);
        }
    }
    
    class Sphere {
        public int x;
        public int y;
        public int radius;
        Color color;
        int velX;
        int velY;
    
        public Sphere() {
            this.x = (int) (Math.random() * 800);
            this.y = (int) (Math.random() * 600);
            this.radius = (int) (Math.random() * 50);
            this.color = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
            velX = new Random().nextInt()%5 + 2;
            velY = new Random().nextInt()%5 + 2;
        }
    
        public Sphere(int x, int y, int radius, Color color) {
            this.x = x;
            this.y = y;
            this.radius = radius;
            this.color = color;
        }
    }
    

    希望这有帮助