Java Drawing Test Codes Text Editor using the Input Method Framework
2010.12.01

Language

Last update: 2013.06.26

Topics

Mouse hit test for a closed shape
2010.08.26


Mouse hit test for an unclosed shape
2010.08.26


Intersection between two arbitrary curves
2010.09.01


Normal lines to a arbitrary curve
2010.09.01


Common normal lines between two shapes
2016.10.20


Text Editor using the Input Method Framework
2010.12.01


Component Library
2012.09.16


Costom Color Chooser
2012.9.23



1. Source code download

=> TextBox.zip


2. Test items and method

The test on this page is completely different from those of the javax.swing.JTextArea, javax.swing.JTextPane or javax.swing.EditorPane.
First of all, the Swing text editors are not suitable to a drawing tool, because the text in those text editors can't be zoom in nor out at an arbitrary scale.
The text editor described on this page is configured by using the modules of the Input Method framework and the Java 2D Graphics. The text in this text editor is displayed mainly by the java.awt.Graphics2D and the the java.awt.font.TextLayout. So the text can be zoom in or out at an arbitrary scale shown in the Figure 2.
The text editor on this page has the capability of inputting a text, cut/copy and paste of a text and setting attributes to a text.
:
• Input Method framework =>Java™ Input Method Framework (IMF) Technology (en)
• Java 2D Graphics =>Java Tutorial Trail: 2D Graphics



3. Test results


Figure 1 Initial display of the text box (Upper: English text, Lower: Japanese text)

The function of the upper text box is exactly the same as that of the lower text box.
Therefore, the English text can be written in the lower text box.


Figure 2 Zooming out the shapes and texts.


4. Operation guide of the test

Operation

説明

Selecting a text box to be edited

If you click inside an existing text box, the text box will be selected and displayed with a green frame as shown in Figure 1. Here "green frame" means that the text box is editable. So if you click inside the rounded rectangle located at the lower position, the text box contained in the rounded rectangle becomes editable.

Zoom in/out

Click the zoom button, then the following dialog will appear.
∙ Zoom to: Select a scale factor in the combo box.
∙ Percent: You can specify a scale factor in 1% increments.
Click the upward/downward arrow button, or specify the scale number directly in the percent box.

: Property tab isn't currently used. 

Undo, Redo

You can undo and redo changes made to your drawing in the current session.
If there is no change in undo or redo, the button is displayed with gray (Figure 1).

Edit

削除

Click on the edit button, then the menu items will appear.
The menu items of cut, copy, paste and delete are used for editing text in a text box.

:
• Selects a text before the cut or copy operation by dragging with the mouse.
The selected text is displayed with the pink background color.


The selected span of the text is shown in the status panel located at the lowest position in the window as follows:

• paste:

If you copy a text from an external application (e.g. MS-Word) and paste the text into a text box of this application, then the text will be pasted as plain text (unformatted text).

• del: Pressing the Backspace key or the Delete key has the same results.

Set font Attributes
by "Font Menu"

 
Font Menu

From the left in the above menus,
・Combo box of font family: select font family.
・Combo box of font size: select font size.
B : Bold on/off.
I : Italic on/off.
U : Underline on/off.
X2 : Subscript on/off.
X2 : Superscript on/off.
A : open the color chooser.

: If you click the A button, the following custom color chooser will open.
=>Custom Color Chooser


  

:
(1) If you position the text cursor (caret) in the text box by clicking the mouse, the attributes of the character located before the text cursor are shown in the "Font Menu".
(2) If you select characters by dragging the mouse and if the selected characters have the common attributes, those attributes are displayed in the "Font Menu".
Example: If the selected characters have the attributes of "Dialog", "16 points", "Bold" and "Underline", the "Font Menu looks like as follows:

 
 

4. Test code overview

main package

Class

Description

DrawMain

public class DrawMain extends JFrame
The main class of ther test.
API=>DrawMain(on this page)

DrawPanel

public class DrawPanel extends JPanel
The drawing panel on which shapes and text boxes are displaed. The subset of the DrawTop DrawPanel class.
API=>DrawTop DrawPanel

DrawParameters

public class DrawParameters
The class in which the drawing parameters are collected. The subset of the DrawTop DrawParameters class.
API=>DrawTop DrawParameters

ListenerPanel

public class ListenerPanel extends DrawPanel implements KeyListener, MouseListener, InputMethodListener, InputMethodRequests
Handles the keyboad, the mouse events ot the event fired by the Input Method framework. The subset of the DrawTop ListenerPanel class.
API=>DrawTop ListenerPanel

ObjectTable

public class ObjectTable
Registers the frequently referred objects to the static fields of this class. The subset of the DrawTop ObjectTable class.
API=>DrawTop ObjectTable


