public BallsFrame() throws HeadlessException {
   super("Funny balls");
   setLayout(new BorderLayout());
   executorService = Executors.newScheduledThreadPool(1);
   ballsPanel =
       new JPanel() {
         @Override
         protected void paintComponent(Graphics g) {
           super.paintComponent(g);
           if (blueBallVisible) {
             g.setColor(Color.BLUE);
             g.fillOval(blueBall.x, getHeight() / 2, BALL_DIAGONAL, BALL_DIAGONAL);
           }
           g.setColor(Color.RED);
           g.fillOval(redBall.x, getHeight() / 2, BALL_DIAGONAL, BALL_DIAGONAL);
         }
       };
   add(ballsPanel, BorderLayout.CENTER);
   blueBallSwitch = new JCheckBox("Collision", true);
   blueBallSwitch.addItemListener(
       e -> {
         if (blueBallVisible = blueBallSwitch.isSelected()) {
           int x;
           do x = random.nextInt(getWidth() - BALL_DIAGONAL);
           while (redBall.x <= x && x <= redBall.rightX()
               || redBall.x <= x + BALL_DIAGONAL && x + BALL_DIAGONAL <= redBall.rightX());
           blueBall.x = x;
         }
       });
   add(blueBallSwitch, BorderLayout.SOUTH);
   blueBallVisible = true;
   moveToRight = true;
   setSize(500, 500);
 }
Exemplo n.º 2
0
  void the_derive(double time, double y[], double dy[]) {
    int i;
    BallVector balls = decode_balls(y); // new BallVector();//Ball[num_balls];
    BallVector dballs = new BallVector(); // new Ball[num_balls];

    for (i = 0; i < num_balls; i++) {
      Ball p = balls.get2(i);
      Ball d = new Ball();
      d.pos = p.speed;
      d.speed = wall_power(p);
      d.speed.y -= 1; // gravity
      dballs.add(d);
    }

    for (i = 0; i < num_balls; i++)
      for (int j = i + 1; j < num_balls; j++) {
        Ball p1 = balls.get2(i);
        Ball p2 = balls.get2(j);
        if (far_away_fast_calc(p1.pos, p2.pos, radius * 2)) continue;
        double dist = p1.pos.calc_dist(p2.pos);
        // if (dist>radius*2)
        //	continue;

        Vec collide_power = calc_collide_power(p1, p2, dist);
        dballs.get2(i).speed.add_to(collide_power);
        dballs.get2(j).speed.sub_to(collide_power);
      }
    for (i = 0; i < springs.size(); i++) {
      Spring s = springs.get2(i);
      Vec collide_power = calc_spring_power(balls.get2(s.start), balls.get2(s.end));
      dballs.get2(s.start).speed.add_to(collide_power);
      dballs.get2(s.end).speed.sub_to(collide_power);
    }
    encode_balls(dballs, dy);
  };
Exemplo n.º 3
0
 public void mouseMoved(MouseEvent e) { // called during motion when no buttons are down
   //  if (e.getY ==
   if ((e.getX() < appletsize_x - pad.width() / 2) && (e.getX() > (pad.width() / 2))) {
     pad.changeX(e.getX() - (pad.width() / 2));
     ball.moveWithPaddle(pad.xPos + (pad.width() / 2) - (ball.radius()));
   }
   e.consume();
   repaint();
 }
  public void draw() {

    background(255);

    for (int i = 0; i < balls.size(); i++) {
      Ball ball = (Ball) balls.get(i);
      ball.calc();
      ball.display();
    }
  }
Exemplo n.º 5
0
  /** Update all of the Ball's and draw them */
  public void update() {

    if (balls.size() != 0) {

      for (int i = 0; i < balls.size(); i++) {
        Ball b = (Ball) balls.get(i);
        b.update();
        b.attract = kelly;
        b.drawBall();
      }
    }
  }
