Hi everyone, I'm working on a project for my computer science class and we're supposed to make a java applet game. I'm not very far in the game quite yet, but I'm trying to make sure that the mouseListener and the rectangles work before I continue on with the game. I found a code on the internet for mouseListener, where they use rectangles to determine whether the click was inside or outside the rectangle.
I was trying to use that with the program that I am doing, but it's not working out so nicely. I'm not sure if I'm positioning the rectangles correctly, or if I have the mouselistner imported properly, but when I click in the applet, it's not doing anything (as in, I think it's not picking up the rectangles or something). I'm not sure what I'm doing wrong, so if you could give me some tips it would be greatly appreciated. Here's the code that I'm working with right now.

package test;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

import java.awt.Graphics;

public class KeyEventsAnimation extends Applet implements MouseListener {
    public static final int WIDTH = 293;
    public static final int HEIGHT = 353;
	private static final String Graphics = null;
	
	int i = 1;
	int xpos; 
	int ypos; 
	
    Graphics offscreenGraphics;
    Image offscreenImage;
    Graphics backBuffer;
    Image frontBuffer;
    Image background;
    Image background2;
    Image trainerEnemy;
    Image trainerYou;
    Image up;
    Image down;
    Image right;
    Image left;
    Image ball;
    Image yourHP;
    Image enemyHP;
    Image pokemonEnemy;
    Ball myBall = new Ball(160,300,1);
    Dimension appletSize;
    boolean leftKey = false;
    boolean rightKey = false;
    boolean upKey = false;
    boolean downKey = false;
    
    
    
    int rect1xco,rect1yco,rect1width,rect1height;
    int rect2xco,rect2yco,rect2width,rect2height; 
    int rect3xco,rect3yco,rect3width,rect3height; 
    int rect4xco,rect4yco,rect4width,rect4height; 


    // The variable that will tell whether or not the mouse 
    // is in the applet area. 
    boolean mouseEntered; 
    boolean check;

    // variable that will be true when the user clicked in the rectangle  
    // the we will draw. 
    boolean rect1Clicked;
    boolean rect2Clicked; 
    boolean rect3Clicked; 
    boolean rect4Clicked; 
   
    public void init(){
    	
     
    	
        this.setSize(WIDTH, HEIGHT);
        appletSize = getSize();
        addKeyListener(new MyKeyListener());
        frontBuffer = createImage(appletSize.width,appletSize.height);
        backBuffer = frontBuffer.getGraphics();
        background = getImage(getDocumentBase(), "bg_fuller.png");
        background2 = getImage(getDocumentBase(), "fightMenu.png");
        trainerEnemy = getImage(getDocumentBase(), "bianca.gif");
        trainerYou = getImage(getDocumentBase(), "Oshawott_BW.gif");
        pokemonEnemy = getImage(getDocumentBase(), "Tepig_BW.gif");
        yourHP = getImage(getDocumentBase(), "yourHP.png");
        enemyHP = getImage(getDocumentBase(), "enemyHP.png");
        
        ball = getImage(getDocumentBase(),"down.png");
        Thread t = new Thread(new MainLoop());
        t.start();
        
        
      //rectangle 1
        rect1xco = 0	; 
        rect1yco = 266	; 
        rect1width = 50; 
        rect1height = 40; 
        
        //rectangle 2
        rect2xco = 155;
        rect2yco = 266; 
        rect2width = 50; 
        rect2height = 40; 
        
      //rectangle 3
        rect3xco =0;
        rect3yco = 324	; 
        rect3width = 50; 
        rect3height = 40; 
        
      //rectangle 4
        rect4xco = 155;
        rect4yco = 324; 
        rect4width = 50; 
        rect4height = 40; 
     
        addMouseListener(this); 
    	
        
        
    }
    private class MyKeyListener extends KeyAdapter{
        public void keyPressed(KeyEvent e){
            switch (e.getKeyCode()){
                case KeyEvent.VK_LEFT:
                leftKey = true;
                break;
                case KeyEvent.VK_RIGHT:
                rightKey = true;
                break;
                case KeyEvent.VK_UP:
                upKey = true;
                break;
                case KeyEvent.VK_DOWN:
                downKey = true;
                break;
            }
        }
        public void keyReleased(KeyEvent e){
            switch (e.getKeyCode()){
                case KeyEvent.VK_LEFT:
                leftKey = false;
                break;
                case KeyEvent.VK_RIGHT:
                rightKey = false;
                break;
                case KeyEvent.VK_UP:
                upKey = false;
                break;
                case KeyEvent.VK_DOWN:
                downKey = false;
                break;
            }
        }
    }
    
      public class MainLoop implements Runnable{
        public MainLoop(){
        }
        public void run(){
            while(true){
                if(upKey){  myBall.moveUp();  }
                if(downKey){ myBall.moveDown(); }
                if(leftKey){ myBall.moveLeft(); }
                if(rightKey){ myBall.moveRight(); }
                repaint();
                try {
                    Thread.sleep(20);
                }
                catch (InterruptedException ex){
                }
            }
        }
    }
    
    public void drawBackground (Graphics display)
    {
    	
    	backBuffer.drawImage(background,0,0,null);
    	
    	startMenu(display);
    }
       
    
    public void openingStartMenu(Graphics g)
    {
    	
    	g.clearRect(0, 0, 500, 500);
    	backBuffer.drawImage(pokemonEnemy,0,0,null);
    	
    	
    }
    
    public void startMenu(Graphics display){
    	
    	
    	backBuffer.drawImage(background,0,0,null);
    	backBuffer.drawImage(background2,0,191,293, 162, null);
    	backBuffer.drawImage(yourHP,130,120,null);
    	backBuffer.drawImage(enemyHP,0,0,null);
    	//g.clearRect(0, 0, 500, 800);
        backBuffer.drawImage(pokemonEnemy,170,50,null);
        backBuffer.drawImage(trainerYou, 50,130,null);
        backBuffer.drawImage(ball, myBall.getPx(), myBall.getPy(), null);
        //backBuffer.drawString("CLICK ON SCREEN TO GAIN FOCUS THEN...",0,230);
        //backBuffer.drawString("press arrow keys to move ball",0,245);
        display.drawImage(frontBuffer,0,0,this);
        drawPokemon(display);
    }
    
    public void drawPokemon(Graphics display)
    {
    	    	
    }
    
 
    
    public void update(Graphics display){
    	
    	startMenu(display);
    }
    

    
    public void paint (Graphics g)
    {
    	
    	update(g);
    }


    public void mouseClicked (MouseEvent me) {

    	 xpos = me.getX(); 
    	 ypos = me.getY(); 
    	  


    	  // Check if the click was inside the rectangle area. 
    	  if (xpos > rect1xco && xpos < rect1xco+rect10width && ypos >rect1yco &&  
    	    ypos < rect1yco+rect1height) {
    		  rect1Clicked = true; 
    		  
    		
    		  if (rect1Clicked == true)
    		  {
    			  
    			  System.exit(0);
    		  }
    		  
    	  }
    	  
       	  
    	  else if (xpos > rect2xco && xpos < rect2xco+rect2width && ypos >rect2yco &&  
    			    ypos < rect2yco+rect2height) {
    		  rect2Clicked = true;
    		
    		  
    		  if (rect2Clicked == true)
    		  {
    			  System.exit(0);
    		  }

    	  }
    	  
    	  else if (xpos > rect3xco && xpos < rect3xco+rect3width && ypos >rect3yco &&  
    			    ypos < rect3yco+rect3height) {
    		  rect3Clicked = true;
    		  
    		  
    		  if (rect3Clicked == true)
    		  {
    			  System.exit(0);
    		  }
    	  }
    	  
    	  else if (xpos > rect4xco && xpos < rect4xco+rect10width && ypos >rect4yco &&  
    			    ypos < rect4yco+rect4height) {
    		  rect4Clicked = true;
    		  
    		  
    		  if (rect4Clicked == true)
    		  {
    			  
    			  System.exit(0);
    		  }
    	  }
    	  

    	 }

    	 
    	 public void mousePressed (MouseEvent me) {}

    	 public void mouseReleased (MouseEvent me) {} 

    	 public void mouseEntered (MouseEvent me) {
  
    		 setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    	 
    	 }

    	 public void mouseExited (MouseEvent me) {

    	  
    	 
    	 }  
    	 
}
This article has been dead for over six months. Start a new discussion instead.