Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-12-2009, 09:31 PM
Member
 
Join Date: Feb 2009
Posts: 9
Rep Power: 0
dsym@comcast.net is on a distinguished road
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:
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-13-2009, 10:43 AM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-13-2009, 08:18 PM
Member
 
Join Date: Feb 2009
Posts: 9
Rep Power: 0
dsym@comcast.net is on a distinguished road
Default
Ok, I'm not really sure what your way was but I figured out how to do it another way. Thanks for replying.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-14-2009, 01:57 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,483
Rep Power: 8
Fubarable is on a distinguished road
Default
... and the other way was...?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mouselistener questions jigglywiggly New To Java 0 05-07-2009 10:59 PM
I need help with implementing an image in to my applet teamzerosc Java 2D 5 02-20-2009 10:18 PM
i need help for MouseListener sfaxianovic New To Java 2 08-21-2008 04:30 AM
MouseListener Aswq New To Java 12 07-18-2008 09:10 AM
I need help with my MouseMotionAdapter and MouseListener. MurderfaceX4 New To Java 1 12-07-2007 04:13 AM


All times are GMT +2. The time now is 09:45 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org