Results 1 to 2 of 2
  1. #1
    js91723 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default Problem with mouseListener (REPOST)

    Java 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 :

    Java 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    In order to avoid duplication of effort, please do not double post the same question; rather, let's stick to one thread. I'm locking this one.

    Thanks for your cooperation.

Similar Threads

  1. Problem with mouseListener
    By js91723 in forum AWT / Swing
    Replies: 12
    Last Post: 05-31-2010, 07:49 PM
  2. can't get x and y from mouselistener
    By j2me64 in forum Java 2D
    Replies: 3
    Last Post: 04-24-2010, 04:57 PM
  3. How to solve this JTable problem?? [REPOST]
    By doha786 in forum New To Java
    Replies: 4
    Last Post: 02-05-2010, 04:02 AM
  4. MouseListener & GUI
    By Suurisa in forum New To Java
    Replies: 2
    Last Post: 10-27-2009, 12:52 AM
  5. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 03:30 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
  •