mouseReleased() not being called after a mouseDragged() event? - java

Should a mouseReleased() event be called when the mouse is released after a mouse is dragged?
I need mouseReleased() to be called to reset some variables used in mouseDragged(), but it never seems to be called.

Check the Oracle tutorial for a simple method to implement both listeners as PaĆ­lo asks in his comments
Basically (modified from the link above):
//where initialization occurs:
MyListener myListener = new MyListener();
addMouseListener(myListener);
addMouseMotionListener(myListener);
private class MyListener extends MouseInputAdapter {
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
// you may not need this method
}
public void mouseDragged(MouseEvent e) {
// do your code
}
public void mouseReleased(MouseEvent e) {
// reset your variables
}

Related

Drawing circles: separation of code into classes

I'm trying to draw circles with random colors and random diameters on mousePressed event but I'm having some issues when I tried to organize my code a little bit "separate my code into classes".
Controller class
public class Controller implements MouseListener {
private HashSet<Circle> circleSet = new HashSet<>();
private int r,g,b,d;
#Override
public void mousePressed(MouseEvent e) {
r = new Random().nextInt(256);
g = new Random().nextInt(256);
b = new Random().nextInt(256);
d = 10+new Random().nextInt(100);
circleSet.add(new Circle(e.getX()-d/2,e.getY()-d/2,d,d,new
Color(r,g,b),0,0));
}
public HashSet<Circle> getCircleSet() {
return circleSet;
}
#Override
public void mouseClicked(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
}
View class
public class View extends JPanel{
Controller controller;
HashSet<Circle> circleHashSet;
public View() {
repaint();
controller = new Controller();
circleHashSet = controller.getCircleSet();
this.addMouseListener(controller);
listen();
System.out.println(circleHashSet.size());
}
public void listen() {
new javax.swing.Timer(100, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
circleHashSet = controller.getCircleSet();
System.out.println(circleHashSet.size());
}
});
}
#Override
public void paintComponents(Graphics g) {
super.paintComponents(g);
for (Circle circle:this.circleHashSet) {
paintCircle(g,circle);
}
System.out.println(circleHashSet);
}
public void paintCircle(Graphics graphics, Circle circle) {
graphics.setColor(circle.color);
graphics.fillOval(circle.x,circle.getY(),circle.a,circle.b);
}
circleHashSet still empty even though I did:
//circleHashSet = controller.getCircleSet();
Circles are added into circleSet after pressing the mouse but I can't get them into the View class.
First of all, the problem is that you are calling the paintComponents(Graphics g) method instead of the paintComponent(Graphics g) method.
If you are done changing this, you should create a separate Circle POJO class for the circles, because the only avaliable Circle avaliable in the basic Java API is from the JavaFX package.
After setting the constructor for the circle class, you can add to the HashSet more easily. Like so: circleSet.add(new Circle(e.getX() - d / 2, e.getY() - d / 2, d, new Color(r, g, b)));
Another problem is that the Timer object you created is never used for anything. First, you should make a Timer object and after initializing it, call the start() method on it to start the checking of the circles.
put this line of code
circleHashSet = controller.getCircleSet();
within paintComponents() method. The reason is that you only get the value of HashSet when you start the program while the hashset is empty at this time. You should renew the value of circleHashSet after painting one with mouse.

Capturing mouse movement while moused pressed in Java

I'm trying to get the mouse position while pressing the mouse button but it doesn't work.
I'm extending the MouseAdapter and as stated at the Javadoc the mouseMove() is invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.
This is an example class I have created to show you my problem:
public class TestMouse extends MouseAdapter{
int x,y;
boolean pressed;
public void mousePressed(MouseEvent e){
pressed = true;
}
public void mouseReleased(MouseEvent e){
pressed = false;
}
/*
Invoked when the mouse is not pressed only.
*/
public void mouseMoved(MouseEvent e){
x = e.getX();
y = e.getY();
}
/*
I want something like that.
*/
public void mousePressedAndMoved(MouseEvent e){
....
}
}
That's the problem with MouseAdapter, since it's a abstract class and not an interface (MouseMotionListener is the one you need), it gives empty implementations for all the possible events just to avoid you from being forced to override them all, this also implies that you could miss some of these events if you don't read docs.
If you look carefully at documentation though, you will see that you have
public void mouseDragged(MouseEvent e)
that you can override to listen exactly to what you need.

MouseListener Not Responding

