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

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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)
 
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
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Members online

Top