2017-05-08 2 views
0

J'essaie de tirer une balle de biais pour qu'elle rebondisse autour de l'écran jusqu'à ce qu'elle touche de nouveau le bas (elle s'arrête alors). Voici le code que j'ai jusqu'à présent, quelqu'un s'il vous plaît aidez-moi à faire fonctionner cela. Je suis nouveau à cela.Mouvement de la balle à l'angle saisi

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

Où est la question? S'il vous plaît voir [demander] – AxelH

Répondre

0

Vous pouvez utiliser sin graphe (0 - 180) pour l'axe y et l'incrément linéaire pour l'axe 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(); 
    } 
}