Problem with mouseListener
Code:
I've got this Jpanel:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class A3JPanel extends JPanel implements ActionListener, KeyListener, MouseListener {
private Rectangle boundary;
private Ant[] ants;
private Player player;
private Timer t;
private GameOver over;
public A3JPanel() {
boundary = new Rectangle(5, 5, 490, 490);
ants = new Ant[30];
for (int i = 0; i < ants.length; i++) {
ants[i] = new Ant();
}
player = new Player(250, 300);
addKeyListener(this);
addMouseListener(this);
t = new Timer(20, this);
}
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < ants.length; i++) {
ants[i].move();
}
player.move();
repaint();
}
public void keyPressed(KeyEvent e) {
t.start();
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
for (int i = 0; i < ants.length; i++) {
ants[i].move();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
player.turnLeft();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
player.turnRight();
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
player.turnUp();
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
player.turnDown();
}
repaint();
}
[COLOR="yellow"]public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
for (int i=0; i < ants.length; i++){
if(ants[i].getAntSize().contains(p)){
ants[i].setX();
}
}[/COLOR]
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < ants.length; i++) {
ants[i].draw(g);
}
g.drawRect(boundary.x, boundary.y, boundary.width, boundary.height);
player.draw(g);
for (int i = 0; i < ants.length; i++) {
if (ants[i].getAntSize().intersects(player.getPlayerSize())) {
over.draw(g);
}
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
and Ant class of :
Code:
import java.awt.*;
public class Ant {
public int x, y, width, height;
public int xSpeed, ySpeed;
public Color antColor;
public Ant () {
x = 200;
y = 300;
width = 15;
height = 30;
xSpeed = (int) (Math.random() * 5) - 3;
ySpeed = (int) (Math.random() * 5) - 3;
int red = (int)(Math.random() * 100);
int green = (int)(Math.random() * 100);
int blue = (int)(Math.random() * 100);
antColor = new Color(red, green, blue);
}
public void move() {
x += xSpeed;
y += ySpeed;
if (x <= 15) {
xSpeed = -1 * xSpeed;
} else if (x >= 490) {
xSpeed = -1 * xSpeed;
}
if (y <= 105) {
ySpeed = -1 * ySpeed;
} else if (y >= 565) {
ySpeed = -1 * ySpeed;
}
}
[COLOR="Yellow"]public void setX() {
x = x + 1000;
}[/COLOR]
public Rectangle getAntSize () {
return new Rectangle (x, y, width, height);
}
public void draw(Graphics g) {
g.setColor(antColor);
g.fillOval(x-10, y-100, width, height);
g.setColor(Color.black);
g.drawOval(x-10, y-100, width, height);
}
}
I returned Ant class as Rectangle so I can use .contain but ants do not disappear even though it compiles perfectly. Do I have to change GetAntSize() or is anything else wrong?