当鼠标使用 Graphics2D 移动时,如何显示 X 和 Y 鼠标位置?
How could I show X & Y mouse position while the mouse is moving with Graphics2D?
当鼠标在 Graphics2D 中移动时,如何显示 X 和 Y 鼠标位置?
我想在鼠标移动时显示坐标,但我可以用 System.out.println
来实现,我想用 drawString.join("", 10, 5)
。
那我怎么才能达到那个水平呢?
*这是我做的
public class Bell2 extends JPanel {
static JFrame frame=new JFrame();
public Bell() {
}
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.yellow);
//Here's where I struggle
g2.drawString.join ("mouseX, mouseY, C");
}
public static void main(String[] args) {
frame.setSize(500,300);
frame.setLocation(300,200);
frame.setVisible(true);
frame.setBackground(Color.black);
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Color c = robot.getPixelColor(456,141);
double mouseY=1.0;
double mouseX=1.0;
while(mouseX !=0 || mouseY !=0) {
mouseX = MouseInfo.getPointerInfo().getLocation().getX();
mouseY = MouseInfo.getPointerInfo().getLocation().getY();
System.out.println("x: "+mouseX+" y: "+mouseY+" c: "+c);
}
}
}
不确定这是否正是您要查找的内容,但是,或者,如果您想与原始示例保持相当接近的话,您可以考虑做这样的事情:
public static void main(String[] args) {
frame.setSize(500,300);
frame.setLocation(300,200);
frame.setVisible(true);
frame.setBackground(Color.black);
try {
final Robot robot = new Robot();
handleMouse(robot);
} catch (final AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void handleMouse(final Robot robot) {
int mouseX = 1;
int mouseY = 1;
while (mouseX !=0 || mouseY !=0) {
final Point mouseLocation = MouseInfo.getPointerInfo().getLocation();
mouseX = mouseLocation.x;
mouseY = mouseLocation.y;
final Color currentColor = robot.getPixelColor(mouseX, mouseY);
System.out.println(String.format("x: %d, y: %d, c: %s", mouseX, mouseY, currentColor));
}
}
请注意,currentColor
每次 都会更新 mouseX
和 mouseY
;在您的原始代码段中并非如此。
如果您正在查看终端上的输出,则需要注意另一件事 - 只有当 mouseX
和 mouseY
保持 <= 255 时,颜色才会发生变化;超出该值,您可能只会看到此输出:
java.awt.Color[r=255,g=255,b=255]
当鼠标在 Graphics2D 中移动时,如何显示 X 和 Y 鼠标位置?
我想在鼠标移动时显示坐标,但我可以用 System.out.println
来实现,我想用 drawString.join("", 10, 5)
。
那我怎么才能达到那个水平呢?
*这是我做的
public class Bell2 extends JPanel {
static JFrame frame=new JFrame();
public Bell() {
}
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.yellow);
//Here's where I struggle
g2.drawString.join ("mouseX, mouseY, C");
}
public static void main(String[] args) {
frame.setSize(500,300);
frame.setLocation(300,200);
frame.setVisible(true);
frame.setBackground(Color.black);
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Color c = robot.getPixelColor(456,141);
double mouseY=1.0;
double mouseX=1.0;
while(mouseX !=0 || mouseY !=0) {
mouseX = MouseInfo.getPointerInfo().getLocation().getX();
mouseY = MouseInfo.getPointerInfo().getLocation().getY();
System.out.println("x: "+mouseX+" y: "+mouseY+" c: "+c);
}
}
}
不确定这是否正是您要查找的内容,但是,或者,如果您想与原始示例保持相当接近的话,您可以考虑做这样的事情:
public static void main(String[] args) {
frame.setSize(500,300);
frame.setLocation(300,200);
frame.setVisible(true);
frame.setBackground(Color.black);
try {
final Robot robot = new Robot();
handleMouse(robot);
} catch (final AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void handleMouse(final Robot robot) {
int mouseX = 1;
int mouseY = 1;
while (mouseX !=0 || mouseY !=0) {
final Point mouseLocation = MouseInfo.getPointerInfo().getLocation();
mouseX = mouseLocation.x;
mouseY = mouseLocation.y;
final Color currentColor = robot.getPixelColor(mouseX, mouseY);
System.out.println(String.format("x: %d, y: %d, c: %s", mouseX, mouseY, currentColor));
}
}
请注意,currentColor
每次 都会更新 mouseX
和 mouseY
;在您的原始代码段中并非如此。
如果您正在查看终端上的输出,则需要注意另一件事 - 只有当 mouseX
和 mouseY
保持 <= 255 时,颜色才会发生变化;超出该值,您可能只会看到此输出:
java.awt.Color[r=255,g=255,b=255]