Exemplo n.º 6
0
 public void paint(Graphics g) {
   if (mainMenu) {
     drawTitleScreen(g);
   } else if (gamePlaying) {
     drawBricks(g);
     g.setColor(Color.green);
     g.fillRect(pad.x(), 590, pad.width(), 10);
     g.fillOval(
         ball.x_Pos() - ball.radius(),
         ball.y_Pos() - ball.radius(),
         ball.radius() * 2,
         ball.radius() * 2);
   }
 } // paint method
 private void startAnimation() {
   redBall = new Ball(1);
   blueBall = new Ball(getWidth() / 2);
   executorService.scheduleAtFixedRate(
       () -> {
         if (redBall.rightX() >= getWidth()
             || redBall.x <= 0
             || blueBallVisible && redBall.intersectsWith(blueBall)) moveToRight = !moveToRight;
         redBall.x += moveToRight ? 1 : -1;
         ballsPanel.repaint();
       },
       1,
       5,
       TimeUnit.MILLISECONDS);
 }
Exemplo n.º 8
0
  public void mouseDragged(MouseEvent e) { // called during motion with buttons down

    ball.LaunchBall();

    e.consume();
    repaint();
  }
Exemplo n.º 9
0
  /** Add a ball to the ArrayList at current mouse position */
  public void addBall() {

    // Store current mouse position
    x1 = mouseX;
    y1 = mouseY;
    // Get a random offset for the ball
    int xDiff = (int) random(-10, 10);
    int yDiff = (int) random(-10, 10);
    // Add the mouse's (x,y) and the random offset
    int x = mouseX + xDiff;
    int y = mouseY + yDiff;
    // Create new ball
    Ball b = new Ball(x, y, ballSize);
    b.STEM = stems;
    b.attract = kelly;
    balls.add(b);
  }
Exemplo n.º 10
0
  /** If a key is pressed perform the respective actions */
  public void keyPressed() {

    // Add 'stems' to the balls
    if (keyCode == SHIFT) {
      stems = !stems;
      for (int i = 0; i < balls.size(); i++) {
        Ball b = (Ball) balls.get(i);
        b.STEM = stems;
      }
    }
    // toggle repaint background
    else if (key == 'b') REPAINT = !REPAINT;
    // Empty the ArrayList of Balls
    else if (key == 'x') balls.clear();
    // Add a ball
    else if (key == 'f') addBall();
  }
Exemplo n.º 11
0
 @Override
 public void paint(Graphics g) {
   super.paint(g);
   for (Ball b : ballsList) {
     g.setColor(b.getColor());
     g.fillOval(b.getX(), b.getY(), b.getSize(), b.getSize());
     b.move(getWidth(), getHeight());
   }
 }
Exemplo n.º 12
0
    @Override
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;

      // paint ball, paddle, and brick configuration
      g2.drawImage(background.getImage(), 0, 0, null);
      bconfig.paint(g2);
      paddle.paint(g2);
      ball.paint(g2);
      g2.setColor(Color.WHITE);
      g2.setFont(new Font("Serif", Font.PLAIN, 20));
      g2.drawString("Score: " + score, 15, 20);
      if (ball.getY() > 500) {
        g2.setColor(Color.WHITE);
        g2.setFont(new Font("Serif", Font.PLAIN, 30));
        g2.drawString("Game Over!", 200, 300);
        g2.drawString("Your final score was " + score, 150, 330);
        timer.stop();
      }
    }
Exemplo n.º 13
0
  /**
   * Moves the paddle left by the movement offset field within the picture window's bounds avoiding
   * the given ball
   *
   * @param pict is the Picture object with the boundary to keep the paddle within
   * @param ball is a ball that causes the paddle to move back if it's in the way
   */
  public void moveLeft(JFrame pict, Ball ball) {
    this.x -= this.offset; // Move x left by the offset

    // Test to see if the paddle moved into the ball
    if (ball.distance(this.x, this.y) < ball.getRadius()
        || ball.distance(this.x, this.y + this.height / 2) < ball.getRadius()
        || ball.distance(this.x, this.y + this.height) < ball.getRadius())
      this.x += this.offset; // If so, move it out of the ball

    // Test to see if the boundary was in the way to the left
    if (this.x < 0) this.x = 0; // If so, set left of paddle to zero
  }
