使用坐标列表更新 G2D
G2D updating with lists of coordinates
我想编写一个程序来模拟 ants.Let 假设我有 100 只蚂蚁,每只蚂蚁都有坐标 (2 integers
)。我使用 Java 图形 (Graphics2D).
现在我想要一个 class 来读取这样的列表:
List<List<Integer>> list = new ArrayList();
这个列表有例如:
[100,200],[200,100]
作为其中的坐标。现在我希望 class 一直更新并删除所有 "dots"(蚂蚁),然后用列表中的坐标绘制新的。
目前这行不通。
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 或其他制作图形的方法,我很乐意听到。
Now I want the class to update al the time and delete all "dots" (ants) and after that draw them new with the coordinates out of the List.
这基本上就是 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 和 y 坐标生成一个随机增量(方向变化)。我怀疑你可能需要一些更复杂的东西
我想编写一个程序来模拟 ants.Let 假设我有 100 只蚂蚁,每只蚂蚁都有坐标 (2 integers
)。我使用 Java 图形 (Graphics2D).
现在我想要一个 class 来读取这样的列表:
List<List<Integer>> list = new ArrayList();
这个列表有例如:
[100,200],[200,100]
作为其中的坐标。现在我希望 class 一直更新并删除所有 "dots"(蚂蚁),然后用列表中的坐标绘制新的。
目前这行不通。
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 或其他制作图形的方法,我很乐意听到。
Now I want the class to update al the time and delete all "dots" (ants) and after that draw them new with the coordinates out of the List.
这基本上就是 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 和 y 坐标生成一个随机增量(方向变化)。我怀疑你可能需要一些更复杂的东西