Text Tool

Text Tool

[code=’java’]/*******************************************************
* MYCPLUS Sample Code – https://www.mycplus.com *
* *
* This code is made available as a service to our *
* visitors and is provided strictly for the *
* purpose of illustration. *
* *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/

import java.awt.*;
import java.awt.event.*;

class TextTool implements KeyEventTool {

public void mousePressed(Point p, ScribbleCanvas canvas) {
canvas.mouseButtonDown = true;
canvas.x = p.x;
canvas.y = p.y;
offscreen = canvas.getOffScreenGraphics();
offscreen.setFont(font);
text = new StringBuffer();
}

public void mouseReleased(Point p, ScribbleCanvas canvas) {
canvas.mouseButtonDown = false;
}

public void mouseDragged(Point p, ScribbleCanvas canvas) {}

public void keyPressed(char c, ScribbleCanvas canvas) {
text.append(c);
offscreen.drawString(text.toString(), canvas.x, canvas.y);
canvas.repaint(canvas.x, canvas.y – ascent – 1,
fm.stringWidth(text.toString()) + 1,
ascent + descent + 2);
}

protected StringBuffer text;
protected Font font = new Font(“Helvetica”, Font.BOLD, 24);
protected FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
protected int ascent = fm.getAscent();
protected int descent = fm.getDescent();
protected Graphics offscreen;

}
[/code]

Tags: Java
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post