Add image in a specific point

Hi!I'm new in swing... How can i load an image from a file and next click in my internalFrame (already created) and display the image in the point where i click?Thank you for the time.
[206 byte] By [addircta] at [2007-10-3 10:48:45]
# 1

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.

TimRyanNZa at 2007-7-15 6:13:31 > top of Java-index,Desktop,Core GUI APIs...
# 2
I can click and obtain the correct coordinates, I just need one way to load and display the image in the exacly place... Do you remember one post where I can take that information?I'll continue looking for... ty
addircta at 2007-7-15 6:13:31 > top of Java-index,Desktop,Core GUI APIs...
# 3

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.

TimRyanNZa at 2007-7-15 6:13:31 > top of Java-index,Desktop,Core GUI APIs...
# 4
This one... http://forum.java.sun.com/thread.jspa?threadID=787864&messageID=4476634#4476634gives some information about how to override paintComponent()
TimRyanNZa at 2007-7-15 6:13:31 > top of Java-index,Desktop,Core GUI APIs...
# 5

> 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.

TimRyanNZa at 2007-7-15 6:13:31 > top of Java-index,Desktop,Core GUI APIs...
# 6

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());

}

};

}

crwooda at 2007-7-15 6:13:31 > top of Java-index,Desktop,Core GUI APIs...