2017-05-08 14 views
0

私はボールを斜めに撃つようにして、ボールが再び底を打つ(停止する)まで画面を跳ね返そうとしています。ここに私がこれまで持っていたコードがあります。誰かがこれを働かせるように助けてください。私はこれに新しいです。打球時のボールの移動

import java.awt.*; 
     import java.util.Formatter; 

     import javax.swing.*; 
     import java.util.Scanner; 

     /** 
     * This class first draws a ball at the middle of the bottom 
     * then it shoots the ball at a certain angle 
     */ 

     public class Ball extends JPanel { 
    private double xPosition ; 
    private static double yPosition ; 
    private Color colorOfBall; 
    private double radius; 
    private double ballSpeedX = 3.0; // Ball's speed for x and y 
    private double ballSpeedY = 3.0; 

    private static final double BALL_SPEED = .01; 

    public Ball() { 
     radius = 10; 
     colorOfBall = Color.WHITE; 

    } 


    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     double width = getWidth();  
     double height = getHeight(); 

     // Declare and initialize local int variables xCenter, yCenter 
     // that represent the center of the rainbow rings: 
     xPosition = (double)((1.0/2)*width); 
     yPosition = (double)(height-radius); 

     //Draw the frame 
     g.setColor(Color.BLACK); 
     g.fillRect(0, 0, 640, 480); 

     //Draw the Ball 
     g.setColor(colorOfBall); 
     g.fillOval((int) (xPosition-radius), (int)(yPosition-radius), (int)(2 * radius), (int)(2 * radius)); 
     System.out.println(xPosition + "," + yPosition); 
    } 

    public void shoot(int angle) 
    { 
     // while(yPosition < getHeight()) 
     while(true) 
     { 
      yPosition-=BALL_SPEED * Math.sin(angle); 
      xPosition+=BALL_SPEED * Math.cos(angle); 
      /*if(xPosition - radius < 0){ 
       ballSpeedX = -ballSpeedX; 
       xPosition = radius; 
      } 
      else if (xPosition + radius > getWidth()){ 
       ballSpeedX = -ballSpeedX; 
       xPosition = getWidth()- radius; 
      } 

      if(yPosition - radius < 0){ 
       ballSpeedY = -ballSpeedY; 
       yPosition = radius; 
      } 
      else if (xPosition + radius > getWidth()){ 
       ballSpeedY = -ballSpeedY; 
       yPosition = getHeight()- radius; 
      }*/ 



      repaint(); 
     } 

     } 


    public static void main(String [] args) { 
     JFrame frame = new JFrame("Ballzzz");//Create frame with header Ballzzz 
     frame.setBounds(0, 0, 400, 500); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Container c = frame.getContentPane(); 
     Ball ball1 = new Ball(); 
     c.add(ball1); 
     frame.setVisible(true); 
     frame.setResizable(false);//you can't resize my frame. hahahhhaha 

     /*Scanner kboard = new Scanner(System.in); 
     System.out.print("Enter the angle you want to shoot the ball: "); 
     int angle = kboard.nextInt();*/ 

     ball1.shoot(45); 


    } 
} 
+0

どこに問題がありますか? [ask]を見てください – AxelH

答えて

0

y軸にsinグラフ(0〜180)、x軸に線形増分を使用できます。

enter image description here

public void shoot(double angle,double force) 
{ 
    double rad = 0; 
    for(double i=angle;i<=180;i++) 
    { 
    rad = angle*Math.PI/180.0; 
    yPosition-=force* Math.sin(rad); 
    xPosition++; 
    repaint(); 
    } 
} 
関連する問題