possible to add a MouseListener to a class ?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1462 Views - Last Post: 10 May 2012 - 08:41 AM Rate Topic: -----

#1 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

possible to add a MouseListener to a class ?

Posted 10 May 2012 - 04:44 AM

hey there im learning game programming in java and currently im working with mouse listeners, but i have one class which i use for drawing stuff to a jpanel called draw.java and then i have a main class that takes care of the JFrame containing the JPanel and so on, but i want to make a class for the mouse listener like Mouse.java but idk how i would go about adding a mouseListener to it :S ?

like if im going to my main method in draw.java ant type Mouse.addMouseListener(Mouse); that ain't working (i have tested different stuff to) so could anyone please explain me a bit ? xD

best regards theshowtekfan

Is This A Good Question/Topic? 0
  • +

Replies To: possible to add a MouseListener to a class ?

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,660
  • Joined: 20-September 08

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 05:28 AM

You add the MouseListener to whatever is producing the events, in your case, the JPanel presumably
Was This Post Helpful? 0
  • +
  • -

#3 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 05:40 AM

ahh i see :) i tried doing draw.addMouseListener(Mouse); in my jpanel class but that ain't working :/ so im kinda confused right there
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,660
  • Joined: 20-September 08

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 05:50 AM

You need to call addMouseListener using a parameter that's a reference to an object that is a MouseListener for it to work
Was This Post Helpful? 0
  • +
  • -

#5 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 06:09 AM

yea and then the object would be my mouse class right ? where i extend the MouseListener etc and write the methods for the mouseListener ?
Was This Post Helpful? 0
  • +
  • -

#6 oha055  Icon User is offline

  • D.I.C Head

Reputation: 46
  • View blog
  • Posts: 249
  • Joined: 02-February 09

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 06:32 AM

View Posttheshowtekfan, on 10 May 2012 - 03:09 PM, said:

yea and then the object would be my mouse class right ? where i extend the MouseListener etc and write the methods for the mouseListener ?


Yes, then you would have to do this:

public class Mouse implements MouseListener {

	@Override
	public void mouseClicked(MouseEvent arg0) {
           // Do stuff when mouse is clicked
	}

	/**
	 * ...
	 */

}



This is just one way of doing it, though.

EDIT:
You could also forget about the Mouse class and make the Draw class implement the MouseListener interface. This could simplify the situation :)

Make Draw implement MouseListener and then (if your Draw class extends JPanel):
addMouseListener(this);


This post has been edited by oha055: 10 May 2012 - 06:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 06:38 AM

if your class implements MouseListener it will be

jcomponent.addMousleListener(this);

and if your class extends JPanel

addMousleListener(this);
which is the equivalent of
this.addMousleListener(this);
Was This Post Helpful? 0
  • +
  • -

#8 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 06:43 AM

yea i see, but for learning purposes i am making a class for the mouse :P i want to get hang of OOP while working with different projects
Was This Post Helpful? 0
  • +
  • -

#9 oha055  Icon User is offline

  • D.I.C Head

Reputation: 46
  • View blog
  • Posts: 249
  • Joined: 02-February 09

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 06:45 AM

View Posttheshowtekfan, on 10 May 2012 - 03:43 PM, said:

yea i see, but for learning purposes i am making a class for the mouse :P i want to get hang of OOP while working with different projects

Fair enough :)
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 06:48 AM

So use oha055's suggestion in his post #6

This post has been edited by pbl: 10 May 2012 - 06:50 AM

Was This Post Helpful? 0
  • +
  • -

#11 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 06:58 AM

yea i know how to make the mouse class and everything lol :P its faily like KeyListeners but idk how to use the addMouseListener to listen on the mouse class :S hard to explain with my horrible english but i have a mouse class and need something like addMouseListener(Mouse); in my draw class
Was This Post Helpful? 0
  • +
  • -

#12 oha055  Icon User is offline

  • D.I.C Head

Reputation: 46
  • View blog
  • Posts: 249
  • Joined: 02-February 09

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 07:14 AM

In the constructor of your Mouse class:


addMouseListener(this);

EDIT:

yourJcomponent.addMouseListener(new Mouse());


This post has been edited by oha055: 10 May 2012 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

#13 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 08:16 AM

yea but i have my JPanel in the drawings class and the mouse listener in another class called Mouse and adding Draw.addMouseListener(this); in the Mouse class does not work
Was This Post Helpful? 0
  • +
  • -

#14 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,839
  • Joined: 05-April 11

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 08:20 AM

you do notice that your problem would be solved in a matter of seconds if you would just post your code, right?
Was This Post Helpful? 0
  • +
  • -

#15 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: possible to add a MouseListener to a class ?

Posted 10 May 2012 - 08:27 AM

yea lol xD im just to lazy to do so because i would have to change to my laptop when i posted the thread :P anyways here's the code

Main class
package Debug;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;

public abstract class Main {
  public static void main(String[] args) {
      JFrame f = new JFrame("Draw");
      Draw draw = new Draw();
      f.add(draw);
      draw.requestFocus();
      
      f.setSize(800, 600);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setTitle("MR.Hairy - Version.Alpha 0.1");
      f.setResizable(false);
      f.setVisible(true);
      
  }
}


Draw class:
package Debug;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

class Draw extends JPanel implements KeyListener {
    
    public static int Xpos = 200;
    public static int Ypos = 350;
    public static boolean fire;
    BufferedImage player = null;
    
    public Draw() {
        
        setFocusable(true);
        addKeyListener(this);
        
        if (Xpos >= 700) {
            Xpos = 700;
        }
        if (Xpos <= -7) {
            Xpos = -7;
        }
        if (Ypos >= 480) {
            Ypos = 480;
        }
        if (Ypos <= -7) {
            Ypos = -7;
        }
        
        try {
            player = ImageIO.read(new File("player.png"));
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
        
        int key = e.getKeyCode();
        
        if (key == e.VK_LEFT) {
            Xpos -= 5;
        }
        if (key == e.VK_RIGHT) {
            Xpos += 5;
        }
        if (key == e.VK_SPACE) {
            fire = true;
        }
        repaint();
        
    }
    
    @Override
    public void keyTyped(KeyEvent e) {
        
    }
    
    @Override
    public void keyReleased(KeyEvent e) {
        
    }
    
    @Override public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        
        Font arial = new Font("Arial",Font.BOLD,22);
        g2d.setFont(arial);
        
        g2d.drawImage(player, Xpos, Ypos, null, null);
        g2d.drawString("Xpos: " + Xpos, 50, 30);
        g2d.drawString("Ypos: " + Ypos, 50, 50);
        g2d.drawString("BYpos: " + shot.BYpos, 50, 70);
        if (fire == true) {
            g2d.fillRect(shot.bullet.y,shot.bullet.x, 5, 3);
        }
    }
    
}


Mouse class:
package Debug;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Mouse implements MouseListener {
    
    public Mouse() {
        addMouseListener(this);
    }
    
    public void mousePressed(MouseEvent e) {
        
    }
    
    public void mouseReleased(MouseEvent e) {
        
    }
    
    public void mouseExited(MouseEvent e) {
        
    }
    
    public void mouseEntered(MouseEvent e) {
        
    }
    
    public void mouseClicked(MouseEvent e) {
        
    }
    
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2