调用 mousePressed 时未调用 mouseMoved
mouseMoved not called while mousePressed has been called
我的 mouseMoved
在 mousePressed
被调用时根本没有被调用,但是当 mousePressed
没有被调用时它被正常调用。如果我在按下鼠标按钮时移动鼠标 mouseMoved
不会被调用。
package src.game.main.gui_hud;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.SwingUtilities;
import src.game.main.Game;
public class Slider {
private Color lc,hc,fc;
private int x,y;
private int w,h;
private Runnable change;
private int lineY;
private double value = 100;
private volatile boolean canMove;
public Slider(Color bgColor,Color filledColor,Color handlerColor,Runnable onValueChange,int x,int y,int w,int h,int lineY) {
setLc(bgColor);
setHc(handlerColor);
setFc(filledColor);
change = onValueChange;
this.x = x;
this.y = y;
this.w = w;
this.h = w;
this.lineY = lineY;
}
public void render(Graphics gt) {
Graphics2D g = (Graphics2D) gt.create(x, y, w, h);
g.setColor(getLc());
g.fillRoundRect(10, y/2-lineY, w-10, lineY, 10, 10);
g.setColor(getFc());
g.fillRoundRect(10, y/2-lineY, (int) ((value*w)/100)-10, lineY, 10, 10);
g.setColor(getHc());
g.fillRoundRect((int)((value*w)/100)-6, y/2-20, 5, 30, 10, 10);
}
public void tick() {
value = Game.clamp(value, 0, 100);
System.out.println(canMove);
}
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
if (new RoundRectangle2D.Double(x+ ((int)((value*w)/100)-6), y + (y/2-20), 5, 30, 10, 10).contains(p)) {
canMove = SwingUtilities.isLeftMouseButton(e);
}
}
public void mouseReleased(MouseEvent e) {
Point p = e.getPoint();
canMove = false;
}
public void mouseMoved(MouseEvent e) {
System.out.println(e.getX());
Point p = e.getPoint();
if(canMove) System.out.println("LOL");
}
public Color getHc() {
return hc;
}
public Slider setHc(Color hc) {
this.hc = hc;
return this;
}
public Color getLc() {
return lc;
}
public Slider setLc(Color lc) {
this.lc = lc;
return this;
}
public int getX() {
return x;
}
public Slider setX(int x) {
this.x = x;
return this;
}
public int getY() {
return y;
}
public Slider setY(int y) {
this.y = y;
return this;
}
public int getW() {
return w;
}
public Slider setW(int w) {
this.w = w;
return this;
}
public int getH() {
return h;
}
public Slider setH(int h) {
this.h = h;
return this;
}
public double getValue() {
return value;
}
public Slider setValue(double v) {
this.value = v;
return this;
}
public Color getFc() {
return fc;
}
public Slider setFc(Color fc) {
this.fc = fc;
return this;
}
根据 mousePressed
处理程序中的代码,您正在尝试执行某种拖动矩形操作,但我不能确定。
mouseMoved
消息将不断发生,直到您收到 mousePressed
事件。收到 mousePressed
事件后,您将开始接收 mouseDragged
事件,直到收到 mouseReleased
事件。在那之后,您将再次开始接收 mouseMoved
事件。
这是为了区分仅移动鼠标和按下其中一个按钮进行拖动。
我的 mouseMoved
在 mousePressed
被调用时根本没有被调用,但是当 mousePressed
没有被调用时它被正常调用。如果我在按下鼠标按钮时移动鼠标 mouseMoved
不会被调用。
package src.game.main.gui_hud;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.SwingUtilities;
import src.game.main.Game;
public class Slider {
private Color lc,hc,fc;
private int x,y;
private int w,h;
private Runnable change;
private int lineY;
private double value = 100;
private volatile boolean canMove;
public Slider(Color bgColor,Color filledColor,Color handlerColor,Runnable onValueChange,int x,int y,int w,int h,int lineY) {
setLc(bgColor);
setHc(handlerColor);
setFc(filledColor);
change = onValueChange;
this.x = x;
this.y = y;
this.w = w;
this.h = w;
this.lineY = lineY;
}
public void render(Graphics gt) {
Graphics2D g = (Graphics2D) gt.create(x, y, w, h);
g.setColor(getLc());
g.fillRoundRect(10, y/2-lineY, w-10, lineY, 10, 10);
g.setColor(getFc());
g.fillRoundRect(10, y/2-lineY, (int) ((value*w)/100)-10, lineY, 10, 10);
g.setColor(getHc());
g.fillRoundRect((int)((value*w)/100)-6, y/2-20, 5, 30, 10, 10);
}
public void tick() {
value = Game.clamp(value, 0, 100);
System.out.println(canMove);
}
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
if (new RoundRectangle2D.Double(x+ ((int)((value*w)/100)-6), y + (y/2-20), 5, 30, 10, 10).contains(p)) {
canMove = SwingUtilities.isLeftMouseButton(e);
}
}
public void mouseReleased(MouseEvent e) {
Point p = e.getPoint();
canMove = false;
}
public void mouseMoved(MouseEvent e) {
System.out.println(e.getX());
Point p = e.getPoint();
if(canMove) System.out.println("LOL");
}
public Color getHc() {
return hc;
}
public Slider setHc(Color hc) {
this.hc = hc;
return this;
}
public Color getLc() {
return lc;
}
public Slider setLc(Color lc) {
this.lc = lc;
return this;
}
public int getX() {
return x;
}
public Slider setX(int x) {
this.x = x;
return this;
}
public int getY() {
return y;
}
public Slider setY(int y) {
this.y = y;
return this;
}
public int getW() {
return w;
}
public Slider setW(int w) {
this.w = w;
return this;
}
public int getH() {
return h;
}
public Slider setH(int h) {
this.h = h;
return this;
}
public double getValue() {
return value;
}
public Slider setValue(double v) {
this.value = v;
return this;
}
public Color getFc() {
return fc;
}
public Slider setFc(Color fc) {
this.fc = fc;
return this;
}
根据 mousePressed
处理程序中的代码,您正在尝试执行某种拖动矩形操作,但我不能确定。
mouseMoved
消息将不断发生,直到您收到 mousePressed
事件。收到 mousePressed
事件后,您将开始接收 mouseDragged
事件,直到收到 mouseReleased
事件。在那之后,您将再次开始接收 mouseMoved
事件。
这是为了区分仅移动鼠标和按下其中一个按钮进行拖动。