... in an image and do some calculation on their [x y] coordinates.
My first idea was to use an image as the background of a JPanel and then register the points, but I'm not sure there will be a way to mark these on the JPanel. There is also the Drawing library, which I'm unfamiliar with, but I'm not sure if I can combine these with Swing.
Can you name me the packages/classes I can use in order to do the task? References of code that already does it are also welcome.
Thank you!
The Background Panel gives a couple of suggestion on how to display a background image depending on your requirements.
Custom Painting Approaches then gives some ideas of how to do custom painting if you need to add custom points to the image.
The issue here is three-fold:
One way to accomplish the above points would be to subclass a
JPanel
and provide those functionalities.1. Display a background image in a panel.
Firstly, as a
JPanel
does not have a way of displaying a background image by default, there must be a way to hold an image in theJPanel
, and then draw that on the panel itself, which can be performed via thepaintComponent
method.One way to accomplish this is to have a field in the class which holds on to an
Image
to draw:The
Graphics
object inpaintComponent
is associated with theMyPanel
and can be used to perform graphics operations.2. Finding the point at which the mouse was clicked.
Secondly, in order to retrieve the point at which the mouse was clicked, one could assign a
MouseListener
to theMyPanel
. In the following example, an anonymous inner class extending theMouseAdapter
is used to minimize writing extra code:Processing that needs to be performed when the mouse is clicked can be included in the
mouseClicked
method.3. How to draw a point on the panel.
Thirdly, in order to find one point at which the mouse was clicked, one can obtain it from the
MouseEvent
object that was passed in from themouseClicked
method:Although the above code is not tested, it should be a starting point.
For example, if multiple points needs to be drawing, perhaps having a
List<Point>
to hold the points, and drawing eachPoint
in thepaintComponents
method could be done.If additional processing needs to be performed when the mouse is clicked, additional code can be added to the
mouseClicked
method.Additional resources:
Thank you to zedoo for pointing out in the comments that making a call to
super.paintComponent
should be performed when overriding thepaintComponent
method.Subclass a JPanel and override the method paintComponent:
In the method you can use methods of the Graphics object passed to it. This method will be invoked every time there is a need to redraw the panel, so you need to store your points in an array and then read and draw each of them in your paintComponent.
You may also find this useful, in case you want a heavyweight component (AWT), this tutorial explains how to extend the Canvas class to draw stuff.