Text tool for a drawing applet

Hi!

I've made a drawing applet where I can draw different shapes like rectangles, oval, polygon etc on top of a JPanel.

Now I want a text tool. I want to be able to serialize an instance of a text object created from this tool so it can be sent through a network. So it can't be a component like TextField or TextArea.

I have made a simple Texttool which lets me type text and show it on the JPanel. BUT. When I press "Backspace" I want it to erase the last charachter in my text object. Instead it only paints out a bracket symbol "[ ]".

Is there any class I can subclass which allows me to modify text(using BackSpace) and that is Serializable aswell.

Otherwise, can you please give me idea of how I can do?

I've found this topic which does almost what I want but it paints out the component itself aswell as the font and size in the textfield can't be changed to for example size 68:

http://forum.java.sun.com/thread.jspa?messageID=9711036

Message was edited by:

CbbLe

[1039 byte] By [CbbLea] at [2007-11-27 7:06:10]
# 1

but it paints out the component itself

To prevent the textField from showing on the panel you will have to convert to Swing/lightweight components.

the font and size in the textfield can't be changed to for example size 68

In the second FreeText class of reply 6 in [url=http://forum.java.sun.com/thread.jspa?messageID=9711036]Drawing text on a canvas[/url] the font size in the textField is determined by this line

textField.setFont(getFont().deriveFont(18f));

in the addNotify method. This is where you can change it. Similarly, the panel font is set in the paint method with this line

g2.setFont(getFont().deriveFont(19f));

Here is a Swing version of FreeText with a borderless textField and the font size declared as a member variable. You should run this as–is to see what it does.

import java.awt.*;

import java.awt.event.*;

import java.awt.font.*;

import java.util.*;

import java.util.List;

import javax.swing.*;

public class FreeText extends JPanel implements ActionListener {

JTextField textField;

List<String> textList;

List<Point> locList;

Point loc;

int fontSize = 36; // Set font size here.

public FreeText() {

textField = new JTextField(16) {

public Dimension getPreferredSize() {

Font font = getFont();

FontRenderContext frc = new FontRenderContext(null, false, false);

LineMetrics lm = font.getLineMetrics("0", frc);

int h = (int)(lm.getAscent() + lm.getDescent());

return new Dimension(200, h);

}

};

textField.setFont(new Font("dialog", Font.PLAIN, fontSize));

textField.setBackground(UIManager.getColor("Panel.background"));

textField.setBorder(null);

textField.addActionListener(this);

textList = new ArrayList<String>();

locList = new ArrayList<Point>();

loc = new Point();

setLayout(null);

}

public void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setFont(textField.getFont());

for(int j = 0; j < textList.size(); j++) {

Point p = (Point)locList.get(j);

String s = (String)textList.get(j);

g2.drawString(s, p.x, p.y);

}

}

public void setLoc(Point p) {

loc = p;

add(textField);

Dimension d = textField.getPreferredSize();

//System.out.printf("textField prefSize = [%d, %d]%n", d.width, d.height);

textField.setBounds(p.x, p.y, d.width, d.height);

textField.requestFocusInWindow();

}

/** Press enter key while caret is in textField. */

public void actionPerformed(ActionEvent e) {

if(!textField.getText().equals("")) {

textList.add(textField.getText());

Font font = textField.getFont();

FontRenderContext frc = new FontRenderContext(null, false, false);

LineMetrics lm = font.getLineMetrics(textField.getText(), frc);

float height = lm.getHeight();

int h = (int)((textField.getHeight() - height)/2 + lm.getAscent());

loc.y += h+1;

locList.add(loc);

}

textField.setText("");

remove(textField);

repaint();

}

public static void main(String[] args) {

FreeText test = new FreeText();

test.addMouseListener(new TextSpotter(test));

JFrame f = new JFrame("click, type, press enter key");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.add(test);

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

class TextSpotter extends MouseAdapter {

FreeText freeText;

public TextSpotter(FreeText ft) {

freeText = ft;

}

public void mousePressed(MouseEvent e) {

freeText.setLoc(e.getPoint());

}

}

crwooda at 2007-7-12 18:57:27 > top of Java-index,Security,Cryptography...