单击鼠标添加图像? Java小程序

时间:2015-09-16 00:10:21

标签: java swing applet

当鼠标点击时,如何在鼠标坐标上添加图像?我看过这个:Adding Images on Mouse Click to JPanel

但我不理解它,并试图在applet中点击鼠标添加它

请不要说,“首先学习一些基本的java!并为我提供一些oracle文档的链接”,我无法从这些内容中获取任何信息。

代码:

> `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”以使其工作

由于

1 个答案:

答案 0 :(得分:1)

首先看一下Painting in AWT and SwingPerforming Custom Painting,了解绘画在AWT / Swing中的运作方式。

然后,请查看2D Graphics,了解有关如何使用Graphics类绘制内容的更多详细信息。

这是一个非常基本的示例,可以加载单个图像,每次单击面板时,都会将其移动到该点。

Move

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

    }

}