Swing MousePosition in einem JPanel abfragen

Lexi

Bekanntes Mitglied
Ich habe ein JPanel welches eine vertikale Linie in Höhe der X-Koordinate des Mauszeigers und eine horizontale Linie in Höhe der Y-Koordinate des Mauszeigers zeichnet. Immer wenn die Maus bewegt wird, soll repaint() aufgerufen und die Linien an die entsprechende neue Stelle gezeichnet werden.
Zur Veranschaulichung habe ich ein kleinen Screenshot angefügt.

Zum Code:

Meine Klasse erbt von JPanel und bekommt einen MouseAdapter per
Code:
addMouseListener(...)
zugefügt. Dieser Adapter überschreibt
Code:
public void mouseMoved(MouseEvent event)
und führt in dieser Methode jedesmal ein
Code:
repaint()
aus.
Code:
paintComponents
ruft
Code:
getMousePosition()
auf und zeichnet mit diesen Informationen die beiden Linien.

Leider wird mir nur eine Fehlermeldung ausgegeben, die sich auf folgende Zeilen bezieht.
Java:
Point mousePos = getMousePosition();
		g.drawLine(0,
				mousePos.y, // Hier wird NPE geworfen
				this.getWidth(),
				mousePos.y);

Hier die Exception:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at de.lexi.std.view.ShootingGround.paintComponent(ShootingGround.java:41)
	at javax.swing.JComponent.paint(JComponent.java:1029)
	at javax.swing.JComponent.paintChildren(JComponent.java:866)
	at javax.swing.JComponent.paint(JComponent.java:1038)
	at javax.swing.JComponent.paintChildren(JComponent.java:866)
	at javax.swing.JComponent.paint(JComponent.java:1038)
	at javax.swing.JLayeredPane.paint(JLayeredPane.java:581)
	at javax.swing.JComponent.paintChildren(JComponent.java:866)
	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5145)
	at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:302)
	at javax.swing.RepaintManager.paint(RepaintManager.java:1145)
	at javax.swing.JComponent.paint(JComponent.java:1015)
	at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
	at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
	at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
	at java.awt.Container.paint(Container.java:1844)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:751)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:696)
	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:676)
	at javax.swing.RepaintManager.access$700(RepaintManager.java:57)
	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1550)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:602)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
 

Anhänge

  • CrossHair.png
    CrossHair.png
    3,8 KB · Aufrufe: 40
Java:
/**
     * Returns the position of the mouse pointer in this <code>Container</code>'s
     * coordinate space if the <code>Container</code> is under the mouse pointer,
     * otherwise returns <code>null</code>.
     * This method is similar to {@link Component#getMousePosition()} with the exception
     * that it can take the <code>Container</code>'s children into account.
     * If <code>allowChildren</code> is <code>false</code>, this method will return
     * a non-null value only if the mouse pointer is above the <code>Container</code>
     * directly, not above the part obscured by children.
     * If <code>allowChildren</code> is <code>true</code>, this method returns
     * a non-null value if the mouse pointer is above <code>Container</code> or any
     * of its descendants.

if(mousePos != null{
...
}

mal testen 🙂
 
Java:
int xPos=0, yPos=0;

public void mouseClicked(MouseEvent e)
{
    xPos = e.getX();
    yPos = e.getY();
    this.repaint();
}

public void paint(Graphics g)
{
  g.drawLine(0, 0, xPos, yPos);
}
 
Java:
int xPos=0, yPos=0;

public void mouseClicked(MouseEvent e)
{
    xPos = e.getX();
    yPos = e.getY();
    this.repaint();
}

public void paint(Graphics g)
{
  g.drawLine(0, 0, xPos, yPos);
}

Er will ja gerade nicht klicken...so wie ich ihn verstandne habe, will er wohl so etwas:
Java:
	JFrame frame = new JFrame();
	final JPanel panel = new JPanel() {
	    @Override
	    protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Point mousePos = getMousePosition();
		if (mousePos != null) {
		    g.drawLine(0, mousePos.y, this.getWidth(), mousePos.y);
		    g.drawLine(mousePos.x,0,mousePos.x,this.getHeight());
		}
	    }

	};
	panel.setPreferredSize(new Dimension(400, 400));
	panel.addMouseMotionListener(new MouseMotionListener() {

	    @Override
	    public void mouseMoved(MouseEvent e) {
		panel.repaint();
	    }

	    @Override
	    public void mouseDragged(MouseEvent e) {
	    }
	});
	frame.add(panel);
	frame.pack();
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setLocationRelativeTo(null);
	frame.setVisible(true);
 
eRaaaa hat recht, ich wollte das ganze über MoveEvents machen. Wenn ich vorher auf null überprüfe funktioniert auch alles wie gewollt. Die Variante mit den beiden int Instanzen funktioniert selbstverständlich genauso gut.
Danke sehr.
 

Zurück
Oben