Now my main problem is that I need to make the ball bounce back after it touches the paddle...Right now the ball goes through the paddle.
And the paddle is moved using arrow keys. So now I need like a code for me to make the ball bounce back when it touches the paddle. And I think this can be done using if statement..not sure. But any help will be greatly appreciated...but here are my classes and codes for them.
ImagePong
Expand|Select|Wrap|Line Numbers
- /**
- * Balloon.java uses a PNG file that is displayed by the applet
- *
- * @author Sulav
- * @version 11/29/07
- */
- //_________________import statements______________________________________
- import java.awt.*;
- import javax.swing.ImageIcon; // needed to use image files from disk
- public class ImagePong
- {
- //________________instance variables/properties_________________________
- Image frame2; //object holds the actual graphic
- Image frame1; //object holds the actual graphic
- int x=0, y=0, xSpeed=15, ySpeed=15, maxX=500, maxY=500;
- Color c;
- int x2=0, y2=0, x2Speed=15, y2Speed=15, max2X=500, max2Y=500;
- //__________constructors_______________________________________________
- public ImagePong()
- { //pong.png must be in your project directory
- frame1 = new ImageIcon("./paddle2.jpg").getImage(); //load the graphic
- x = 190; y = 450;
- frame2 = new ImageIcon("./back.jpg").getImage(); //load the graphic
- x2 = 1; y2 = 1;
- } //end of Balloon() constructor
- //____________________methods___________________________________
- // getImage() sends back the current image
- public Image getImage()
- {
- return frame1;
- } // end of getImage()
- public Image getImage2()
- {
- return frame2;
- } // end of getImage()
- // getX() sends back the x position
- public int getX()
- {
- return x;
- } // end of getX
- //get Y () sends back the y position )
- public int getY()
- {
- return y;
- } // end of getY
- // getX() sends back the x position
- public int getX2()
- {
- return x2;
- } // end of getX
- //get Y () sends back the y position )
- public int getY2()
- {
- return y2;
- } // end of getY
- public void setXSpeed (int newXSpeed)
- {
- xSpeed=newXSpeed;
- xSpeed=newXSpeed;
- }
- public void setYSpeed (int newYSpeed)
- {
- ySpeed=newYSpeed;
- ySpeed=newYSpeed;
- }
- public void moveRight()
- {
- if(x<maxX + 12)
- {
- x+=xSpeed;
- }
- }
- public void moveLeft()
- {
- if(x<maxX + 12)
- {
- x-=xSpeed;
- }
- }
- } // end of Balloon class
ImageMover(Applet)
Expand|Select|Wrap|Line Numbers
- /**
- * ImageMover.java animates an image in the applet window.
- * It uses double buffering to provide nice smooth animation
- *
- * @author Sulav
- * @version 11/29/07
- */
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- public class ImageMover extends Applet implements Runnable, KeyListener
- {
- private Image buffer; //holds a copy of the applet window
- private Graphics gBuffer; // the copy's graphics page
- private ImagePong back; //create a paddle object to be animated
- private ImagePong paddle; //create a background object to be animated
- private Thread t; //create the thread to do it
- private Ball pingpong; //thing being bounced
- public void init()
- {
- // create graphics buffer, the size of the applet
- buffer=createImage(this.getWidth(),this.getHeight());
- gBuffer=buffer.getGraphics();
- // Initialize drawing colors
- setBackground(Color.black);
- setForeground(Color.white);
- pingpong = new Ball(this.getWidth(), this.getHeight(), Color.red);
- addKeyListener(this); // register the KeyListener with the applet
- // create the Balloon object and thread to control animation
- paddle = new ImagePong();
- back = new ImagePong();
- t = new Thread(this);
- t.start();
- } // end of init()
- // run() is used by the Thread T to keep the balloon moving
- public void run()
- {
- // run this loop forever
- while(true)
- {
- pingpong.move();
- // move the balloon given the size of the applet window
- //pong.move(this.getWidth(), this.getHeight());
- repaint();
- try {t.sleep(20);} //pause a little to slow things down
- catch (Exception e) {}
- }
- } //end of run
- // method to stop the thread from going. will cause a warning when compiled
- public void stop()
- {
- t.stop();
- } // end of stop() method
- // update() needs to run paint() so Java doesnt erase the window
- public void update(Graphics g)
- {
- paint(g);
- } // end of update()
- // paint() paints the applet window
- public void paint(Graphics g)
- {
- pingpong.draw(g);
- // set the drawing color and cover the buffer witha gilled rectangle
- gBuffer.setColor(Color.black);
- gBuffer.fillRect(0, 0, this.getWidth(), this.getHeight());
- //draw the balloon image in the BUFFER at its own coordinates
- //gBuffer.drawImage(paddle.getImage(), paddle.getX(),
- // paddle.getY(), this);
- // draw the balloon image in the BUFFER at its own coordinates
- gBuffer.drawImage(back.getImage2(), back.getX2(),
- back.getY2(), this);
- // draw the balloon image in the BUFFER at its own coordinates
- gBuffer.drawImage(paddle.getImage(), paddle.getX(),
- paddle.getY(), this);
- // now draw the iamge to the applet window
- g.drawImage (buffer, 0, 0, this);
- } // end of paint()
- public void keyPressed(KeyEvent event) {
- int keyCode = event.getKeyCode(); //get the key hit
- if (keyCode == KeyEvent.VK_D)
- {
- paddle.moveRight();
- }
- if (keyCode == KeyEvent.VK_A)
- {
- paddle.moveLeft();
- }
- event.consume(); //keep anything else from using the key
- repaint(); // signal Java to redraw the window
- }
- public void keyReleased(KeyEvent event) {
- event.consume(); // ignore keyReleased
- }
- public void keyTyped(KeyEvent event) {
- event.consume(); // ignore keyTyped
- }
- } // end of ImageViewer class
Ball
Expand|Select|Wrap|Line Numbers
- /**
- * Ball.java is the object that will get bounced around
- *
- * @author Sulav
- * @version 11/16/07
- */
- import java.awt.*;
- import java.awt.geom.*;
- public class Ball
- {
- // set constants for colors
- final Color blue = Color.black, green = Color.white;
- // variables for location, speed, and direction of ball size of window
- int x, y, horVel = 13, vertVel = 16, maxX, maxY;
- Color c;
- public Ball(int mx, int my, Color c)
- {
- maxX = mx; // set the max variables
- maxY = my;
- this.c = c;
- //frame2 = new ImageIcon("./pong.jpg").getImage();
- // give the ball a random starting position
- x = (int) (Math.random() * maxX) + 1;
- y = (int) (Math.random() * maxY) + 1;
- }
- public void draw(Graphics g)
- {
- Graphics2D g2 = (Graphics2D) g; //supersize the drawing page
- GradientPaint grad = new GradientPaint(x+20, y-20, green, x+20, y+60, blue);
- //g2.setPaint(grad);
- g2.setPaint(c);
- g2.fill (new Ellipse2D.Double(x, y, 40, 40));
- } // end of draw()
- public void move()
- {
- y+=vertVel; // add the horizontal velocity to x
- x+=horVel;
- if ((y > maxY) || (y < 1)) //checking left, right boundaries
- {
- vertVel = -vertVel; //reverse direction if at left/right edge
- }
- if ((x > maxY) || (x < 1)) //checking left, right boundaries
- {
- horVel = -horVel; //reverse direction if at left/right edge
- }
- } // end of move() method
- } //end of Ball class