2017-05-08 36 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 축에는 죄 그래프 (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(); 
    } 
}