[英]mouseMoved not called while mousePressed has been called
當mousePressed
被調用時,我的mouseMoved
根本不會被調用,但是在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
處理程序中的代碼,盡管我不確定,但是您正在嘗試進行某種拖動矩形。
在收到mousePressed
事件之前, mouseMoved
消息將一直發生。 收到mousePressed
事件后,您將開始接收mouseDragged
事件,直到收到mouseReleased
事件為止。 之后,您將再次開始接收mouseMoved
事件。
這旨在允許在僅移動鼠標和按住按鈕之一進行拖動之間進行區分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.