Ok I was doing this project but now Im halfway stuck. This project contains 3 class at the moment. Im mainly trying to make this applet like the game pong. I have the paddle and a ball and background and stuff.
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...b ut here are my classes and codes for them. ImagePong - /**
-
* 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(Appl et) - /**
-
* 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 - /**
-
* 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
1 1960 BigDaddyLH 1,216
Recognized Expert Top Contributor
So now I need like a code for me to make the ball bounce back when it touches the paddle.
You know how to make the ball bounce, because it is bouncing off the edges, right? Do you know how to detect if the ball is touching the paddle?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: duikboot |
last post by:
Hi all,
I'm trying to export a view tables from a Oracle database to a Mysql
database. I create insert statements (they look alright), but it all goes
wrong when I try to execute them in Mysql, because the dates must have
quotes on each side.
I just don't know how make the dates right.
Well I'll just show you the code and some insert statements it generates.
Could anyone please help me?
|
by: dwight0 |
last post by:
I am having a problem with a query,
I am not sure if i would use a join or a subquery to complete this
problem.
I have two queries, and i need to divide one by the other, but i cant
seem to get any
type of join to work with them.
Here is the situation.
I have a projectDB table that has a list of different projects for
each employee to work on.
Each project has an employee assigned to it.
|
by: m3ckon |
last post by:
Hi there,
had to rush some sql and am now going back to it due to a slow db
performance. I have a db for sales leads and have created 3 views based
on the data I need to produce.
However one o the views, which has subqueries to the other views is VERY
slow and it needs to be speeded up, but am unsure how, can anyone
help... below is the sql?
|
by: Colin McGuire |
last post by:
Hi, this is a really simple question I have been banging my head on a
brick wall over.
The program below changes the background colour of a form depending on
whether the cursor is inside a rectangle drawn on the form or not. It
works perfectly as shown below.
But it won't work if I change the values of scaleFactor, rotateFactor,
translateFactorX, translateFactorY in the program. I would like to
'correct' the values of e.X and e.Y in...
|
by: jon f kaminsky |
last post by:
Hi-
I've seen this problem discussed a jillion times but I cannot seem to
implement any advice that makes it work. I am porting a large project from
VB6 to .NET. The issue is using the combo box bound to a table as a lookup,
drawing values from another table to populate the available selections. This
all worked fine in VB6.
I have distilled the problem down to a simple form drawing data from the
Northwind database for a representative...
| |
by: phillip.s.powell |
last post by:
Now I have another SQL query for MySQL I can't figure out!! This is
overwhelming me completely and I also must have this figured out today
and I can't figure it out!!
UPDATE student_db.student
SET has_letter1 = (
SELECT i.letter1 FROM olddb.student i, student_db student s WHERE
s.unique_key = i.unique_key
)
|
by: Student |
last post by:
Hi All,
I have an assignment for my Programming language project to create a
compiler that takes a C++ file as input and translate it into the C
file. Here I have to take care of inheritance and operator overloading
and virtual functions. I mean it should not hamper the C++ meaning. In
frank and simple terms i need to implement a small C++ compiler in C++,
and i want the intermediate representation to be C. Please help me in
this....
|
by: Ilias Lazaridis |
last post by:
I have implemented a simple schema evolution support for django, due to
a need for a personal project. Additionally, I've provided an Audit:
http://case.lazaridis.com/wiki/DjangoAudit
As a result, I was censored ('banned' from the development list)
My initial non-public complain about censorship on django devel,
sent to six relevant project recipients remained unanswered.
|
by: atombee |
last post by:
Hi- this is the project that will not end! (sure you've all been there). I had originally purchased a php/css nav bar for the client, but it was buggy as hell, so I decided to do in css, in which I am still a novice, I am afraid.
You can see the sample nav bar at www.tangerine-sky.com/horizontal_nav.html (I am pasting source code below)
It's very simple, has the css included in the page ... works fine on a mac, okay on firefox on a PC but...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |