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
17 Replies - 841 Views - Last Post: 10 May 2012 - 08:41 AM
Replies To: possible to add a MouseListener to a class ?
#2
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
#3
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
#4
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
#5
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 ?
#6
Re: possible to add a MouseListener to a class ?
Posted 10 May 2012 - 06:32 AM
theshowtekfan, 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
#7
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);
jcomponent.addMousleListener(this);
and if your class extends JPanel
addMousleListener(this);
which is the equivalent of
this.addMousleListener(this);
#8
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
i want to get hang of OOP while working with different projects
#9
Re: possible to add a MouseListener to a class ?
Posted 10 May 2012 - 06:45 AM
#10
Re: possible to add a MouseListener to a class ?
Posted 10 May 2012 - 06:48 AM
#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
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
#12
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:
EDIT:
yourJcomponent.addMouseListener(new Mouse());
This post has been edited by oha055: 10 May 2012 - 07:22 AM
#13
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
#14
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?
#15
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
anyways here's the code
Main class
Draw class:
Mouse class:
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) {
}
}
|
|

New Topic/Question
Reply



MultiQuote





|