1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: How to draw a String to an applet screen at the point of mouse click?

Discussion in 'Software Development' started by ElementOfMagic, Feb 3, 2013.

Thread Status:
Not open for further replies.
  1. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    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. :) (y)
     
  2. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    What I would do here is have a variable that stores the text that has been entered then listen for mouse clicks. When the mouse is clicked call the drawString method. Like this:

    Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ClickText extends Applet implements KeyListener, MouseListener{
    	
    	private Graphics g;
    	private String input = "";
    	public ClickText() {
    		
    	}
    	public void init() {
    		g = getGraphics();
    		addKeyListener(this);
    		addMouseListener(this);
    		this.setFocusable(true);
    	}
    	
    	@Override
    	public void keyTyped(KeyEvent e) {
    		input += e.getKeyChar();
    	}
    	@Override
    	public void mousePressed(MouseEvent arg0) {
    		g.drawString(input, arg0.getX(), arg0.getY());
    	}
    	@Override
    	public void mouseClicked(MouseEvent arg0) {
    	}
    	@Override
    	public void mouseEntered(MouseEvent arg0) {
    	}
    	@Override
    	public void mouseExited(MouseEvent arg0) {
    	}
    	@Override
    	public void mouseReleased(MouseEvent arg0) {
    	}
    	@Override
    	public void keyPressed(KeyEvent e) {
    	}
    	@Override
    	public void keyReleased(KeyEvent e) {
    	}
    }
    
    
    The code is hopefully self explanatory
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1088087

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice