java把图片放到鼠标上,添加图片上点击鼠标? Java小程序

How would I go and add an image on the mouse coordinates when the mouse clicks? I have looked at this :Adding Images on Mouse Click to JPanel

But I don't understand it and am trying to add it on mouse click in an applet

And please don't say, "Learn some basic java first! and provide me with a link to some oracle docs", I just can't get any info from those things.

Code:

> `import java.applet.Applet;

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.image.BufferedImage;

import java.io.IOException;

`import java.net.URL;

import javax.imageio.ImageIO;

public class SHR extends Applet implements MouseListener{

int a;

int b;

@Override

public void mouseClicked(MouseEvent e) {

a = e.getX();

b = e.getY();

paint(null, a, b);/this is the part i am having trouble with

}

@Override

public void mouseEntered(MouseEvent arg0) {

}

@Override

public void mouseExited(MouseEvent arg0) {

}

@Override

public void mousePressed(MouseEvent arg0) {

}

@Override

public void mouseReleased(MouseEvent arg0) {

}

public void paint(Graphics g, int x, int y){

BufferedImage photo = null;

try

{

URL u = new URL(getCodeBase(),"SilverHandRecruit.png");

photo = ImageIO.read(u);

}

catch (IOException e)

{

g.drawString("Problem reading the file", 100, 100);

}

g.drawImage(photo,x, y, 10, 30, null);

}

}

`

The problem is, I don't know what I am supposed to replace "null" with to get it to work

Thanks

解决方案

Start by taking a look at Painting in AWT and Swing and Performing Custom Painting to understand how painting works in AWT/Swing.

Then, take a look at 2D Graphics for more details about how you can use the Graphics class to paint things with.

This is a really basic example which loads a single image and every time you click on the panel, moves it to that point.

QB7Ws.gif

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.image.BufferedImage;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

public class DrawImage {

public static void main(String[] args) {

new DrawImage();

}

public DrawImage() {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

ex.printStackTrace();

}

JFrame frame = new JFrame("Testing");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new TestPane());

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

public class TestPane extends JPanel {

private BufferedImage image;

private Point drawPoint;

public TestPane() {

try {

image = ImageIO.read(getClass().getResource("/SmallPony.png"));

addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

drawPoint = new Point(e.getPoint());

repaint();

}

});

} catch (IOException ex) {

ex.printStackTrace();

}

}

@Override

public Dimension getPreferredSize() {

return new Dimension(200, 200);

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g.create();

if (drawPoint != null) {

g2d.drawImage(image, drawPoint.x, drawPoint.y, this);

}

g2d.dispose();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值