将方法动画绘制到另一个类

时间:2014-04-24 06:59:44

标签: java animation paintcomponent

有点粗略的java,但有人可以告诉我如何从对象列表中获取绘制方法,比如说有超过1个框,并将它放在actionPerform中,这样我就可以修改{的getter {1}}和XY他们并让他们搬家?

来自repaint()班级:

Box

public void draw(Graphics g) { g.setColor(Color.BLUE); g.fillRect((int) (getX() - (width/2)), (int) (getY() - (height/2)), getWidth(), getHeight()); } 方法:(这是另一个类)

actionPerform

1 个答案:

答案 0 :(得分:1)

基本理念是......

调用actionPerformed时......

  • 遍历对象列表并以一些有意义的方式更新它们
  • 致电重绘......

然后在您的组件paintComponent方法中,迭代列表并绘制它们

以下是一个非常基本的概念......

Boxes

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FlyingBoxes {

    public static void main(String[] args) {
        new FlyingBoxes();
    }

    public interface Drawable {

        public void update(JComponent comp);

        public void draw(Graphics g);

    }

    public class Box implements Drawable {

        private int x;
        private int y;
        private int width = 10;
        private int height = 10;

        private int xDelta;
        private int yDelta;

        public Box(int x, int y) {
            this.x = x;
            this.y = y;

            xDelta = random();
            yDelta = random();
        }

        @Override
        public void update(JComponent comp) {
            x += xDelta;
            y += yDelta;
            if (x < 0) {
                x = 0;
                xDelta *= -1;
            } else if (x + width > comp.getWidth()) {
                x = comp.getWidth() - width;
                xDelta *= -1;
            }
            if (y < 0) {
                y = 0;
                yDelta *= -1;
            } else if (y + height > comp.getHeight()) {
                y = comp.getHeight() - height;
                yDelta *= -1;
            }
        }

        @Override
        public void draw(Graphics g) {
            g.setColor(Color.BLUE);
            g.fillRect(x, y, width, height);
        }

        protected int random() {

            int value = 0;
            do {
                value = -2 + (int)(Math.random() * 4);
            } while (value == 0);

            return value;

        }

    }

    public FlyingBoxes() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private List<Drawable> drawables;

        public TestPane() {
            drawables = new ArrayList<>(25);
            for (int index = 0; index < 25; index++) {
                int x = (int) (Math.random() * 190);
                int y = (int) (Math.random() * 190);
                drawables.add(new Box(x, y));
            }

            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    for (Drawable d : drawables) {
                        d.update(TestPane.this);
                    }
                    repaint();
                }
            });
            timer.start();
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (Drawable d : drawables) {
                d.draw(g);
            }
        }
    }

}