Java中,在JFrame中点击某一个位置以后,就在这个点添加一个图片或者imageicon,如何实现

如题所述

第1个回答  2011-04-21
你想实现画图的功能?
简单点的话,实现一个JPanel,覆盖paintComponent方法,在方法里面对要绘制的对象进行绘制..如你的需求,可以实现一个DrawItem类:
class DrawItem{
private Rectangle rect; 图像绘制的大小范围
private Image image; 源图像
..
public void draw(Graphics2D g){
g.drawImage(image, ...);// 具体看API
}
在Panel类中有一个DrawItem列表,而paintComponent方法调用:
for(DrawItem item: this.items){
item.draw(g);
}

此后监听鼠标点击事件,事件处理中执行:
MouseEvent e..
Rectangle imgBounds = image.getBounds();
DrawItem item = new DrawItem();
item.setImage(image);
item.setBounds(new Rectangle(e.getX(), e.getY(), imgBounds width, imgBounds.height);
this.addItem(item);
this.repaint();

大概的思路就是如此了~~本回答被提问者采纳
第2个回答  2011-04-20
开一个List存图象对象,再开个线程调用repaint()方法,在paint()方法里把List里每个对象取出来,再画出来就可以了。
第3个回答  2011-04-20
鼠标点击事件触发时,获取鼠标的坐标,并将该图片的坐标设置为该坐标
相似回答