How to draw a vertical and horizontal line of the mouse position?



Hi, from mouseMove I wanna use the mouseEvent to draw on my window component the position of my mouse, but I don’t have a Graphics context. How to do ?


In all modern UI frameworks, the drawing is not done directly, but via a model. That is, because the OS needs to be able to issue a paint() call at any time (e.g. because the window was minimised, or occluded by something dragged over it or whatever else reason).

You will have to create a model of the thing you created by mouse moving, store that in a member variable, and write the paint() method, so it uses this information. You might have to trigger the painting by calling repaint() in your mouse event.

If you want to google about that topic, it is called “Model-View-Controller pattern”.

Hope that helps


Something like :

class MyComponent : public Component
	MyComponent() {}
	void paint(Graphics& g) override
		g.drawLine(0.0f, m_mousepos.getY(), getWidth(), m_mousepos.getY());
		g.drawLine(m_mousepos.getX(), 0.0f, m_mousepos.getX(), getHeight());
	void mouseMove(const MouseEvent& ev) override
		m_mousepos = ev.position;
	Point<float> m_mousepos;


Oh thank you, why didn’t I think of that, grrr! Sometimes the simplest things block us!


Thank you Daniel too,
I understood the MVC model well, I’m learning to see its scope in Juce:-) I thought it was possible to add a drawing to the context graphics outside of paint. The solution given by Xenakios is obvious once seen, I should have found it…