How to use MouseListener

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 8289 Views - Last Post: 05 April 2010 - 03:50 PM Rate Topic: -----

#1 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

How to use MouseListener

Posted 04 April 2010 - 03:01 PM

Hi all.I hipe you can help me in using a method od mouseListener.
I want to close the program when the mause is clicked , but i don`t know how tu use these methods.
Thus is teh code:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.applet.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

//import java.awt.event.*;


public class Life5 extends JApplet{
  public static void main(String s[]) {
    JFrame frame = new JFrame();
    frame.setTitle("Game of Life");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JApplet applet = new Life5();
    applet.init();
    frame.getContentPane().add(applet);
    frame.pack();
    frame.setVisible(true);
  }
  
  public void init() {
    JPanel panel = new LifePanel();
    getContentPane().add(panel);
 }
}


    
class LifePanel extends JPanel implements ActionListener,MouseListener{
  

// MouseListener methods
public void mouseClicked(MouseEvent e){
 
 //System.exit(0);
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}


  int n = 221;
  boolean[][] cells1;
  boolean[][] cells2;
  
  public LifePanel() {
    setPreferredSize(new Dimension(400, 400));
    setBackground(Color.white);
    cells1 = new boolean[n][n];
    cells2 = new boolean[n][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cells1[i][j] = false;
        cells2[i][j] = false;
      }
    }
	 cells1[(n/2)][(n/2)] = true;
    Timer timer = new Timer(1000, this);
    timer.start();
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    
    g2.setColor(Color.lightGray);
    int p = 0;
    int c = 2;
    int len = 443;
    for (int i = 0; i <= n; i++) {
      g2.drawLine(0, p, len, p);
      g2.drawLine(p, 0, p, len);
      p += 2;
    }
    g2.setColor(Color.black);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (cells1[i][j]) {
          int x = i*c;
          int y = j*c;
          g2.fillOval(x, y, c, c);
        }
      }
    }
  }
  
  public void actionPerformed(ActionEvent e) {
    boolean[][] cells = cells1;
    for (int i = 0; i < n ; i++) {
      for (int j = 0; j < n ; j++) {
        cells2[i][j] = cells[i][j];
        int nb = neighbors(cells,i, j);
			if(cells2[i][j] == false && nb != 1)
			cells2[i][j] = true;	
			else if( cells2[i][j] == true && (nb == 1 || nb ==  3))
		      cells2[i][j] = true;
		   else{ cells2[i][j] = false;}
				
	 //System.out.println(nb);	
		  
      }

		}		    

	 cells1 = cells2;
    cells2 = cells;
    repaint();
  }
  
 private int countNeighbor(boolean[][] cells,int x, int y) { 
        if (x<0||x>n-1) { return 0; } 
        if (y<0||y>n-1) { return 0; } 
        return cells[x][y] ? 1 : 0; 
} 
 
private int neighbors(boolean[][] cells,int x, int y) { 
        return countNeighbor(cells,x, y-1)  
                + countNeighbor(cells,x, y+1)
                + countNeighbor(cells,x-1, y)  
                + countNeighbor(cells,x+1, y); 
}  


}


This post has been edited by drilli: 04 April 2010 - 03:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to use MouseListener

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: How to use MouseListener

Posted 04 April 2010 - 03:07 PM

you should add a Mouselistener to the panel LifePanel:
addMouseListener(this);//this, since LifePanel implements MouseListener


override the mouseClicked method to exit the program.
here is a tutorial on implementing the MouseListener:
http://java.sun.com/...selistener.html
Was This Post Helpful? 0
  • +
  • -

#3 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: How to use MouseListener

Posted 05 April 2010 - 02:37 PM

I tried but I don`t know how to do it because I am e begginer.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: How to use MouseListener

Posted 05 April 2010 - 02:45 PM

That is no excuse...in fact, as "e beginner" you ought to learn how to do something like this. Why don't you read through the tutorial that japanir gave you. If you get frustrated, that's a good thing, and means that you are stretching your limits and learning. If you need help though please post your code and tell us why it is not working and we will see how we can help you.
Was This Post Helpful? 1
  • +
  • -

#5 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: How to use MouseListener

Posted 05 April 2010 - 02:47 PM