I can't get this MouseListener to work. Why? Nothing happens when I click th mouse
import acm.program.*;
import acm.graphics.*;
import java.awt.event.*;
/** Draws an oval whenever the user clicks the mouse */
public class DrawOvals extends GraphicsProgram implements MouseListener {
public void run() {
addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
GOval oval = new GOval(100,100,OVAL_SIZE, OVAL_SIZE);
oval.setFilled(true);
add(oval, e.getX(), e.getY());
System.out.println("Got here!");
}
/* Private constants */
private static final double OVAL_SIZE = 20;
/* implements the required methods for mouse listener*/
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
According to the link you provided in the comments in the OP, you have to call
addMouseListeners();
instead of
addMouseListener(this);
The description says:
"Use the GraphicsProgram itself as a listener for mouse events that occur within the embedded GCanvas. To do so, all the student has to do is define any listener methods to which the program needs to respond and then call addMouseListeners(), which registers the program as both a MouseListener and MouseMotionListener."
The other option is to use
GCanvas canvas = getGCanvas();
canvas.addMouseListener(this);

mouse pressed->drag->released. in java

Excuse me:
I just can't know how to link these successive operation?
Mouse pressed and then drag then release. If an user doesn't do this operation some action won't happen...
Should I add code as the is already pressed to distinguish that?
The constant MOUSE_MOVED doesn't work since Eclipse told me it doesn't know it although I find the parameter in mouse event api
I don't know what's going on... Please help!
Implement a MouseInputListener using a MouseInputAdapter subclass and handle the mousePressed, mouseDragged, and the mouseReleased events.
Take a look at this tutorial for examples.
Here is a simple class that encapsulates the drag detection:
public abstract static class MouseDragListener {
java.awt.Component component;
MouseEvent dragStart;
public MouseDragListener(java.awt.Component component) {
super();
this.component = component;
component.addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
dragStart = null;
}
public void mouseDragged(MouseEvent e) {
if (dragStart == null)
dragStart = e;
}
});
component.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (dragStart != null) {
dragReleased(dragStart, e);
}
}
});
}
then to use:
new MouseDragListener(center){
void dragReleased(MouseEvent start,MouseEvent end){
// do something ...
}
}

mouse listener on frame

Hi
i am trying to add a mouse listener to my frame to get the position of the mouse clicked and check if it is inside the circle, the problem is that it is not triggering
public class CircleDraw extends Frame implements MouseListener {
static int circles = 0;
private double color;
double mousex = 0;
double mousey = 0;
int score;
public void mouseClicked(MouseEvent evt)
{
mousex = evt.getX();
mousey = evt.getY();
}
public void mouseEntered (MouseEvent me) {}
public void mousePressed (MouseEvent me) {}
public void mouseReleased (MouseEvent me) {}
public void mouseExited (MouseEvent me) {}
public void paint(Graphics g) {
try {
this.addMouseListener(this);
while (circles < 20) {
color = 10*Math.random();
Shape circle = new Ellipse2D.Double(900*Math.random(),900*Math.random(), 50.0f, 50.0f);
Graphics2D ga = (Graphics2D)g;
ga.draw(circle);
if(color >2)
ga.setPaint(Color.green);
else
ga.setPaint(Color.BLACK);
ga.fill(circle);
if(circle.contains(mousex, mousey) && color > 2)
score ++;
else
if(circle.contains(mousex, mousey) && color < 2)
score--;
Thread.sleep(1000);
System.out.println(circles);
System.out.println(mousex);
System.out.println(mousey);
circles ++;
ga.setPaint(Color.white);
ga.fill(circle);
}
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
Frame frame = new CircleDraw();
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
frame.setSize(1000, 1000);
frame.setVisible(true);
}}
It is deadly to add your mouselistener in the paint() method, since this method is called very very often (with each repaint), and so many listeners are added (with each repaint).
You should add the listener to your content-panel and not to the JFrame itself. This will do it. You can do this in the constructor of your class:
public CircleDraw() {
this.getContentPane().addMouseListener(this);
}
This won't solve your problem completely I think, since you won't get your mouseclick while your paint-method is active. Your code-design (especially your while-loop) does not give any time to other events to fire. So the mouseclick-event will be handled after your 20 loops. You can check this by adding
public void mouseClicked(MouseEvent evt) {
mousex = evt.getX();
mousey = evt.getY();
System.out.println("X: "+mousex+"/ Y: "+mousey);
}
to your code. You have to run your GUI in a different thread (e.g. use SwingUtilities and a Runnable() therefor). I recommend you to get a good book on JAVA development. Or you can start with online-tutorials like this one.
IMHO you should not try to deal with awt, instead use SWING or SWT for GUI-design, since this is much more compfortable.
add the listener in the constructor, the paint is called repeatedly
Here are some of the problems I see with that source:
Adds the listener in paint()
Calls wait() within the paint() method.
Calls System.exit() within the paint() method (not strictly a problem, but very unusual).
Is poorly formatted and hard to understand
Calls deprecated methods.
Codes in AWT in the wrong millennium.

Categories

Resources