0

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

Community
  • 1
  • 1
Roy Li
  • 3
  • 3
  • Start by having a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) to better understand how painting is done in AWT/Swing. Is there any reason why you are using `Applet`? Given the fact that most browsers now days are actively disabling Java/Applet's it seems to like a lot of work with little gain – MadProgrammer Sep 16 '15 at 00:15
  • Actually, I started trying to do this with swing lol. It didn't work out for me, but I think I'll try it again just to be careful – Roy Li Sep 16 '15 at 00:17
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Sep 16 '15 at 00:19
  • How would I add the MouseListener? Do i do frame.addMouseListener(this) or something? – Roy Li Sep 16 '15 at 00:28
  • And the only reason I used applets was because a bunch of errors involving public static void main(String[] args), but applets don't need that. Just to clear things up. – Roy Li Sep 16 '15 at 00:30

1 Answers1

1

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.

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

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • You will need to supply your own image, which should be stored within the classpath context of your code (ie within the Jar file), but how you achieve this will depend on what IDE you are using – MadProgrammer Sep 16 '15 at 00:38
  • Just trying to find the correct place to put it now – Roy Li Sep 16 '15 at 00:39
  • Thanks, this works perfectly! But, sadly I don't really understand this code since I'm a newb and all, do you think you can break it down for me? That would be very much appreciated:) You don't have to, it would just save me some time. Also, thank you for providing the video and all. I would +1, but I don't have that much reputation yet. – Roy Li Sep 16 '15 at 00:41
  • Start with the links that I've provided, they link back to a lot of important information you need to understand in order to work with the low level functionality of the painting routines and `Graphics` APIs – MadProgrammer Sep 16 '15 at 00:42
  • ok, now I'm trying to make it play a sound on click, but when I click, I get this: http://pastebin.com/BM38a83F code:http://pastebin.com/PkDmJku3 – Roy Li Sep 16 '15 at 01:08
  • The audio file can't be loaded because the reference is `null` – MadProgrammer Sep 16 '15 at 01:18
  • This line of code here? Clip clip = AudioSystem.getClip(); – Roy Li Sep 16 '15 at 19:34
  • Well, I assume you had to do something like `AudioInputStream ais = AudioSystem.getAudioInputStream(...);` passing in the `URL` reference of your audio clip, somewhere around here it would suggest that you're passing a `null` value because the resource you're trying to load can't be found – MadProgrammer Sep 16 '15 at 23:58