下記のソースコードで実行すると、4つの点が現れcounterが100の倍数になる度にそれぞれの点が上下左右のうち1方向に進む事を期待していたのですが、実際に実行してみると、4つの点が現れてcounterが100の倍数になる度に方向を変換するところまでは良かったのですが、全部の点が同じ方向に進んでしまいます。
それぞれの点が個別の方向に動くようにしたいのですが、どう修正すれば改善できるでしょうか?
java
1import java.awt.*; 2import javax.swing.*; 3import java.util.Random; 4 5public class Tracking { 6 public static void main(String[] args) { 7 JFrame frame = new JFrame(); 8 9 frame.setTitle("Simple Animation"); 10 frame.setSize(1000, 1000); 11 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 12 13 MyPanel panel = new MyPanel(); 14 frame.getContentPane().add(panel); 15 16 frame.setVisible(true); 17 } 18} 19 20class MainPanel extends JPanel implements Runnable { 21 public static int counter; 22 // Cfの数 23 private static int Fugitives = 4; 24 // Cfを格納する配列 25 private drawCf [] Cf; 26 27 public MainPanel() { 28 // Cfを格納する配列を作成 29 Cf = new drawCf[Fugitives]; 30 // Cfを作成 31 Cf[0] = new drawCf(10,10,20); 32 Cf[1] = new drawCf(50,50,20); 33 Cf[2] = new drawCf(100,100,20); 34 Cf[3] = new drawCf(200,200,20); 35 // 10ミリ秒ごとに画面を書き換える 36 Thread refresh = new Thread(this); 37 refresh.start(); 38 } 39 40 public void paintComponent(Graphics g) { 41 super.paintComponent(g); 42 43 // アニメーションの1コマごとの処理 44 for (int i = 0; i < Fugitives; i++) { 45 Cf[i].draw(g); 46 } 47 counter++; 48 49 } 50 // 10ミリ秒ごとに画面を書き換える 51 public void run() { 52 while(true) { 53 // 各ボールを移動させる 54 for (int i = 0; i < Fugitives; i++) { 55 Cf[i].move(); 56 } 57 repaint(); 58 try { 59 Thread.sleep(10); 60 } 61 catch(Exception e) { 62 } 63 } 64 } 65} 66 67class Draw{ 68 public static boolean check; 69 public static int x; 70 public static int y; 71 72 private static int direction; 73 private static int Random; 74 75 public void ball(int X,int Y) { 76 check = false; 77 //画面端か判定 78 if(X > 1000){X = 0; check = true;} 79 if(X < 0){X = 1000; check = true;} 80 if(Y > 1000){Y = 0; check = true;} 81 if(Y < 0){Y = 1000; check = true;} 82 x = X; 83 y = Y; 84 } 85 public static void random(int X,int Y,double C){ 86 double D1 = C / drawCf.rD; 87 int D2 = (int)D1; 88 if(D1 - D2 == 0){ 89 Random R = new Random(); 90 direction = R.nextInt(4); 91 } 92 switch(direction){ 93 case 0: X++; break; 94 case 1: Y++; break; 95 case 2: X--; break; 96 case 3: Y--; break; 97 } 98 x = X; 99 y = Y; 100 //System.out.println(direction); 101 } 102} 103 104class drawCf{ 105 //Drawオブジェクト 106 Draw draw = new Draw(); 107 108 private int x; 109 private int y; 110 private int S; 111 112 public static int rD = 100; 113 114 public static boolean random; 115 // コンストラクタ(新しいボールオブジェクトを作る) 116 public drawCf(int x, int y,int S) { 117 // ボールの属性を設定 118 this.x = x; 119 this.y = y; 120 this.S = S; 121 } 122 123 public void move() { 124 //反転させる 125 draw.ball(x,y); 126 if(Draw.check == true){ x = Draw.x; y = Draw.y;} 127 128 //RandomWalk 129 draw.random(x,y,(double) MyPanel.counter); 130 x = Draw.x; 131 y = Draw.y; 132 133 } 134 public void draw(Graphics g) { 135 g.setColor(Color.green); 136 g.fillOval(x, y, S, S); 137 } 138}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/08/10 15:27