Exemplo n.º 14
0
  /**
   * Moves the paddle up by the movement offset field within the picture window's bounds avoiding
   * the given ball
   *
   * @param pict is the Picture object with the boundary to keep the paddle within
   * @param ball is a ball that causes the paddle to move back if it's in the way
   */
  public void moveUp(JFrame pict, Ball ball) {
    this.y -= this.offset; // Move y up by the offset

    // Test to see if the paddle moved into the ball
    if (ball.distance(this.x, this.y) < ball.getRadius()
        || ball.distance(this.x + this.width / 2, this.y) < ball.getRadius()
        || ball.distance(this.x + this.width, this.y) < ball.getRadius())
      this.y += this.offset; // If so, move it out of the ball

    // Test to see if the boundary was in the way above
    if (this.y < 0) this.y = 0; // If so, set top of paddle to zero
  }
Exemplo n.º 15
0
  /**
   * Moves the paddle right by the movement offset field within the picture window's bounds avoiding
   * the given ball
   *
   * @param pict is the Picture object with the boundary to keep the paddle within
   * @param ball is a ball that causes the paddle to move back if it's in the way
   */
  public void moveRight(JFrame pict, Ball ball) {
    this.x += this.offset; // Move x right by the offset

    // Test to see if the paddle moved into the ball
    if (ball.distance(this.x + this.width, this.y) < ball.getRadius()
        || ball.distance(this.x + this.width, this.y + this.height / 2) < ball.getRadius()
        || ball.distance(this.x + this.width, this.y + this.height) < ball.getRadius())
      this.x -= this.offset; // If so, move it out of the ball

    // Test to see if the boundary was in the way on the right
    if (this.x + this.width > pict.getWidth())
      this.x = pict.getWidth() - this.width; // If so, set right to right edge
  }
Exemplo n.º 16
0
  /**
   * Moves the paddle down by the movement offset field within the picture window's bounds avoiding
   * the given ball
   *
   * @param pict is the Picture object with the boundary to keep the paddle within
   * @param ball is a ball that causes the paddle to move back if it's in the way
   */
  public void moveDown(JFrame pict, Ball ball) {
    this.y += this.offset; // Move y down by the offset

    // Test to see if the paddle moved into the ball
    if (ball.distance(this.x, this.y + this.height) < ball.getRadius()
        || ball.distance(this.x + this.width / 2, this.y + this.height) < ball.getRadius()
        || ball.distance(this.x + this.width, this.y + this.height) < ball.getRadius())
      this.y -= this.offset; // If so, move it out of the ball

    // Test to see if the boundary was in the way below
    if (this.y + this.height > pict.getHeight())
      this.y = pict.getHeight() - this.height; // If so, set bottom to picture bottom
  }
Exemplo n.º 17
0
  public void run() {
    while (true) {
      if (t < 5) {
        t++;
      } else {
        t = 0;
      }
      if (mainMenu) {

        // Ball is bounced if its x - position reaches the right border of the applet
        if (posy_cursor < posy_new) {

          // Change direction of ball movement
          posy_cursor += 50;

        }
        // Ball is bounced if its x - position reaches the left border of the applet
        else if (posy_cursor > posy_highscores) {

          // Change direction of ball movement
          posy_cursor -= 50;
        }
      } else if (gamePlaying) {
        ball.move();
        if (ball.y_Pos() < 0) {
          ball.reflectVertically();

        } else if ((ball.x_Pos() < 0) || (ball.x_Pos() > appletsize_x)) {
          ball.reflectHorizontally();
        }

        if (ball.y_Pos() > 590) {

          if ((ball.x_Pos() > pad.x() - ball.radius())
              && ((ball.x_Pos() < pad.x() + (pad.width() / 2)))) {
            ball.reflectFromPaddle(LEFT);
          } else if ((ball.x_Pos() > pad.x() + (pad.width() / 2))
              && ((ball.x_Pos() < pad.x() + pad.width + ball.radius()))) {
            ball.reflectFromPaddle(RIGHT);
          } else {
            ball.startBall();
          }
        }

        for (int x = 0; x < numBricksX; x++) {
          for (int y = 0; y < numBricksY; y++) {
            if (brickWall[x][y].notBroken()) {
              if (brickWall[x][y].ballContactVertical(ball.x_Pos(), ball.y_Pos())) {
                ball.reflectVertically();
                brickWall[x][y].reduceLife();
                brickWall[x][y].startDropping();
              } else if (brickWall[x][y].ballContactHorizontal(ball.x_Pos(), ball.y_Pos())) {
                ball.reflectHorizontally();
                brickWall[x][y].reduceLife();
                brickWall[x][y].startDropping();
              }
            }
          }
        }
      }
      if (t == 5) {
        for (int x = 0; x < numBricksX; x++) {
          for (int y = 0; y < numBricksY; y++) {
            brickWall[x][y].dropPowerUp();
            if (brickWall[x][y].paddleContact(pad.xPos, 590)) {
              pad.widthIncrease();
            }
          }
        }
      }

      try {
        // Stop thread for 20 milliseconds
        Thread.sleep(2);
      } catch (InterruptedException ex) {
        // do nothing
      }

      repaint();
    }
  }
