Hey

Im having a hard trouble drawing in a Java window so if there is a plugin that shows the coordinates in a Java window when I hover my mouse over that spot it would make my coordinate easier.

Dani AI

Generated

Short answer: instead of hunting for a NetBeans plugin, add a tiny bit of code to the running Swing component. is right to point at mouse motion events—below are two compact, reliable patterns that are easy to drop into a JPanel: 1) use Swing tooltips for an instant hover readout, or 2) update a status JLabel (or draw an overlay) for a persistent coordinate display. Both work with nested components, scroll panes and transformed drawing if converted properly.

Tooltip (fast, minimal code):

public class CoordPanel extends JPanel {
    public CoordPanel() {
        setToolTipText(""); // enables Swing tooltip support for this component
        ToolTipManager.sharedInstance().setInitialDelay(0); // optional: show immediately
    }

    @Override
    public String getToolTipText(MouseEvent e) {
        return String.format("x=%d y=%d", e.getX(), e.getY());
    }
}

Status bar / overlay (more control, handles transforms):

JLabel status = new JLabel("x=0 y=0");
MyDrawingPanel drawing = new MyDrawingPanel(); // custom panel that draws your content

drawing.addMouseMotionListener(new MouseMotionAdapter() {
    @Override
    public void mouseMoved(MouseEvent e) {
        Point p = SwingUtilities.convertPoint(drawing, e.getPoint(), drawing);
        // if drawing uses an AffineTransform 'tx' (scale/translate), invert it:
        try {
            Point2D model = tx.createInverse().transform(p, null);
            status.setText(String.format("x=%.1f y=%.1f", model.getX(), model.getY()));
        } catch (NoninvertibleTransformException ex) {
            status.setText(String.format("x=%d y=%d", p.x, p.y));
        }
    }
});

Practical notes: mouse events already run on the EDT so updating a JLabel is safe; avoid repainting the entire canvas on every mouse move—update only the small area or the status label; use SwingUtilities.convertPoint when coordinates must be expressed in another component’s coordinate system (for JScrollPane, nested panels, etc.). For marking points persistently, capture clicks (mousePressed) and store the coordinates for repaint. These approaches keep things simple and debuggable compared with searching for an IDE plugin.

Recommended Answers

All 2 Replies

you can implement a mouse motion listener:

http://download.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html

and in on the mouse movement method track x,y to a variable to use in your paint components.

you can implement a mouse listener to detect clicks

http://download.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

on a click write the current x,y as motion has updated it to writableX and writabley, then call repaint(), and then in your paint method use DrawString to write the cordinates of that click out somewhere in window.

if your drawing on a panel, make your panel class implement both listners. use the add method to add both listeners, and then you just need to copy in the methods of each listener ( with no code in at first). then add the code as i outlined, writing the x,y in motion and writing the writable x,y in click and repaint. You will get error methods if you use the implement and add but fail to copy in all methods. there is maybe 4 or 5 for each.

Mike

You can actually just use a motion listener and on each call to change in motion, mouse dragged in think, assign x,y and repaint and use drawstring in paint to print where the mouse is hovering at the moment, at a given spot like 50,50 on screen.

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.