Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 21st, 2005, 04:21 PM
Registered User
 
Join Date: Aug 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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;

}











Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.