There are many posts related to loading images - do a search on this forum or Google for that.
There are also many posts about overriding paintComponent() to draw images on JPanels etc, seach on this forum or Google for that.
There are also many posts about MouseListeners and mouse clicks and how to process them, seach on this forum or Google for that.
This one (a whole 7 posts ago) is doing something similar...
http://forum.java.sun.com/thread.jspa?threadID=789169&tstart=0
You can't just "load the image at a particular spot", you have remember where the click was and draw the image at that spot in your paintComponent() method.
> This one (a whole 7 posts ago) is doing something
> similar...
>
> http://forum.java.sun.com/thread.jspa?threadID=789169&
> tstart=0
>
> You can't just "load the image at a particular spot",
> you have remember where the click was and draw the
> image at that spot in your paintComponent() method.
Sorry - that one gives details about using a JLabel rather than overriding paintComponent() - but that may well be the best solution for you anyway.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class LocateImage extends JPanel {
BufferedImage image;
Point loc;
public LocateImage(BufferedImage image) {
this.image = image;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(loc == null) {
loc = new Point();
locateImage(getWidth()/2, getHeight()/2);
}
g.drawImage(image, loc.x, loc.y, this);
}
private void locateImage(int cx, int cy) {
int x = cx - image.getWidth()/2;
int y = cy - image.getHeight()/2;
loc.setLocation(x, y);
repaint();
}
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File("images/cougar.jpg"));
LocateImage test = new LocateImage(image);
test.addMouseListener(test.locator);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
private MouseListener locator = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
locateImage(e.getX(), e.getY());
}
};
}