Exemplo n.º 18
0
 void new_ball(Vec vec) {
   Ball b = new Ball();
   b.pos = vec;
   balls.add(b);
 }
Exemplo n.º 19
0
 public void start() {
   ball = new Ball(50, 50);
   ball.changeColor(colorList[0]);
 }
Exemplo n.º 20
0
  // Constructor//
  public Game_Screen() {
    JFrame game_frame = new JFrame("Legend of Pong"); // creates the jframe
    JLabel Background =
        new JLabel(
            new ImageIcon(
                "C:\\Users\\Daniel\\workspace\\Pong_Game\\src\\resources\\Pics\\gameScreen.jpg")); // gets the BG image
    game_frame.setContentPane(Background); // places background image

    // setting the header section//

    // Score//
    long score = getScore(); // int to hold score value
    JLabel scoreL = new JLabel(); // label to display score
    scoreL.setText("Score: " + String.valueOf(score)); // connects label and int
    scoreL.setBounds(25, 15, 200, 35); // sets location of score
    scoreL.setFont(new Font("Courier New", Font.BOLD, 20)); // sets font and size of font
    scoreL.setForeground(Color.GREEN); // sets color of Score
    game_frame.add(scoreL); // adds score

    // Timer//
    int time = getTime();
    JLabel timeL = new JLabel(); // label to hold time
    timeL.setText("Time(sec): " + String.valueOf(time)); // connects label and int
    timeL.setBounds(350, 15, 200, 35); // set location of time
    timeL.setFont(new Font("Courier New", Font.BOLD, 20)); // sets font and size of font
    timeL.setForeground(Color.GREEN); // sets color
    game_frame.add(timeL); // adds time

    // Life//
    int life = getLife(); // int to hold value of lives left
    JLabel lifeL = new JLabel(); // label to hold life
    lifeL.setText("Lives: " + String.valueOf(life)); // connects label and int
    lifeL.setBounds(700, 15, 200, 35); // sets location of life
    lifeL.setFont(new Font("Courier New", Font.BOLD, 20)); // sets font and size of font
    lifeL.setForeground(Color.GREEN); // sets color of life
    game_frame.add(lifeL); // adds life

    // Paddle//
    JPanel paddle = new JPanel(); // new panel for paddle object and image
    Paddle object = new Paddle(); // creates new paddle object
    JLabel link =
        new JLabel(
            new ImageIcon(
                "C:\\Users\\Daniel\\workspace\\Pong_Game\\src\\resources\\Pics\\Link.gif")); // new
                                                                                             // paddle image(also link)
    paddle.add(object); // adds the actual object
    paddle.add(link); // adds image to the paddle Panel
    paddle.setBounds(
        object.getX(),
        object.getY(),
        object.getWidth(),
        object.getHeight()); // sets location and size of the paddle
    game_frame.add(paddle); // game_frame gets link image

    // a single ball = test //
    JPanel ball = new JPanel(); // creates the JPanel to hold the ball object and image
    Ball obj = new Ball(); // creates the ball object
    JLabel stone =
        new JLabel(
            new ImageIcon(
                "C:\\Users\\Daniel\\workspace\\Pong_Game\\src\\resources\\Pics\\Ball.png")); // creates the ball image
    ball.add(obj); // adds the object
    ball.add(stone); // adds the image
    ball.setBounds(750, 250, obj.getWidth(), obj.getHeight()); // sets ball location and size
    game_frame.add(ball); // adds it to the screen

    // final frame things//
    game_frame.pack();
    game_frame.setResizable(false);
    game_frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    game_frame.setVisible(true);
  } // end of screen constructor
