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.