Hi all.I hope you can help me in using a method od mouseListener.
I want to close the program when the mause is clicked , but i don`t know how tu use these methods.
This is the code:




import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.awt.geom.*; 
import java.applet.*; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseEvent; 
 
//import java.awt.event.*; 
 
 
public class Life5 extends JApplet{ 
  public static void main(String s[]) { 
    JFrame frame = new JFrame(); 
    frame.setTitle("Game of Life"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JApplet applet = new Life5(); 
    applet.init(); 
    frame.getContentPane().add(applet); 
    frame.pack(); 
    frame.setVisible(true); 
  } 
   
  public void init() { 
    JPanel panel = new LifePanel(); 
    getContentPane().add(panel); 
 } 
} 
 
 
     
class LifePanel extends JPanel implements ActionListener,MouseListener{ 
   
 
// MouseListener methods 
public void mouseClicked(MouseEvent e){ 
  
 //System.exit(0); 
} 
public void mouseEntered(MouseEvent e){ 
} 
public void mouseExited(MouseEvent e){ 
} 
public void mousePressed(MouseEvent e){ 
} 
public void mouseReleased(MouseEvent e){ 
} 
 
 
  int n = 221; 
  boolean[][] cells1; 
  boolean[][] cells2; 
   
  public LifePanel() { 
    setPreferredSize(new Dimension(400, 400)); 
    setBackground(Color.white); 
    cells1 = new boolean[n][n]; 
    cells2 = new boolean[n][n]; 
    for (int i = 0; i < n; i++) { 
      for (int j = 0; j < n; j++) { 
        cells1[i][j] = false; 
        cells2[i][j] = false; 
      } 
    } 
         cells1[(n/2)][(n/2)] = true; 
    Timer timer = new Timer(1000, this); 
    timer.start(); 
  } 
 
  public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g2 = (Graphics2D)g; 
     
    g2.setColor(Color.lightGray); 
    int p = 0; 
    int c = 2; 
    int len = 443; 
    for (int i = 0; i <= n; i++) { 
      g2.drawLine(0, p, len, p); 
      g2.drawLine(p, 0, p, len); 
      p += 2; 
    } 
    g2.setColor(Color.black); 
    for (int i = 0; i < n; i++) { 
      for (int j = 0; j < n; j++) { 
        if (cells1[i][j]) { 
          int x = i*c; 
          int y = j*c; 
          g2.fillOval(x, y, c, c); 
        } 
      } 
    } 
  } 
   
  public void actionPerformed(ActionEvent e) { 
    boolean[][] cells = cells1; 
    for (int i = 0; i < n ; i++) { 
      for (int j = 0; j < n ; j++) { 
        cells2[i][j] = cells[i][j]; 
        int nb = neighbors(cells,i, j); 
                        if(cells2[i][j] == false && nb != 1) 
                        cells2[i][j] = true;     
                        else if( cells2[i][j] == true && (nb == 1 || nb ==  3)) 
                      cells2[i][j] = true; 
                   else{ cells2[i][j] = false;} 
                                 
         //System.out.println(nb);       
                   
      } 
 
                }                    
 
         cells1 = cells2; 
    cells2 = cells; 
    repaint(); 
  } 
   
 private int countNeighbor(boolean[][] cells,int x, int y) {  
        if (x<0||x>n-1) { return 0; }  
        if (y<0||y>n-1) { return 0; }  
        return cells[x][y] ? 1 : 0;  
}  
  
private int neighbors(boolean[][] cells,int x, int y) {  
        return countNeighbor(cells,x, y-1)   
                + countNeighbor(cells,x, y+1) 
                + countNeighbor(cells,x-1, y)   
                + countNeighbor(cells,x+1, y);  
}   
 
 
}

This post has been 


Was This Post Helpful? 0
  • +
  • -

#6 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: How to use MouseListener

Posted 05 April 2010 - 02:54 PM

Ok I read it and i am trying to do like the tutorial says but I can not make it work.Maybe I need to read more about that but I thought that when you help me i will analyse the code and I will understand it.

Thanks for reply.
Was This Post Helpful? 0
  • +
  • -

#7 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: How to use MouseListener

Posted 05 April 2010 - 02:54 PM

View PostDogstopper, on 05 April 2010 - 08:45 PM, said:

If you get frustrated, that's a good thing, and means that you are stretching your limits and learning.


Deffinately true, everyone goes through this stage, it makes you improve your problem solving skills.
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to use MouseListener

Posted 05 April 2010 - 02:58 PM

View Postdrilli, on 05 April 2010 - 12:54 PM, said:

Ok I read it and i am trying to do like the tutorial says but I can not make it work.Maybe I need to read more about that but I thought that when you help me i will analyse the code and I will understand it.

Thanks for reply.


It doesn't work that way. We don't just give you a solution so you can learn from it (i.e; turn the code in as your assignment and pass the course without learning or doing anything).

Show us what you've written (and by written I'm not saying what you've copied & pasted from the tutorials listed) and a good explanation of what you're not understanding (and any errors you're getting) and we will try to guide you through it.
Was This Post Helpful? 0
  • +
  • -

#9 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: How to use MouseListener

Posted 05 April 2010 - 02:59 PM

Ok Ok don`t make any other reply for that program because i will do it by myself.However it will take a lot of time to me.

Thanks
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: How to use MouseListener

Posted 05 April 2010 - 03:02 PM

*** Topics Merged *** Please don't double post!

Well, I am glad you are going for it! You will hopefully learn how they work and when you need help, come ask! I might even show you a few secret about listeners. But I will let you go now. Try to think positively and remember that frustration is good...in small amounts.
Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to use MouseListener

