-2

EDIT: Okay, so I've managed to get it working now with the help of a friend. =)


I am currently creating a Java applet. The applet works by taking text input from the user and displaying it to the screen when they press return. This is the point up to which my program is working at the moment. I was wondering whether there was a way in which I could make it so the text input/String is drawn when the mouse is clicked, at the point of mouse click.

Many thanks in advance to anyone who can help me out with this. :)

  • 1
    *"How to draw a String to an applet screen at the point of mouse click?"* [What have you tried?](http://www.whathaveyoutried.com/) There are plenty of examples of drawing things on mouse click. One tip though, sort this out in a `JPanel` added to a `JFrame` first. Only once it is working should it be added to a `JApplet`. – Andrew Thompson Feb 03 '13 at 23:35
  • Okay, how would I do that? – ElementOfMagic Feb 04 '13 at 00:10
  • *"how would I do that?"* Search? Go to the `search` box on the upper right and type "[java] mouse" etc. But I see @MadProgrammer has supplied an answer. BTW - don't go looking for 'applet+string+mouse'. It is too specific, and anything that can be done in an applet can also be done in a frame (which is easier to develop and debug, BTW). – Andrew Thompson Feb 04 '13 at 00:17
  • I can't use a frame, this is something I have to do using an applet. – ElementOfMagic Feb 04 '13 at 02:09
  • ..Doesn't matter now, I've managed to do it using the applet. =) – ElementOfMagic Feb 04 '13 at 02:20
  • I actually have another problem now (still relating to the same program), which I've explained in my question. ^^ – ElementOfMagic Feb 04 '13 at 02:21
  • *"I have to do using an applet"* No, you don't. Or rather, once you have it working in a `JPanel` added to a frame, it is fairly simple to transfer it into an applet. But due to a number of reasons, applets are harder to work with during development, and more difficult for people to get set up and working in their IDE (so you'll typically get less help with them). On other mattes: For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 04 '13 at 02:28
  • Yes, I do because that's how I've been told to do it. – ElementOfMagic Feb 04 '13 at 02:40
  • Thanks, but I don't need luck. ;) I have a friend who helped me get it working. =) – ElementOfMagic Feb 06 '13 at 00:04
  • 1
    So add an answer. Otherwise this thread is of no value to the community. – Andrew Thompson Feb 06 '13 at 00:07

1 Answers1

3

I was wondering whether there was a way in which I could make it so the text input/String is drawn when the mouse is clicked, at the point of mouse click.

Answer: Yes...

Would it be rude of me to leave the answer as that...?

This is a relatively simple process, depending on what you want to achieve...

This example just uses Graphics#drawString to render text to a custom component. You could, equally, just at a label to the component at the specified point, but that's another can of worms.

enter image description here

public class TestDrawText {

    public static void main(String[] args) {
        new TestDrawText();
    }

    public TestDrawText() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Point textPoint;

        public TestPane() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    textPoint = e.getPoint();
                    repaint();
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 
            if (textPoint != null) {
                FontMetrics fm = g.getFontMetrics();
                g.drawString("You clicked at " + textPoint.x + "x" + textPoint.y, textPoint.x, textPoint.y + fm.getAscent());
            }
        }

    }

}

Check out

For more info.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366