2016-09-29 8 views
-1

私はJFrameでアプリを作る必要があります。ボールアニメーションのこの障害を理解できません

5つのボールがあり、フォームの周りをランダムに移動します。フォームを終了して境界線に当たってはいけません。ボールのバウンスを矩形から外す方法を理解することができないので、フォームの中央に四角形があり、それが大きな問題です。

私はすでに何かをやり始めましたが、ボールはフォーム上のいくつかのランダムな場所で跳ね返りました。

タスク:

  • 作成JFrame(完了)
  • がランダム(完了)位置
  • の周り及びスポーン移動5 Ballsは、フォームの中央にRectangleを作成します(完了)
  • ボールを矩形から跳ね返させます。 (完了)

最終的にそれを示しています。私は最近、このような何かをやっていた

import java.awt.Color; 
import java.awt.Graphics; 
import java.util.Random; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

@SuppressWarnings("serial") 
public class Main extends JPanel { 

Random rn = new Random(); 
int blueX = rn.nextInt(650) + 1; 
int blueY = rn.nextInt(650) + 1; 

int redX = rn.nextInt(650) + 1; 
int redY = rn.nextInt(650) + 1; 

int yellowX = rn.nextInt(650) + 1; 
int yellowY = rn.nextInt(650) + 1; 

int greenX = rn.nextInt(650) + 1; 
int greenY = rn.nextInt(650) + 1; 

int magentaX = rn.nextInt(650) + 1; 
int magentaY = rn.nextInt(650) + 1; 

int blueAngleX = rn.nextInt(10) + 1; 
int blueAngleY = rn.nextInt(20) + 1; 

int redAngleX = rn.nextInt(10) + 1; 
int redAngleY = rn.nextInt(50) + 1; 

int yellowAngleX = rn.nextInt(40) + 1; 
int yellowAngleY = rn.nextInt(50) + 1; 

int greenAngleX = rn.nextInt(30) + 1; 
int greenAngleY = rn.nextInt(20) + 1; 

int magentaAngleX = rn.nextInt(20) + 1; 
int magentaAngleY = rn.nextInt(50) + 1; 

int rectX = 325, rectY = 325, rectW = 100, rectH = 100; 

int speed = 5; 

private void move() { 

    rectContact(); 

    if (blueX + blueAngleX < 0) { 
     blueAngleX = speed; 
    } else if (blueX + blueAngleX > getWidth() - 30) { 
     blueAngleX = -speed; 
    } else if (blueY + blueAngleY < 0) { 
     blueAngleY = speed; 
    } else if (blueY + blueAngleY > getHeight() - 30) { 
     blueAngleY = -speed; 
    } 

    blueX = blueX + blueAngleX; 
    blueY = blueY + blueAngleY; 

    /////////////////////////////////////////////////////////////////////////////////////////// 

    if (redX + redAngleX < 0) { 
     redAngleX = speed; 
    } else if (redX + redAngleX > getWidth() - 30) { 
     redAngleX = -speed; 
    } else if (redY + redAngleY < 0) { 
     redAngleY = speed; 
    } else if (redY + redAngleY > getHeight() - 30) { 
     redAngleY = -speed; 
    } 

    redX = redX + redAngleX; 
    redY = redY + redAngleY; 

    /////////////////////////////////////////////////////////////////////////////////////////// 

    if (yellowX + yellowAngleX < 0) { 
     yellowAngleX = speed; 
    } else if (yellowX + yellowAngleX > getWidth() - 30) { 
     yellowAngleX = -speed; 
    } else if (yellowY + yellowAngleY < 0) { 
     yellowAngleY = speed; 
    } else if (yellowY + yellowAngleY > getHeight() - 30) { 
     yellowAngleY = -speed; 
    } 

    yellowX = yellowX + yellowAngleX; 
    yellowY = yellowY + yellowAngleY; 

    /////////////////////////////////////////////////////////////////////////////////////////// 

    if (greenX + greenAngleX < 0) { 
     greenAngleX = speed; 
    } else if (greenX + greenAngleX > getWidth() - 30) { 
     greenAngleX = -speed; 
    } else if (greenY + greenAngleY < 0) { 
     greenAngleY = speed; 
    } else if (greenY + greenAngleY > getHeight() - 30) { 
     greenAngleY = -speed; 
    } 

    greenX = greenX + greenAngleX; 
    greenY = greenY + greenAngleY; 

    /////////////////////////////////////////////////////////////////////////////////////////// 

    if (magentaX + magentaAngleX < 0) { 
     magentaAngleX = speed; 
    } else if (magentaX + magentaAngleX > getWidth() - 30) { 
     magentaAngleX = -speed; 
    } else if (magentaY + magentaAngleY < 0) { 
     magentaAngleY = speed; 
    } else if (magentaY + magentaAngleY > getHeight() - 30) { 
     magentaAngleY = -speed; 
    } 

    magentaX = magentaX + magentaAngleX; 
    magentaY = magentaY + magentaAngleY; 

} 

public void rectContact() { 

    if (blueY + 30 >= rectY && (blueX >= rectX - 25 && blueX <= rectX) 
      || (blueY >= rectY + 100 && (blueX >= rectX && blueX <= rectX + 100))) { 
     blueAngleX = -speed; 
    } 
    if (blueX + 10 >= rectX && (blueY >= rectY && blueY <= rectY - 25) 
      || (blueX >= rectX && (blueY >= rectY && blueY <= rectY))) { 
     blueAngleY = -speed; 
    } 

} 

@Override 
public void paint(Graphics g) { 
    super.paint(g); 
    g.setColor(Color.BLUE); 
    g.fillOval(blueX, blueY, 30, 30); 

    g.setColor(Color.RED); 
    g.fillOval(redX, redY, 30, 30); 

    g.setColor(Color.YELLOW); 
    g.fillOval(yellowX, yellowY, 30, 30); 

    g.setColor(Color.GREEN); 
    g.fillOval(greenX, greenY, 30, 30); 

    g.setColor(Color.MAGENTA); 
    g.fillOval(magentaX, magentaY, 30, 30); 

    g.setColor(Color.RED); 
    g.fillRect(rectX, rectY, rectW, rectH); 
} 

public static void main(String[] args) throws InterruptedException { 

    JFrame frame = new JFrame("Moving Ball!"); 
    Main main = new Main(); 
    frame.add(main); 
    frame.setBounds(300, 0, 750, 750); 
    frame.setVisible(true); 
    frame.setResizable(false); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    while (true) { 
     main.move(); 
     main.repaint(); 
     Thread.sleep(10); 
    } 
} 

} 
+1