textBox package

Class

Description

AttributedInterval

public class AttributedInterval implements Serializable, Cloneable
Represents the interval on which a text attribute is defined.
API=> DrawTop AttributedInterval

AttributedStringUtil

public class AttributedStringUtil implements Serializable
Provides the convenient utility methods to handle the AttributedString object.
API=>DrawTop AttributedStringUtil

CaretPosition

public class CaretPosition
Represents the text cursor (caret) position,
API=>DrawTop CaretPosition

FontStyle

public class FontStyle
・Set an font attribute to characters.
・Returns the default font attributes.
・Gets the attributes from the Font Menu.
・Sets the font attributes to the Font Menu.
・Gets the font attributes from the text by specifying the text interval.
API=>DrawTop FontStyle

Interval

public class Interval implements Serializable
The interval which is used in the AttributedStringUtil and TextBox classes.
API=>DrawTop Interval

LineBreaker

public class LineBreaker
Breaks an attributed string into lines which fit within the width of the display area by using java.awt.font.LineBreakMeasurer.
API=>DrawTop LineBreaker

SerializableAttributedString

public class SerializableAttributedString implements Serializable, Cloneable
The java.text.AttributedString cannot be written to a file and cannot be transferred via a clipboard, because it is not serializable. Therefore it cannot be used to copy and paste operation.
This class is implemented with the Serializable interface which can be used to copy and paste operation.
API=> DrawTop SerializableAttributedString

ShapeContainer public class ShapeContainer
The subset of the DrawTop ShapeContainer class. This class is a container of a shape element (ShapeElement) like a line, polyline, cubic curve, rectangle, rounded rectangle and an ellipse. If the shape element is a group element (GroupElement), the group element has the list of shape elements and other group elements.
API=> DrawTop ShapeContainer
TextBox

public class TextBox implements MouseListener, MouseMotionListener, Serializable
The subset of the DrawTop TextBox class. This class is a simple text editor which enable us to input text, edit the text, add text attributes (bold, italic, fomt family, size color etc.) and so on.
API=>DrawTop TextBox

TextUndoSetup

public class TextUndoSetup
If the undo and redo setting is done for each inserted or deleted character, it will be uneconomical. This class provides a convenient way to the undo and redo setting which can handles the block of the characters.
API=>DrawTop TextUndoSetup


util package

Class

Description

ButtonOfColorChooser

public class ButtonOfColorChooser extends JButton implements ActionListener, ColorSelectionListene
In this test, this class is used to configure the A button of the "Set font Attributes" described above. Clicking this button opens the CustomColorChooserDialog described below.
API=> DrawTop ButtonOfColorChooser

ButtonOfPulldownMenu

public class ButtonOfPulldownMenu extends JMenuBar
In this test, this class is used to configure the edit button of the "Edit" described above. The subset of the DrawTop ButtonOfPulldownMenu class.
API=>DrawTop ButtonOfPulldownMenu

ButtonOfToggle

public class ButtonOfToggle extends JToggleButton implements ItemListener
In this test, this class is used to configure the Zoom, Undo, Redo, Del buttons and the B(Bold), I(Italic), U, X2, X2 buttons of the "Set font Attributes" described above.
API=>DrawTop ButtonOfToggle

ComboBox

public class ComboBox extends JComboBox implements ActionListener
In this test, this class is used to configure the "font family" combo box and "font size" combo box of the "Set font Attributes" described above.
API=>DrawTop ComboBox

ContainerManager

public class ContainerManager
The subset of the DrawTop ContainerManager class which manages the data of shapes by the ArrayList.
API=>DrawTop ContainerManager

CustomColorChooserDialog

public class CustomColorChooserDialog extends JDialog implements ActionListener, WindowListener
This class is the custom color chooser for the DrawTop.
API=>DrawTop CustomColorChooserDialog
=>Topics - Custom Color Chooser

DialogOfZoom

public class DialogOfZoom extends JDialog
This class is the dialog described in the "Zoom in/out".
API=>DrawTop DialogOfZoom

DrawMenu

public class DrawMenu
Configurs the button, combo box and so on of the tool bar.
API=> DrawMenu (on this page)

Edit

public class Edit implements ClipboardOwner
The subset of the DrawTop Edit class. This class handles the cut/copy&paste operation.
API=>DrawTop Edit

StatusPanel

public class StatusPanel extends JPanel
This panel is the status panel located on the bottom of the window. The cursor position in the text box and the range of the selected text are displayed on this panel.
API=>DrawTop StatusPanel

TransferableAttributedString

public class TransferableAttributedString implements Transferable, Serializable
The text data format which can be transferred via a clipboad in the "cut/copy&paste" operation.
API=>DrawTop TransferableAttributedString

