Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default Help with Applet implementing mouselistener

    Hello everyone. I am trying to make a game where an image moves randomly around the applet and the user has to try to click on it. I have successfully got it to show the image in the applet and recognize when the image is clicked on, but now I can't get the position of the image to change, which should be pretty basic. Here is the code:
    Java Code:
    import java.applet.*;
    import java.awt.image.*;
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.Graphics;
    import javax.imageio.ImageIO;
    import java.awt.event.*;
    
    public class VinooGame extends Applet implements Runnable, MouseListener{
    	
    	//variables
    	int x_pos = 15, y_pos = 15;
    	int Speed = 20;
    	int AppWidth = 650, AppHeight = 650;
    	BufferedImage vpic = null;
    	TextArea textBox = null;
    	ArrayList <String> positive = new ArrayList <String>();
    	ArrayList <String> negative = new ArrayList <String>();
    	
    	
    	public void init(){
    		//creates the message lists
    		Scanner in;
    		try{
    		    	in = new Scanner(new File("positive.txt"));
    		    	while(in.hasNextLine()){
    		      		String phrase = in.nextLine();
    		      		positive.add(phrase);
    		    	}
    		    	in = new Scanner(new File("negative.txt"));
    		    	while(in.hasNextLine()){
    		      		String phrase = in.nextLine();
    		      		negative.add(phrase);
    		    	}
    		    }catch(IOException i){
    		    	System.out.println("Error: " + i.getMessage());
    		    }
    		    
    		// initializes applet size
    		resize(AppWidth,AppHeight);
    		
    		//creates picture of Vinoo
    		try {
    		    vpic = ImageIO.read(new File("vinoo.gif"));
    		} 
    		catch (IOException e) {
    	    	System.out.println("Error: " + e.getMessage());
    		}
    	}
    	
    	public void start(){
    		Thread th = new Thread(this);
    		th.start();
    	}
    	
    	public void stop(){
    		
    	}
    	
    	public void destroy(){
    		
    	}
    	
    	public void run() {
    		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    		
    		while (true){
    			x_pos = (int) (Math.random() * (AppWidth-vpic.getWidth()));
    			y_pos = (int) (Math.random() * (AppHeight-vpic.getWidth()));
    			x_pos++;
    			y_pos++;
    			repaint();
    
    			try 
    			{ 
    
    				Thread.sleep (Speed);
    
    			} 
    			catch (InterruptedException ex) 
    			{ 
    		    	System.out.println("Error: " + ex.getMessage());
    
    			} 
    			Thread.currentThread().setPriority(Thread.MAX_PRIORITY); 
    
    			} 
    		
    	}
    	
    	public void paint(Graphics g){
    
    		g.drawImage(vpic, x_pos, y_pos, null);
    	}
    	
    	public boolean isInImage(int x, int y){ //checks if the mouse is on the image
    		int h = vpic.getHeight();
    		int w = vpic.getWidth();
    		int minx = x_pos;
    		int maxx = x_pos + w;
    		int miny = y_pos;
    		int maxy = y_pos + h;
    		return (x >= minx && x <= maxx && y >= miny && y <=maxy);
    	}
    
    	public void mouseClicked(MouseEvent e) {
    		int mx = e.getX();
    		int my = e.getY();
    		if (this.isInImage(mx, my))
    			Message(positive);
    		else
    			Message(negative);
    	}
    	
    	public void mouseEntered(MouseEvent e) {
    		
    		
    	}
    
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public void mousePressed(MouseEvent e) {
    
    	}
    
    	public void mouseReleased(MouseEvent e) {
    		
    	}
    	public void Message(ArrayList <String> mess){
    		int i = (int) (Math.random()*mess.size());
    		System.out.println(mess.get(i));
    	}
    
    }
    (It is called Vinoo game because the image is the picture of a kid in my class named Vinoo).
    It would appear that there is a conflict between the run method and the mouselistener methods, but I don't know how to make it run while still making mouselistener work.
    Any help would be much appreciated, Thanks.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    For starters, any code that updates the UI must be executed on the event thread (using invokeLater() or an event listener).

    You'll probably find it easier to create animations if you use a Swing Timer.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    Ok, I'm not really sure what your way was but I figured out how to do it another way. Thanks for replying.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Mouselistener questions
    By jigglywiggly in forum New To Java
    Replies: 0
    Last Post: 05-07-2009, 10:59 PM
  2. Replies: 5
    Last Post: 02-20-2009, 10:18 PM
  3. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 04:30 AM
  4. MouseListener
    By Aswq in forum New To Java
    Replies: 12
    Last Post: 07-18-2008, 09:10 AM
  5. I need help with my MouseMotionAdapter and MouseListener.
    By MurderfaceX4 in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 04:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •