用坐标列表更新G2D

时间:2015-09-17 06:17:26

标签: java graphics graphics2d

我想制作一个模拟蚂蚁的程序。我们说我有100只蚂蚁,每只蚂蚁都有坐标(2 integers)。我使用Java Graphics(Graphics2D)。

现在我想要一个读取类似列表的类:

List<List<Integer>> list = new ArrayList();

此列表例如:

  

[100200],[200100]

作为坐标。现在我想让课程更新时间并删除所有&#34;点&#34; (蚂蚁)之后用列表中的坐标绘制新的。

现在这不起作用。

public class FrameHandler {

Panel panel  = new Panel();

public FrameHandler() {
    //initialize frame
    frame.repaint();

    List<List<Integer>> test = new ArrayList();
    List<Integer> t1 = new ArrayList();
    t1.add(100);
    t1.add(200);
    test.add(gunther);

    frame.add(panel);

    panel.setList(test);

    //the thread sleeps for 5 seconds

    List<Integer> t2 = new ArrayList();
    t2.add(100);
    t2.add(100);
    test.add(gunther2);

    panel.removeAll();

    panel.setList(olaf);
}


public void setList(List<Integer> list) {
    panel.setList(list);
}

public void setSize(int width, int height) {
    frame.setSize(width, height);
}

private class Panel extends JPanel {

    List<List<Integer>> antList = new ArrayList();

    private void doDrawing(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        for(int i = 0; i < antList.size(); i++) {
            g2d.fillRect(antList.get(i).get(0), antList.get(i).get(1), 2, 2);
        }
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        doDrawing(g);
    }

    public void setList(List<List<Integer>> list) {
        antList = list;
    }
}

}

如果您对如何解决问题有另一个想法,可能使用API​​或其他方法来制作图形,我很乐意听到它。

1 个答案:

答案 0 :(得分:1)

  

现在我想让课程更新时间并删除所有&#34;点&#34; (蚂蚁)之后用列表中的坐标绘制新的。

这基本上是绘画在Swing中的工作方式,在每个绘制周期中,您需要从头开始重新绘制组件的整个状态。有关详细信息,请参阅Painting in AWT and Swing

问题就变成了,你如何更新List。虽然有很多方法可以做到这一点,但使用Swing Timer可能是最简单的(通常是最安全的)。有关详细信息,请参阅How to use Swing Timers

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 java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FrameHandler {

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

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

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

    public static class TestPane extends JPanel {

        protected static final Random RANDOM_DELTA = new Random();

        private List<List<Integer>> ants;

        public TestPane() {
            Random rnd = new Random();
            ants = new ArrayList<>(25);
            for (int index = 0; index < 10; index++) {
                List<Integer> ant = new ArrayList<>(2);
                // You should also have a look at the java.awt.Point class :P
                ant.add(rnd.nextInt(200 - 2)); //x
                ant.add(rnd.nextInt(200 - 2)); //y
                ants.add(ant);
            }

            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    for (List<Integer> ant : ants) {
                        int xDelta = randomDelta();
                        int yDelta = randomDelta();

                        int x = ant.get(0) + xDelta;
                        int y = ant.get(1) + yDelta;

                        if (x < 0) {
                            x = 0;
                        } else if (x + 2 > getWidth()) {
                            x = getWidth() - 2;
                        }
                        if (y < 0) {
                            y = 0;
                        } else if (y + 2 > getHeight()) {
                            y = getHeight() - 2;
                        }

                        ant.set(0, x);
                        ant.set(1, y);
                    }
                    repaint();

                }
            });
            timer.start();
        }

        protected int randomDelta() {
            int delta = 0;
            do {
                double rnd = Math.random();
                delta = rnd < 0.5d ? -1 : 1;
            } while (delta == 0);
            return delta;
        }

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

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (List<Integer> ant : ants) {
                g2d.fillRect(ant.get(0), ant.get(1), 2, 2);
            }
            g2d.dispose();
        }

    }

}

这个例子基本上为每个蚂蚁的x&amp; x生成随机增量(方向变化)。 y坐标。我怀疑你可能需要一些更复杂的东西