Posted 05 April 2010 - 03:03 PM

Don't worry about how much time it takes (well unless you've got an assignment due). You're not going to learn Java in a day or two, it takes time and there's no way around that.

And dont get down on yourself when you run into issues, that's how you're going to learn and expand yourself as a programmer. We didn't all start out star programmers, like I've been doing this for over 15 years professionally so it takes time.
Was This Post Helpful? 2
  • +
  • -

#12 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: How to use MouseListener

Posted 05 April 2010 - 03:12 PM

No no it`s not an assignment but I said "a lot of time" because I am a student and i need to learn the other subjects too, espacially math(it needs more time).
I also will tell you that the last choise for me is posting and looking for help here because i want to learn java really.
Was This Post Helpful? 0
  • +
  • -

#13 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to use MouseListener

Posted 05 April 2010 - 03:15 PM

You'll notice that as long as you put forth an effort and show that you're trying, instead of a Can I have teh codez for <insert project here> people here will bend over backwards to help. We have some awesome Java experts here who really know what they're talking about
Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: How to use MouseListener

Posted 05 April 2010 - 03:33 PM

@PsychoCoder: :D

@drilli: I agree that learning Java on your own is important. (I'm 15, completely self-taught, I understand). However, realize that posting here will only help to diversify and expand your knowledge and I suggest posting to ask for critique on a program or a better, faster way. Most of the time, we can help you make it better. That's a good learning experience and I advise you to take that under consideration.

However, you also make a good point about school...Personally, I am really bad with doing my homework, but it does need to be done, and remember when coding as a student that we cant spend all of our time coding as you pointed out.
Was This Post Helpful? 0
  • +
  • -

#15 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: How to use MouseListener

Posted 05 April 2010 - 03:47 PM

Wow... you were right.it was so easy but the problem is that I didn`t look in details about MouseListener.Thank you all for helping and for your advices.Maybe i was a little rude but I am sorry.
Now it is ok and this is my updated code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.applet.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

//import java.awt.event.*;


public class Life5 extends JApplet {
  public static void main(String s[]) {
    JFrame frame = new JFrame();
    frame.setTitle("Game of Life");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JApplet applet = new Life5();
    applet.init();
    frame.getContentPane().add(applet);
    frame.pack();
    frame.setVisible(true);
  }
  
  public void init() {
    JPanel panel = new LifePanel();
    getContentPane().add(panel);
   
	}
}


    
class LifePanel extends JPanel implements ActionListener,MouseListener{


// MouseListener methods
//public void mouseClicked(MouseEvent e){
 
 //System.exit(0);
//}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}


  int n = 221;
  boolean[][] cells1;
  boolean[][] cells2;
  
  public LifePanel() {
   addMouseListener(this);//
    setPreferredSize(new Dimension(400, 400));
    setBackground(Color.white);
    cells1 = new boolean[n][n];
    cells2 = new boolean[n][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cells1[i][j] = false;
        cells2[i][j] = false;
      }
    }
	 cells1[(n/2)][(n/2)] = true;
    Timer timer = new Timer(1000, this);
    timer.start();
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    
    g2.setColor(Color.lightGray);
    int p = 0;
    int c = 2;
    int len = 443;
    for (int i = 0; i <= n; i++) {
      g2.drawLine(0, p, len, p);
      g2.drawLine(p, 0, p, len);
      p += 2;
    }
    g2.setColor(Color.black);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (cells1[i][j]) {
          int x = i*c;
          int y = j*c;
          g2.fillOval(x, y, c, c);
        }
      }
    }
  }
  
  public void actionPerformed(ActionEvent e) {
    boolean[][] cells = cells1;
    for (int i = 0; i < n ; i++) {
      for (int j = 0; j < n ; j++) {
        cells2[i][j] = cells[i][j];
        int nb = neighbors(cells,i, j);
			if(cells2[i][j] == false && nb != 1)
			cells2[i][j] = true;	
			else if( cells2[i][j] == true && (nb == 1 || nb ==  3))
		      cells2[i][j] = true;
		   else{ cells2[i][j] = false;}
				
	 //System.out.println(nb);	
		  
      }

		}		    

	 cells1 = cells2;
    cells2 = cells;
    repaint();
  }
  
 private int countNeighbor(boolean[][] cells,int x, int y) { 
        if (x<0||x>n-1) { return 0; } 
        if (y<0||y>n-1) { return 0; } 
        return cells[x][y] ? 1 : 0; 
} 
 
private int neighbors(boolean[][] cells,int x, int y) { 
        return countNeighbor(cells,x, y-1)  
                + countNeighbor(cells,x, y+1)
                + countNeighbor(cells,x-1, y)  
                + countNeighbor(cells,x+1, y); 
}  

public void mouseClicked(MouseEvent e){
 
 System.exit(0);
}

}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2