1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

mouseListener and MouseMotionListener in Java

Discussion in 'Java' started by gulliccj, Feb 23, 2009.

Thread Status:
Not open for further replies.
  1. gulliccj

    gulliccj New Member

    Joined:
    Jun 7, 2008
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    hello, for a school assignment, i have elected to do Pong, in a standard applet. to come at this, I used a thread, that implements Runnable for the main class. I also need to use MouseListener and MouseMotionListener. But i cannot implement them. How do i get around that. Below i have copied the code. Please help! It is due on friday.
     
  2. gulliccj

    gulliccj New Member

    Joined:
    Jun 7, 2008
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    HTML:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.applet.*;
    
    public class JavaPong extends Applet implements Runnable{
    
    
    	String color = "WHITE";
    	boolean done = false;				// done
    	Point ballPosition;					//position
    	double ballx, bally; 				// velocity
    	int rad;							//balls size
    	Rectangle player, computer, walls;	//rectangles
    	Thread game;						//main executing thread
    	Image doublebuffer;					//offscreen buffer
    
    	AudioClip wavPong;					//sound effect
    	boolean hit = false;				//if ball hit wall or paddle
    
    	Image bgImage;						//background image
    
    	boolean start;
    
    	int yourScore,oppScore;
    
    	Point mouse;						//Mouses Current Position
    
    	public void run(){
    		//infinate game loop
    		try{
    			while(!done){
    				testPlayer();
    				tesWalls();
    				updateBall();
    				updatePlayer();
    				playSounds();
    				repaint();
    				Thread.sleep(10);
    
    			}
    		}
    		catch(InterruptedException e){
    		}
    	}
    
    	//init(), then start()
    
    	public void init(){
    		String radIns = JOptionPane.showInputDialog(null, "Enter ball Radius: ");//get bal radius
    		int radIn = Integer.parseInt(radIns);									//parse radius
    		rad = radIn;															//ball radius = x;
    		ballx = -4.0;
    		bally = 4.0;
    		ballPosition = new Point(getWidth()/2, getHeight()/2);					//set initial ball location
    
    		//create offscreen buffer
    		doublebuffer = createImage(getWidth(),getHeight());
    
    		hit = false;
    
    		//paddle dimensions
    		int x,y,w,h;
    
    		w = (int)(0.02 * getWidth());
    		h = (int)(.35 * getHeight());
    		x = w;
    		y =	(int)(.50 * getHeight());
    
    		player = new Rectangle(x, y, w, h);
    
    		mouse = new Point(0,0);
    		addMouseListener(this);
    		addMouseMotionListener(this);
    
    
    
    	}
    
    
    
    	public void start(){
    		wavPong = getAudioClip(getCodeBase(), "pong.wav");
    		game = new Thread(this);			//This is a thread
    		game.start();						//Thread will in turn execute run()
    		bgImage = getImage(getCodeBase(), "JavaPong.png");
    	}
    
    	//start MouseListeners
    
    	public void mouseClicked(MouseEvent e){
    		if(start = false){
    			startGame();
    		}
    	}
    
    	public void mouseEntered(MouseEvent e){
    	}
    
    	public void mouseExited(MouseEvent e){
    	}
    
    	public void mousePressed(MouseEvent e){
    	}
    
    	public void mouseReleased(MouseEvent e){
    	}
    
    	//MouseMotionListener 2 abstract
    	public void mouseDragged(MouseEvent e){
    
    	}
    	public void mouseMoved(MouseEvent e){
    		mouse.setLocation(0,e.getY());
    	}
    
    	void updatePlayer(){
    		player.y = mouse.y;
    	}
    
    	void startGame(){
    
    	}
    
    
    
    	public void paint(Graphics g){
    
    		Graphics dbg = doublebuffer.getGraphics();
    
    		dbg.setColor(new Color(0,100,100));
    		dbg.fillRect(0,0,getWidth(),getHeight());
    
    		dbg.drawImage(bgImage, getWidth()/2-bgImage.getWidth(this)/2,getHeight()/2 - bgImage.getHeight(this)/2 , this);
    
    		dbg.setColor(Color.WHITE);
    		dbg.fillOval(ballPosition.x-rad,ballPosition.y-rad,rad*2, rad*2);		//draw ball
    
    		dbg.setColor(Color.BLACK);
    		dbg.fillRect(player.x-player.width/2, player.y - player.height/2, player.width, player.height);
    		dbg.setColor(Color.WHITE);
    		dbg.fillRect(player.x-player.width/2+2, player.y - player.height/2+2, player.width-4, player.height-4);
    
    		g.drawImage(doublebuffer, 0, 0, this);
    	}
    
    	public void update(Graphics g){
    		paint(g);
    	}
    
    	public void tesWalls(){
    		int top = 0, left = 0, right = getWidth()-1, bottom = getHeight() - 1;
    
    		if((ballPosition.x + ballx - rad) <= left){
    			ballx *= -1;
    		}
    		if((ballPosition.x + ballx + rad) >= right){
    			ballx *= -1;
    		}
    		if((ballPosition.y + bally - rad) <= top){
    			bally *= -1;
    		}
    		if((ballPosition.y + bally + rad) >= bottom){
    			bally *= -1;
    		}
    
    
    	}
    
    	void testPlayer(){
    		//if ball is moving left
    
    		if(ballx < 0){
    			if((player.x+player.width/2) >= (ballPosition.x-rad)){
    				if((player.y-player.height/2) < (ballPosition.y +rad) && ((player.y + player.height/2) > (ballPosition.y- rad))){
    					//reverse direction add 5% more speed
    					ballx *= -1.05;
    					bally *= 1.05;
    					hit = true;
    				}
    			}
    		}
    	}
    
    	public void updateBall(){
    		ballPosition.x += ballx;
    		ballPosition.y += bally;
    	}
    
    	void playSounds(){
    		if(hit){
    			wavPong.play();//play audio
    			hit = false;
    		}
    	}
    }
    }
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Duplicate of JavaPong. Thread closed.
     
Thread Status:
Not open for further replies.

Share This Page