UndoConstants

public class UndoConstants
This class represents the constants such as:
(a) The codes of command operations which are ADD, DELETE, CHANGE,
(b) The object of command operations which are CONTAINER, TEXTBOX, PAINTSTYLE, SIZE_POSITION and SHAPE.
API=>DrawTop UndoConstants

UndoDrawManager

public class UndoDrawManager extends UndoManager
The extended class of the javax.swing.undo.UndoManager.
API=>DrawTop UndoDrawManager

UndoableDrawEdit

public class UndoableDrawEdit extends AbstractUndoableEdit
The extended class of the AbstractUndoableEdit.
API=>DrawTop UndoableDrawEdit

Util

public class Util
The subset of the DrawTop Util class which consists of the simple methods such as geometric calculations, sort methods and so on.
API=>DrawTop Util

ViewUtil

public class ViewUtil
Zooms in and out the drawing in the window. The subset of the DrawTop ViewUtil class.
API=>DrawTop ViewUtil



5. Test code API

public class DrawMain extends JFrame

Field

Description

scrollPanelSize

protected Dimension scrollPanelSize=new Dimension(800,500);
The size of the JScrollPane.

statusPanelSize

public Dimension statusPanelSize=new Dimension(800,30);
The size of the StatusPanel.

numOfShapes

static int numOfShapes=2;
The number of the shapes displayed on the DrawPanel. In this test the number is two and the shape is the RoundRectangle2D object.


Method

Description

main

public static void main(String[] args)
main method.

setFrame

void setFrame()
・Sets the JFrame of this class which consists of the following objects.
TabbedPane, DrawPanel, ListenerPanel, StatusPanel, ScrollPane.
・Registers the frequent use objects listed up below to the ObjectTable.
ViewUtil, DrawMenu, ContainerManager, Edit, UndoDrawManager, CustomColorChooserDialog

createToolBarPanel

public JPanel createToolBarPanel()
Creates the button, combo box and so on of the tool bar.

createShapes

public static void createShapes()
Creates shapes displayed on the DrawPanel. In this test, the shapes are two RoundRectangle2D objects.

createText

public static AttributedCharacterIterator[] createText()
Creates the attributed strings (AttributedString objects) which are stored in the text boxes and displayed on the DrawPanel.



public class DrawMenu

Field

Description

imageWidth, imageHeight

public final static int imageWidth=20, imageHeight=20;
The size of the image attached on a button.This size is applied to the images on the Undo, Redo, Del buttons and the B(Bold), I(Italic), U, X2, X2 buttons of the "Set font Attributes" described above.

menuItemImageWidth,
menuItemImageHeight

public final static int menuItemImageWidth=64, menuItemImageHeight=10;
The size of the image attached on the menu item of the pulldown menu.

buttonWidth, buttonHeight

public final static int buttonWidth=22, buttonHeight=22;
The size of the button on the tool bar.

DefaultFont

public final static Font DefaultFont=new Font(Font.DIALOG, Font.PLAIN, 12);
Default font.

MenuFont

public final static Font MenuFont=new Font(Font.DIALOG, Font.BOLD, 12);
The size of the text on the button.

MenuItemFont public final static Font MenuItemFont=new Font(Font.DIALOG, Font.BOLD, 11);
The size of the text on the menu item of the pulldown menu.
zoom

public static ButtonOfToggle zoom;
The "zoom" button object.

undo, redo

public static ButtonOfToggle undo, redo;
The "undo" and "redo" button objects.

edit

public static ButtonOfPulldownMenu edit;
The "edit" pulldown menu objects

del

public static ButtonOfToggle del;
The "del" button object.

fontFamily, fontSize

public static ComboBox fontFamily, fontSize;
The "fontFamily" and "fontSize" combo box objects.

fontBold, fontItalic, fontUnderline, fontSubscript, fontSuperscript

public static ButtonOfToggle fontBold, fontItalic, fontUnderline, fontSubscript, fontSuperscript;
The "Bold", "Italic", "Underline", "Subscript" and "Superscript" button objects.

fontColor

public static ButtonOfColorChooser fontColor;
The fontColor button (A button) object.


Method

Description

createEditGroup

public static JPanel createEditGroup()
Creates the "zoom", "undo", "redo" and "del" component objects on the tool bar.

createFontStyleGroup

public static JPanel createFontStyleGroup()
Creates the "Font menu" object.

setFontStyle

public void setFontStyle(FontStyle fontStyle)
Calls this method to set the font attributes to the selected text in the text box when the font attributes are selected on the "Font menu". If no text is selected in the text box, nothing is done.



Copyright (c) 2009-2013
All other trademarks are property of their respective owners.