batBallapplet
the bat doesnt hit the ball the ball rather goes through the bat. i want the bat to hit the ball.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.Color;
import java.awt.BasicStroke;
import java.awt.geom.Line2D;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class BatBallApplet extends Applet
{
public boolean inPlay = false;
public BatBallApplet()
{
bat = new Rectangle(BAT_X, BAT_Y, BAT_WIDTH, BAT_HEIGHT);
class MousePressListener implements MouseMotionListener
{
public void mousePressed(MouseEvent event){}
public void mouseDragged(MouseEvent event){}
public void mouseReleased(MouseEvent event){}
public void mouseMoved(MouseEvent event)
{
int x = event.getX();
int y = 285; // determines the position of movement
bat.setLocation(x,y);
repaint();
}
public void mouseClicked(MouseEvent event)
{
inPlay = true;
}
}
MouseMotionListener listener = new MousePressListener();
addMouseMotionListener(listener);
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.draw(bat);
g2.fill(bat);
Ellipse2D.Double ball = new Ellipse2D.Double(x,y,Ball_WIDTH,Ball_HEIGHT);
g2.draw(ball);
g2.setColor(Color.red);
g2.fill(ball);
if(x >= getWidth())
xd = -2;
if(y >= getHeight())
yd = -2;
if(x <= 0)
xd = +2;
if(y <= 0)
yd = +2;
if (inPlay = true)
{
x = x + xd;
y = y + yd;
}
else
{
inPlay = false;
}
repaint();
try{
Thread.sleep(5);
}
catch(Exception e){}
}
private Ellipse2D ball;
private Rectangle bat;
public static final int BAT_X = 10;
public static final int BAT_Y = 285; //initialises the position of the bat
public static final int BAT_WIDTH = 30; // intialising bat size
public static final int BAT_HEIGHT = 10;// ""
public static final double Ball_WIDTH = 10;
public static final double Ball_HEIGHT = 10;
private int x;
private int y;
private int xd =1;
private int yd =1;
}
|