horiuchi #6895(2008/07/31 23:59 GMT) [ Java ] Rating1/1=1.00
Java がまだ投稿されていなかったので、Swingで表示するものを作ってみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Timer; import java.util.TimerTask; import javax.swing.AbstractAction; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class RandomWalk extends JFrame { private static final int WIDTH = 640; private static final int HEIGHT = 480; private static final Dimension PLOT_SIZE = new Dimension(WIDTH, HEIGHT); private final JTextField countField_ = new JTextField(8); private final JButton startButton_ = new JButton(new AbstractAction("開始") { public void actionPerformed(ActionEvent e) { start(); } }); private final JButton stopButton_ = new JButton(new AbstractAction("停止") { public void actionPerformed(ActionEvent e) { stop(); } }); private final JButton endButton_ = new JButton(new AbstractAction("終了") { public void actionPerformed(ActionEvent e) { dispose(); } }); private final Random random_ = new Random(); private volatile boolean walking_ = false; private final List<Point> points_ = new ArrayList<Point>(); public RandomWalk() { super("2次元ランダムウォーク"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); configure(); Box main = Box.createVerticalBox(); main.add(createOperationArea()); main.add(createPlotArea()); this.add(main); Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { repaint(); }; }, 0, 100L); } private void configure() { countField_.setText("1000"); startButton_.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); stopButton_.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); } private JComponent createPlotArea() { plotArea_.setBackground(Color.WHITE); plotArea_.setPreferredSize(PLOT_SIZE); return plotArea_; } private JComponent createOperationArea() { JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); panel.add(new JLabel("N=")); panel.add(countField_); panel.add(Box.createHorizontalGlue()); panel.add(startButton_); panel.add(stopButton_); panel.add(endButton_); return panel; } public void start() { initialize(); walking_ = true; } public void stop() { walking_ = false; } private void initialize() { int count = 1; try { count = Integer.parseInt(countField_.getText()); } catch (NumberFormatException e) { } points_.clear(); points_.add(new Point(WIDTH / 2, HEIGHT / 2)); for (int index = 1; index < count; index++) { points_.add(new Point(random_.nextInt(WIDTH), random_.nextInt(HEIGHT))); } } private final JPanel plotArea_ = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (walking_) { for (Point p: points_) { nextStep(p); g.drawRect(p.x, p.y, 1, 1); } } } private void nextStep(Point p) { switch (random_.nextInt(4)) { case 0: p.x--; break; case 1: p.x++; break; case 2: p.y--; break; case 3: p.y++; break; } } }; public static void main(String[] args) { RandomWalk frame = new RandomWalk(); frame.pack(); frame.setVisible(true); } }
Rating1/1=1.00-0+
[ reply ]
horiuchi
#6895()
[
Java
]
Rating1/1=1.00
Java がまだ投稿されていなかったので、Swingで表示するものを作ってみました。
Rating1/1=1.00-0+
[ reply ]