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

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

问题描述

当鼠标单击时,我将如何在鼠标坐标上添加图像?我看过这个:

import java.awt.Dimension;导入 java.awt.EventQueue;导入 java.awt.Graphics;导入 java.awt.Graphics2D;导入 java.awt.Point;导入 java.awt.event.MouseAdapter;导入 java.awt.event.MouseEvent;导入 java.awt.image.BufferedImage;导入 java.io.IOException;导入 javax.imageio.ImageIO;导入 javax.swing.JFrame;导入 javax.swing.JPanel;导入 javax.swing.UIManager;导入 javax.swing.UnsupportedLookAndFeelException;公共类 DrawImage {公共静态无效主(字符串 [] args){新的绘制图像();}公共 DrawImage() {EventQueue.invokeLater(new Runnable() {@覆盖公共无效运行(){尝试 {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {ex.printStackTrace();}JFrame frame = new JFrame("测试");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.add(new TestPane());框架.pack();frame.setLocationRelativeTo(null);frame.setVisible(true);}});}公共类 TestPane 扩展 JPanel {私有 BufferedImage 图像;私人点绘制点;公共测试窗格(){尝试 {image = ImageIO.read(getClass().getResource("/SmallPony.png"));addMouseListener(新鼠标适配器(){@覆盖public void mouseClicked(MouseEvent e) {drawPoint = new Point(e.getPoint());重绘();}});} catch (IOException ex) {ex.printStackTrace();}}@覆盖公共维度 getPreferredSize() {返回新维度(200, 200);}受保护的无效paintComponent(图形g){super.paintComponent(g);Graphics2D g2d = (Graphics2D) g.create();如果(绘制点!= null){g2d.drawImage(image, drawPoint.x, drawPoint.y, this);}g2d.dispose();}}}

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.

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天全站免登陆