Exemplo n.º 21
0
 public void mouseClicked(MouseEvent ev) {
   ball.move(ev.getX(), ev.getY());
   repaint();
 }
Exemplo n.º 22
0
    public void checkForHit() {

      // change ball speed when ball hits paddle
      if (ball.getShape().intersects(paddle.getShape())) {
        int leftSide = paddle.getX();
        int middleLeft = paddle.getX() + (int) (paddle.getWidth() / 3);
        int middleRight = paddle.getX() + (int) (2 * paddle.getWidth() / 3);
        int rightSide = paddle.getX() + paddle.getWidth();

        if ((ball.getX() >= leftSide) && (ball.getX() < middleLeft)) {
          // change ball speed
          ball.setXspeed(-2);
          ball.setYspeed(-2);
        }
        if ((ball.getX() >= middleLeft) && (ball.getX() <= middleRight)) {
          // change ball speed
          ball.setYspeed(-2);
        }
        if ((ball.getX() > middleRight) && (ball.getX() <= rightSide)) {
          // change ball speed
          ball.setXspeed(2);
          ball.setYspeed(-2);
        }
      }

      // change ball speed when ball hits brick
      for (int i = 0; i < bconfig.getRows(); i++) {
        for (int j = 0; j < bconfig.getCols(); j++) {
          if (bconfig.exists(i, j)) {
            if (ball.getShape().intersects(bconfig.getBrick(i, j).getShape())) {
              Point ballLeft =
                  new Point(
                      (int) ball.getShape().getX(),
                      (int) (ball.getShape().getY() + ball.getShape().getHeight() / 2));
              Point ballRight =
                  new Point(
                      (int) (ball.getShape().getX() + ball.getShape().getWidth()),
                      (int) (ball.getShape().getY() + ball.getShape().getHeight() / 2));
              Point ballTop =
                  new Point(
                      (int) (ball.getShape().getX() + ball.getShape().getWidth() / 2),
                      (int) ball.getShape().getY());
              Point ballBottom =
                  new Point(
                      (int) (ball.getShape().getX() + ball.getShape().getWidth() / 2),
                      (int) (ball.getShape().getY() + ball.getShape().getHeight()));
              if (bconfig.getBrick(i, j).getShape().contains(ballLeft)) {
                // change ball speed
                ball.setXspeed(2);
                score++;
              } else if (bconfig.getBrick(i, j).getShape().contains(ballRight)) {
                // change ball speed
                ball.setXspeed(-2);
                score++;
              }
              if (bconfig.getBrick(i, j).getShape().contains(ballTop)) {
                // change ball speed
                ball.setYspeed(2);
                score++;
              } else if (bconfig.getBrick(i, j).getShape().contains(ballBottom)) {
                // change ball speed
                ball.setYspeed(-2);
                score++;
              }

              // remove brick
              bconfig.removeBrick(i, j);
            }
          }
        }
      }
    }
