添加图片上点击鼠标? Java小程序 [英] Add image on mouse click? Java applet

查看:233
本文介绍了添加图片上点击鼠标? Java小程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么会去和鼠标添加图像坐标中的鼠标点击时?我已经看过这个:对鼠标添加图像点击的JPanel

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

但我不明白,我试图在applet添加鼠标点击

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

和请不要说,学习一些基本的Java第一!并为我提供一个链接到某些Oracle文档,我不能得到这些东西的任何信息。

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



}
`

问题是,我不知道我应该代替NULL一起得到它的工作。

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

感谢

推荐答案

通过的绘画在AWT和Swing 执行定制绘画了解画中的AWT / Swing的是如何工作的。

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

然后,看看 2D图形了解更多详细信息,你如何能使用图形类油漆事情。

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.

移动

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

    }

}

这篇关于添加图片上点击鼠标? Java小程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