MouseAdapter methods - why `mouseDragged`, `mouseMoved` and `mouseWheelMoved` included? - java

My goal was to simplify this code (all works fine):
package test;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JWindow;
public class A extends JWindow implements MouseListener, MouseMotionListener {
public A() {
addMouseListener(A.this);
addMouseMotionListener(A.this);
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
//This method is being used, working fine
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
#Override
public void mouseDragged(MouseEvent e) {
//This method is being used, working fine
}
#Override
public void mouseMoved(MouseEvent e) {
}
}
But if I decide to use MouseAdapter like this:
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
transferFocusBackward();
//This method is being used, working fine
}
#Override
public void mouseDragged(MouseEvent e) {
//This method is being used, not working anymore
}
});
mouseDragged receiving no events. But if I add MouseMotionListener like this:
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
//This method is being used, working fine
}
});
addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
//This method is being used, working fine
}
});
Then everything backs to normal. So my question is - what is the purpose of mouseDragged, mouseMoved and mouseWheelMoved methods in MouseAdapter class if they do not work?

OK, I think I understand now: in MouseAdapter class documentation it is said that "Create a listener object using the extended class and then register it with a component using the component's addMouseListener, addMouseMotionListener, addMouseWheelListener methods". So now it looks like:
private class MouseListeners extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
//This method is being used, working fine
}
#Override
public void mouseDragged(MouseEvent e) {
//This method is being used, working fine
}
}
and then:
MouseListeners listeners = new MouseListeners();
addMouseListener(listeners);
addMouseMotionListener(listeners);
Now everything is OK.

Related

Drawing lines on canvas using Mouse without Swing in java

My Quesition is similar to this question
Drawing lines with mouse on canvas : Java awt
My problem is that When the windows is minimized and maximized , the drawn lines are gone everytime
But my working is quite different because i used only awt components and without swing.
import java.awt.*;
import java.awt.event.*;
class Drawing extends WindowAdapter implements MouseMotionListener, MouseListener, ComponentListener {
Frame f;
Canvas c;
int X=400,Y=400;
int px=-1,py=-1;
int x,y;
public Drawing() {
f=new Frame("Drawing - Canvas");
f.addWindowListener(this);
f.addComponentListener(this);
f.setSize(X,Y);
c=new Canvas();
f.add(c);
c.addMouseMotionListener(this);
c.addMouseListener(this);
f.setVisible(true);
}
public void componentResized(ComponentEvent e) {}
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
int x,y;
x=e.getX();
y=e.getY();
Graphics g=c.getGraphics();
if(px!=-1) {
g.drawLine(px,py,x,y);
}
else {
g.drawLine(x,y,x,y);
}
px=x;
py=y;
}
public void mouseReleased(MouseEvent e) {
this.X=400; this.Y=400;
this.px=-1; this.py=-1;
}
public void windowClosing(WindowEvent e) {
f.dispose();
}
public static void main(String[] args) {
Drawing c=new Drawing();
}
}
Can someone help me out on these problems ?
getGraphics is NEVER the write way to perform custom paint.
Start by having a read through Painting in AWT and Swing and Performing Custom Painting for a better understanding how painting works and how you should work with it.
The simple answer is, you need to maintain a model of what has been painted, so that on each paint pass you can re-draw it.
For example Resize the panel without revalidation, Draw trail of circles with mouseDragged, Why is my line not drawing?

Java why is the key listener not working?

I do not know why this doesn't work. I have already read many posts, and added setFocusable but it just does not work.
public class Spiel {
public static void main(String[] args) {
Playground pg = new Playground();
pg.setLocation(0,0);
pg.setSize(1000,1000);
pg.setVisible(true);
pg.setFocusable(true);
}
}
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Playground extends JFrame implements KeyListener {
Playground(){
}
#Override
public void keyTyped(KeyEvent e) {
System.exit(0);
}
#Override
public void keyPressed(KeyEvent e) {
System.exit(0);
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
You only implemented the KeyListener but if you want it to actually work you still need to register it to your frame.
Playground(){
addKeyListener(this); // should do the trick
}
Otherwise your frame wouldn't know that it actually has to listen and call the methods when a key is pressed.

How do I simplify MouseListener so that I don't have all these unused methods?

Below I have the following code, so that when someone clicks on the "Close", the window will close. Below that is another exit button on the same menu bar, simply for redundancy (it'll be changed later to be something else, but the point stands as follows). My question is, is there any way to make this more simplistic? I mean there are four unused methods for every menu, and I'm going to need to do a few more. Any ideas on how to fix this?
closeFile.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
exit.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
Also, ActionListener wouldn't work for me, so I can't use that (don't believe I'm supposed to either).
Use a MouseAdapter and override the methods that you want.
closeFile.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
});
closeFile.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
//your code
}
});
Note: You dont have to write 'implements MouseListener' during class definition.
For more information, search for adapter classes, more specifically for MouseAdapter class.

How to set up mouse and keyboard input

Hi I'm trying to program a game using java. This is my first time using java, I am used to C#. In C# I would call Mouse.getLocation() and create a rect using the mouses location. Then by using if(Mouse.Left().toString() == "Pressed") I would then check if the mouse rect intersected with any other objects and act accordingly.
I've noticed in java you aren't provided with methods like these. So I was wondering, is the best way to approach mouse input simply to add listeners on all my clickable objects? I understand listeners and have a good idea how to use them but I was just wanting to check if there are more efficient ways to handle input or ways geared more towards what I'm most conformable with.
let your frame implement the MouseListener interface
implement all abstract methods, but in your case it is probably the mouseClicked event
identify if the button clicked is a left click, using the SwingUtilities class
if it is a left click, then set the x and y, which is the location of your click relative to the frame, not the screen.
public class MouseListeningObject extends JFrame implements MouseListener {
int x, y;
public MouseListeningObject () {
addMouseListener(this);
}
#Override
public void mouseClicked(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)){
x = e.getX();
y = e.getY();
}
}
#Override
public void mousePressed(MouseEvent e) {
// Some codes here
}
#Override
public void mouseReleased(MouseEvent e) {
// Some codes here
}
#Override
public void mouseEntered(MouseEvent e) {
// Some codes here
}
#Override
public void mouseExited(MouseEvent e) {
// Some codes here
}
}
You want your frame to implement MouseListener then add it in the constructor.
class MyFrame extends JFrame implements MouseListener {
MyFrame() {
addMouseListener(this);
}
#Override
public void mousePressed(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e) {}
}

Java applet - mouse and key listener

Is there a way to implement a KeyListener and MouseListener in the same applet? I already tried any ways I thought that would work and I tried Google. :\
my try:
C:\Users\Dan\Documents\DanJavaGen\tileGen.java:23: tileGen is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener
public class tileGen extends JApplet implements KeyListener, MouseListener {
^
1 error
You can certainly implement both KeyListener and MouseListener in the same applet :)
You must have the following methods in the tileGen class:
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e)b{
}
public void mouseExited(MouseEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
If you already have some of these methods implemented you can leave them out.
Gentle hint: Capitalize your class name as TileGen :) Lower-case class names are — by convention — used only for generated or internal code.

Categories