1. 'ペイント」を上書きしないで、オーバーライドは' paintComponent' 2.アニメーション化したい場合は、[javax.swing.Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)を使用することをお勧めします。 – copeg

+0

1)より良いヘルプ早く、[MCVE]または[短く、自己完結型、正しい例](http://www.sscce.org/)を投稿してください。 * "5つのボールを作る" * MCVE/SSCCEにボールを1つだけ入れる。あなたがそれを使って作業することができれば、まったく同じように5で動作するはずです。 2)関連する例については、[複雑な形状による衝突検出](http://stackoverflow.com/a/14575043/418556)も参照してください。 –

+0

@copegだから、私はTimerに切り替えました。別の問題が発生しました...新しいコードを追加しました。多分あなたは私を助けたり、ヒントを与えることができます。 – UniQLostInTheCode

答えて

0

と私はthis出くわし:

は、ここに私のコードです。それはまったく同じではありませんが、私はトップの答えを見ることがあなたをかなり助けてくれると思います。

0

さて、私はjavax.swing.Timerを使用しました。 私はボールのランダムなスポーンエリアに関するいくつかの他の問題に遭遇しました。 私は、5で割るボールの座標X、Yを使用すると気づきました。すべてが問題ありませんが、ボールが再び矩形を通過し始めます。 私は理由を理解できません。 私の考えは、x1、y1、x2、y2などの乱数生成器を作るかもしれません。 50から250の範囲の乱数を選択しますが、その数は5で割り切れる必要があります。乱数が5,10,15,20,25のようなものであることを意味します。 forループと配列を使って値を格納し、プログラムが配列リストからx1のランダムな値を選ぶようにすることができます。

しかし、それは完全に間違って聞こえると私は、トピックをちょうどオフにさまよっていると思いますすべての...

だからここに私の新しいコードです:

import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.util.Random; 
import javax.swing.JFrame; 
import javax.swing.Timer; 

@SuppressWarnings("serial") 
public class Uzd2 extends JFrame implements ActionListener, KeyListener { 
Graphics2D g2d; 
Timer timer = new Timer(5, this); 

int width1 = 30, width2 = 30, width3 = 30, width4 = 30, width5 = 30; 
int height1 = 30, height2 = 30, height3 = 30, height4 = 30, height5 = 30; 

Random rn = new Random(); 

int x1 = 20 + rn.nextInt(50) + 1; 

int x2 = 20 + rn.nextInt(50) + 1; 

int x3 = 20 + rn.nextInt(50) + 1; 

int x4 = 20 + rn.nextInt(50) + 1; 

int x5 = 20 + rn.nextInt(50) + 1; 

int y1 = 20 + rn.nextInt(50) + 1; 

int y2 = 20 + rn.nextInt(50) + 1; 

int y3 = 20 + rn.nextInt(50) + 1; 

int y4 = 20 + rn.nextInt(50) + 1; 

int y5 = 20 + rn.nextInt(50) + 1; 



int x1Diff = 5, y1Diff = -5; 
int x2Diff = 5, y2Diff = -5; 
int x3Diff = 5, y3Diff = -5; 
int x4Diff = 5, y4Diff = -5; 
int x5Diff = 5, y5Diff = -5; 
int y1Rect = 325, x1Rect = 325; 
int y2Rect = 100, x2Rect = 150; 

public Uzd2() { 
    timer.start(); 

    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setBounds(10, 10, 750, 750); 
    setResizable(false); 
    setTitle("Animācija"); 
    addKeyListener(this); 

    System.out.println("X1 = " + x1 + " X2 = " + x2 + " X3 = " + x3 + " X4 = " + x4 + " X5 = " + x5); 
    System.out.println("Y1 = " + y1 + " Y2 = " + y2 + " Y3 = " + y3 + " Y4 = " + y4 + " Y5 = " + y5); 

} 

public void paint(java.awt.Graphics g) { 

    super.paint(g); 
    g2d = (Graphics2D) g; 
    g2d.setStroke(new BasicStroke(5)); 
    g2d.setColor(Color.BLUE); 

    animacija1(x1, y1, width1); 
    animacija2(x2, y2, width2); 
    animacija3(x3, y3, width3); 
    animacija4(x4, y4, width4); 
    animacija5(x5, y5, width5); 

    g2d.setColor(Color.RED); 
    g2d.fillRect(325, 325, 100, 100); 
} 

public void animacija1(int x1, int y1, int w1) { 
    g2d.setColor(Color.BLUE); 
    g2d.fillOval(x1, y1, width1, height1); 
} 

public void animacija2(int x2, int y2, int w2) { 
    g2d.setColor(Color.GREEN); 
    g2d.fillOval(x2, y2, width2, height2); 
} 

public void animacija3(int x3, int y3, int w3) { 
    g2d.setColor(Color.MAGENTA); 
    g2d.fillOval(x3, y3, width3, height3); 
} 

public void animacija4(int x4, int y4, int w4) { 
    g2d.setColor(Color.CYAN); 
    g2d.fillOval(x4, y4, width4, height4); 
} 

public void animacija5(int x5, int y5, int w5) { 
    g2d.setColor(Color.YELLOW); 
    g2d.fillOval(x5, y5, width5, height5); 
} 

public void actionPerformed(ActionEvent e) { 
    aprekini1(); 
    aprekini2(); 
    aprekini3(); 
    aprekini4(); 
    aprekini5(); 

    repaint(); 

} 

public void aprekini1() { 
    //Top, bottom. 
    if ((y1 <= 20 || y1 + 30 >= 760) || (y1 + 25 == y1Rect && (x1 >= x1Rect && x1 <= x1Rect + 100)) 
      || (y1 == y1Rect + 100 && (x1 >= x1Rect && x1 <= x1Rect + 100))) { 
     y1Diff = -y1Diff; 

    } 
    //Left, right. 
    if ((x1 <= 0 || x1 + 30 >= 750) || (x1 + 25 == x1Rect && (y1 >= x1Rect && y1 <= y1Rect + 100)) 
      || (x1 == x1Rect + 100 && (y1 >= x1Rect && y1 <= y1Rect + 100))) { 
     x1Diff = -x1Diff; 

    } 

    x1 += x1Diff; 
    y1 += y1Diff; 

} 

public void aprekini2() { 


    // Top, bottom. 
    if ((y2 <= 20 || y2 + 30 >= 760) || (y2 + 25 == y1Rect && (x2 >= x1Rect && x2 <= x1Rect + 100)) 
      || (y2 == y1Rect + 100 && (x2 >= x1Rect && x2 <= x1Rect + 100))) { 
     y2Diff = -y2Diff; 

    } 
    // Left, right. 
    if ((x2 <= 0 || x2 + 30 >= 750) || (x2 + 25 == x1Rect && (y2 >= x1Rect && y2 <= y1Rect + 100)) 
      || (x2 == x1Rect + 100 && (y2 >= x1Rect && y2 <= y1Rect + 100))) { 
     x2Diff = -x2Diff; 

    } 

    x2 += x2Diff; 
    y2 += y2Diff; 

} 

public void aprekini3() { 

    // Top, bottom. 
    if ((y3 <= 20 || y3 + 30 >= 760) || (y3 + 25 == y1Rect && (x3 >= x1Rect && x3 <= x1Rect + 100)) 
      || (y3 == y1Rect + 100 && (x3 >= x1Rect && x3 <= x1Rect + 100))) { 
     y3Diff = -y3Diff; 

    } 
    // Left, right. 
    if ((x3 <= 0 || x3 + 30 >= 750) || (x3 + 25 == x1Rect && (y3 >= x1Rect && y3 <= y1Rect + 100)) 
      || (x3 == x1Rect + 100 && (y3 >= x1Rect && y3 <= y1Rect + 100))) { 
     x3Diff = -x3Diff; 

    } 

    x3 += x3Diff; 
    y3 += y3Diff; 

} 

public void aprekini4() { 


    // Top, bottom. 
    if ((y4 <= 20 || y4 + 30 >= 760) || (y4 + 25 == y1Rect && (x4 >= x1Rect && x4 <= x1Rect + 100)) 
      || (y4 == y1Rect + 100 && (x4 >= x1Rect && x4 <= x1Rect + 100))) { 
     y4Diff = -y4Diff; 

    } 
    // Left, right. 
    if ((x4 <= 0 || x4 + 30 >= 750) || (x4 + 25 == x1Rect && (y4 >= x1Rect && y4 <= y1Rect + 100)) 
      || (x4 == x1Rect + 100 && (y4 >= x1Rect && y4 <= y1Rect + 100))) { 
     x4Diff = -x4Diff; 

    } 

    x4 += x4Diff; 
    y4 += y4Diff; 

} 

public void aprekini5() { 

    // Top, bottom. 
    if ((y5 <= 20 || y5 + 30 >= 760) || (y5 + 25 == y1Rect && (x5 >= x1Rect && x5 <= x1Rect + 100)) 
      || (y5 == y1Rect + 100 && (x5 >= x1Rect && x5 <= x1Rect + 100))) { 
     y5Diff = -y5Diff; 

    } 
    // Left, right. 
    if ((x5 <= 0 || x5 + 30 >= 750) || (x5 + 25 == x1Rect && (y5 >= x1Rect && y5 <= y1Rect + 100)) 
      || (x5 == x1Rect + 100 && (y5 >= x1Rect && y5 <= y1Rect + 100)))  { 
     x5Diff = -x5Diff; 

    } 

    x5 += x5Diff; 
    y5 += y5Diff; 

} 



@Override 
public void keyTyped(KeyEvent e) { 
    if (e.getKeyCode() == KeyEvent.VK_W) { 
     y1Rect -= 5; 
    } 
} 

@Override 
public void keyPressed(KeyEvent e) { 

} 

@Override 
public void keyReleased(KeyEvent e) { 

} 

public static void main(String[] args) { 

    Uzd2 frame = new Uzd2(); 
    frame.setVisible(true); 

} 

} 
+0

バンプすることは可能ですか? – UniQLostInTheCode