1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

JavaPong

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

  1. gulliccj

    gulliccj New Member

    Joined:
    Jun 7, 2008
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Hello, I am trying to make PONG in a Java Applett, I need to add a MouseListener and MouseMotionListener, but I am already implementing Runnable, how do i solve this problem. Please Help.

    the code for JavaPong.java, HTML below

    Code:
    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;
    		}
    	}
    }

    JavaPong.html
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    	<head>
    	</head>
    	<body bgcolor="black">
    		<center>
    			<applet
    				code	= "JavaPong.class"
    				width	= "640"
    				height	= "480"
    				>
    			</applet>
    		</center>
    	</body>
    </html>
    
    
    
     
  2. gulliccj

    gulliccj New Member

    Joined:
    Jun 7, 2008
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Any Help would be awesome
     

Share This Page