ボールを上に投げて落下してくるというプログラムを作りました
コード public class Sample extends Applet implements Runnable{ Thread th; double G = 9.8*2; //重力加速度 int time; double mul = 0.1; double times; double V0y = 120.0; //初速度 double Vy; int X = 200; //x座標 int Y = 800; //y座標 public void init(){ setSize(800, 800); setBackground(Color.black); } public void start(){ th = new Thread(this); th.start(); } public void run(){ for(time = 0; time < 1000; time++){ times = time*mul; repaint(); try{ Thread.sleep(50); } catch(InterruptedException e){ } } } public void paint(Graphics g){ Vy = V0y - G*times; //V0 - at Y = (int)(800 - (V0y*times - G*times*times/2));//V0t - at*t/2 g.setColor(Color.white); g.fillRect(X, Y, 2, 2); } }
これをandroidで作ったのが下のもの
コード public class MainActivity extends Activity implements Runnable{ private Handler h = new Handler(); private FrameLayout frameLayout; SampleView sv; double G = 9.8*2; int time; double mul = 0.1; double times; double V0y = 200.0; double Vy; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); frameLayout = new FrameLayout(this); frameLayout = (FrameLayout)findViewById(R.id.FrameLayout1); frameLayout.setBackgroundColor(Color.BLACK); sv = new SampleView(this); frameLayout.addView(sv); h.postDelayed(this, 500); } public void run(){ for(time = 0; time < 1000; time++){ times = time*mul; Vy = V0y - G*times; sv.y = (int)(1400 - (V0y*times - G*times*times/2)); sv.invalidate(); try{ Thread.sleep(50); } catch(InterruptedException e){ } } } } public class SampleView extends View{ Paint paint = new Paint(); int x = 200; //x軸 int y = 1400; //y軸 public SampleView(Context context) { super(context); } protected void onDraw(Canvas canvas){ paint.setColor(Color.WHITE); paint.setStrokeWidth(10); canvas.drawPoint(x, y, paint); } }
このようなandroidのプログラムを作ったのですが、実行するとx=200,y=1400の位置から動きませんでした。
はじめのjavaのプログラムはきちんと動くのですが、どこがだめなのでしょうか?
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。