Exemplo n.º 23
0
  /**
   * Determines if the given ball has collided with the paddle and returns true if it has and false
   * if it has not. If the ball has collided with the paddle the balls movement vectors are modified
   * so the ball will bounce back at the proper angle.
   *
   * @param ball is the ball to examine to see if it has collided with the paddle
   * @return Returns true if ball collides with paddle and if so updates vectors
   */
  public boolean collision(Ball ball) {
    // Temp variables for paddle corner coordinates;
    int pX1 = this.x;
    int pX2 = pX1 + this.width - 1;
    int pY1 = this.y;
    int pY2 = pY1 + this.height - 1;

    // Temp variables  for ball coordinates, movement vectors, and radius
    int bX = ball.getX();
    int bY = ball.getY();
    int vX = ball.getXVector();
    int vY = ball.getYVector();
    int bRad = ball.getRadius();

    // Testing for collisions with the paddle edges and the Ball
    if (bX < pX1 && bY >= pY1 && bY <= pY2 && vX > 0) // left edge
    {
      if (bX + bRad > pX1) // will collide with edge
      {
        ball.setX(pX1 - bRad);
        ball.setXVector(-Math.abs(vX)); // Change x direction
        return true;
      }
    } else if (bX > pX2 && bY >= pY1 && bY <= pY2 && vX < 0) // right edge
    {
      if (bX - bRad < pX2) // will collide with edge
      {
        ball.setX(pX2 + bRad);
        ball.setXVector(Math.abs(vX)); // Change x direction
        return true;
      }
    } else if (bY < pY1 && bX >= pX1 && bX <= pX2 && vY > 0) // top edge
    {
      if (bY + bRad > pY1) // will collide with edge
      {
        ball.setY(pY1 - bRad);
        ball.setYVector(-Math.abs(vY)); // Change y direction
        return true;
      }
    } else if (bY > pY2 && bX >= pX1 && bX <= pX2 && vY < 0) // bottom edge
    {
      if (bY - bRad < pY2) // will collide with edge
      {
        ball.setY(pY2 + bRad);
        ball.setYVector(Math.abs(vY)); // Change y direction
        return true;
      }
    } else // Otherwise test to see if the ball hits a corner of the paddle
    {
      if (ball.distance(pX1, pY1) <= bRad
          && ball.distance(pX1, pY1) <= ball.distance(pX2, pY1)) { // top-left corner collision
        if (Math.abs(pX1 - bX) >= Math.abs(pY1 - bY)) if (vX > 0) ball.setXVector(-vX);
        if (Math.abs(pX1 - bX) <= Math.abs(pY1 - bY)) if (vY > 0) ball.setYVector(-vY);
        return true;
      } else if (ball.distance(pX2, pY1) <= bRad) { // top-right corner collision
        if (Math.abs(pX2 - bX) >= Math.abs(pY1 - bY)) if (vX < 0) ball.setXVector(-vX);
        if (Math.abs(pX2 - bX) <= Math.abs(pY1 - bY)) if (vY > 0) ball.setYVector(-vY);
        return true;
      } else if (ball.distance(pX2, pY2) <= bRad
          && ball.distance(pX2, pY2) <= ball.distance(pX1, pY2)) { // bottom-right corner collision
        if (Math.abs(pX2 - bX) >= Math.abs(pY2 - bY)) if (vX < 0) ball.setXVector(-vX);
        if (Math.abs(pX2 - bX) <= Math.abs(pY2 - bY)) if (vY < 0) ball.setYVector(-vY);
        return true;
      } else if (ball.distance(pX1, pY2) <= bRad) { // bottom-left corner collision
        if (Math.abs(pX1 - bX) >= Math.abs(pY2 - bY)) if (vX > 0) ball.setXVector(-vX);
        if (Math.abs(pX1 - bX) <= Math.abs(pY2 - bY)) if (vY < 0) ball.setYVector(-vY);
        return true;
      }
    }
    return false;
  }
Exemplo n.º 24
0
 public void actionPerformed(ActionEvent e) {
   ball.changeColor(colorList[++colorNo % colorList.length]);
   repaint();
 }
Exemplo n.º 25
0
 public boolean intersectsWith(Ball other) {
   return this.x <= other.x && other.x <= this.rightX()
       || this.x <= other.rightX() && other.rightX() <= this.rightX();
 }
Exemplo n.º 26
0
 public void paint(Graphics g) {
   ball.paint(g);
 }