###ヘディングのテキスト###前提・実現したいこと
アクティブエリアを考慮してBallクラスのmoveメソッドでの跳ね返り処理のどこが不完全であるかを見つけ,ボールの跳ね返り処理正しく行われるように修正したいのですがどうしたらいいかわかりません
###発生している問題・エラーメッセージ
ボールが動かない
###該当のソースコード
// BlockMain.java import javax.swing.JFrame; @SuppressWarnings("serial") public class BlockMain extends JFrame{ public static void main(String[] args) { BlockMain w = new BlockMain(); w.setTitle("ブロック崩し?"); w.setSize(300, 400); //Window のサイズをセット BlockCanvas bc = new BlockCanvas(); w.add(bc); w.setVisible(true); //表示する } } // BlockCanvas.java import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; @SuppressWarnings("serial") public class BlockCanvas extends Canvas implements Runnable { Ball ball = new Ball(); Thread thread; // スレッドを使うためのインスタンスを宣言 Dimension dim; // 描画領域の大きさ BlockCanvas() { dim = getSize(); // 描画領域の大きさを調べる thread = new Thread(this); // スレッドを生成 thread.start(); // スレッドを開始 (run()が呼ばれる) } // ダブルバッファリング public void paint(Graphics g) { Dimension dim = getSize(); // 描画領域の大きさを調べる Image m_image = createImage(dim.width, dim.height);// 裏画面の画像イメージ Graphics m_g = m_image.getGraphics(); // 裏画面に付随するグラフィックスオブジェクト m_g.setColor(Color.white); m_g.fillRect(0, 0, dim.width, dim.height); ball.draw(m_g, dim); g.drawImage(m_image, 0, 0, this); // 裏画面を表画面にコピー } @Override public void run() { while (true) { // このアプリの実行中は無限ループ try { Thread.sleep(50); } catch (InterruptedException e) {} // 例外は受け流す ball.move(dim); repaint(); // 再描画要求 } } public class Ball { int x = 50, y = 100; // ボール中心の座標 int vx = 4, vy = 5; // ボールの速度ベクトル static final double RADIUS = 0.02; // ボールの半径はウインドウの2% int radius; // ボールの半径 public void draw(Graphics g, Dimension dim) { radius = (int)(RADIUS * dim.width); g.setColor(Color.black); g.fillOval(x - radius, y - radius, radius + radius, radius + radius); } public void move(Dimension dimension) { x += vx; if (x < 0) { x = 0; vx = -vx; } else if (x > dimension.width) { x = dimension.width; vx = -vx; } y += vy; if (y < 0) { y = 0; vy = -vy; } else if (y > dimension.height) { y = dimension.height; vy = -vy; } } } }
###試したこと
課題に対してアプローチしたことを記載してください
###補足情報(言語/FW/ツール等のバージョンなど)
Eclipse Luna
回答2件
あなたの回答
tips
プレビュー