Java 我怎样才能展示X&;Y鼠标在图形中移动时的鼠标位置2D?

Java 我怎样才能展示X&;Y鼠标在图形中移动时的鼠标位置2D?,java,swing,graphics2d,awtrobot,Java,Swing,Graphics2d,Awtrobot,当鼠标使用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(Graphic

当鼠标使用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
保持不变时,颜色才会出现变化1)“我这里有3个问题”这里应该只有一个问题。将另外两个移动到单独的(标题适当的)问题线程。(投票以“太宽”结束)。2) 不需要使用
机器人
。而是在相关组件中添加一个
MouseMotionListener
。3) 自定义绘制
JComponent
(如
JPanel
)的正确方法是
paintComponent(Graphics)
。好的,我现在更改了它,你对Robot和MouseMotionListener的看法是正确的,但是,与此代码相当接近的话,我如何使用主屏幕内的自定义颜色,对于主方法之外的方法,例如绘制方法?有人告诉我return可以工作,没错,它应该如何与graphics2D一起工作,我是说string.join(“,10,10);?您正在尝试调用此方法吗?如果是这种情况,您希望传递一些
字符串
以及坐标。。。e、 例如,
final String-todraw=String.format(“%d,%d,%s”,mouseX,mouseY,color)
g2.抽绳(stringToDraw、mouseX、mouseY)没错,但我正在努力使用画法,我尝试使用颜色变量(robot.getPixelColor),但我不能,我说我需要创建一个局部变量,一种从循环中引入颜色的方法,而这是我不知道的部分
java.awt.Color[r=255,g=255,b=255]