I have loaded a jpg image in which I want to draw letters and circles, given a x,y coordinate.
I have been trying to figure out the paintIcon of the ImageIcon class
<Use a library to do just that. One that you might try is JMagick.
I have used Java Advanced Imaging library (http://java.sun.com/products/java-media/jai/forDevelopers/jaifaq.html), but you can also look at ImageJ (http://rsbweb.nih.gov/ij/index.html)
I imagen you could use this method to overlay the elements you need every time the image is drawn in the UI (this would happen numerous times as you are not drawing ON the image data its self) but may be suitable to your purposes (and advantageous if the overlay changes over time).
Something like:
new ImageIcon("someUrl.png"){
public void paintIcon(Component c, Graphics g, int x, int y) {
super(c, g, x, y);
g.translate(x, y);
g.drawOval(0, 0, 10, 10);
...
g.translate(-x, -y);
}
};
Having said that, mmyers' answer is much better if you want to modify the image data.
Manipulating images in Java can be achieved by using the Graphics or Graphics2D contexts.
Loading images such as JPEG and PNG can be performed by using the ImageIO class. The ImageIO.read
method takes in a File
to read in and returns a BufferedImage, which can be used to manipulate the image via its Graphics2D (or the Graphics, its superclass) context.
The Graphics2D
context can be used to perform many image drawing and manipulation tasks. For information and examples, the Trail: 2D Graphics of The Java Tutorials would be a very good start.
Following is a simplified example (untested) which will open a JPEG file, and draw some circles and lines (exceptions are ignored):
// Open a JPEG file, load into a BufferedImage.
BufferedImage img = ImageIO.read(new File("image.jpg"));
// Obtain the Graphics2D context associated with the BufferedImage.
Graphics2D g = img.createGraphics();
// Draw on the BufferedImage via the graphics context.
int x = 10;
int y = 10;
int width = 10;
int height = 10;
g.drawOval(x, y, width, height);
g.drawLine(0, 0, 50, 50);
// Clean up -- dispose the graphics context that was created.
g.dispose();
The above code will open an JPEG image, and draw an oval and a line. Once these operations are performed to manipulate the image, the BufferedImage
can be handled like any other Image
, as it is a subclass of Image
.
For example, by creating an ImageIcon using the BufferedImage
, one can embed the image into a JButton or JLabel:
JLabel l = new JLabel("Label with image", new ImageIcon(img));
JButton b = new JButton("Button with image", new ImageIcon(img));
The JLabel
and JButton
both have constructors which take in an ImageIcon
, so that can be an easy way to add an image to a Swing component.
The pure-Java way is to use ImageIO to load the image as a BufferedImage. Then you can call createGraphics() to get a Graphics2D
object; you can then draw whatever you want onto the image.
You can use an ImageIcon
embedded in a JLabel
to do the displaying, and you can add a MouseListener
and/or a MouseMotionListener
to the JLabel
if you're trying to allow the user to edit the image.