Java Drawing DrawTop

Language

JP  US  UK

 

Editing Shape/Text

 H. Jyounishi, Tokyo Japan
 

Frame (Index), No frame                 version:0.3(latest)  

Summary:The classes on this page provide the capabilities of cut/copy and paste operations for a text or shape objects through clipboard. In addition, the Edit class provides the delete operation.
Relevant major classes of Java SE: java.awt.*, java.awt.geom.*, java.util.*, java.awt.image.*, javax.imageio.ImageIO, java.io.*, java.text.*, java.awt.datatransfer.*
Class on this page: Edit, TransferableAttributedString, TransferableShapeContainer, ScreenImage, PasteContainer

1. Overview
(1) The cut/copy and paste operation for a text object

The cut/copy and paste operation within this application is performed by using the AtttributedString (java.text.AttributedString). On the other hand, the cut/copy and paste operation with external applications is performed by using a plain text which has no text attribute.

Operation Description
cut/copy The object to be copied to the clipboard is a TransferableAttributedString object which implements java.awt.datatransfer.Transferable interface.
An AttributedString object of this application is converted to a SerializableAttributedString object and set to the TransferableAttributedString object.

=> copyString method
The TransferableAttributedString can return both an AttributedString object (SerializableAttributedString) and a simple String object (plain text). The former is used for the operations within this application, and the later is used for exchanging a text with external applications.
paste Checks the content in the clipboard by the isDataFlavorSupported method of the TransferableAttributedString.

∙ The content is an AttributedString(Edit.AttributedStringFlavor)

The cut/copy operation was performed in this application. Converts theSerializableAttributedString to the AttributedString and pastes it in a TextBox.
=>pasteAttributedString method.


∙ The content is a plain text(DataFlavor.stringFlavor)

The cut/copy operation was performed in a external application. Gets the string (plain text) and pastes it in a TextBox.
=>pasteString method


(2)The cut/copy and paste operation for shape objects
The cut/copy and paste operation in this application is performed by using a SerializableElement object. In addition, the cut/copy and paste operation with external applications is performed by using an image object.
Operation Description
cut/copy The object to be copied to the clipboard is a TransferableShapeContainer object which implements java.awt.datatransfer.Transferable interface.
Shape objects (ShapeContainer objects) to be transfered are converted to SerializableElement objects and set to the TransferableShapeContainer object.

=>copyContainers method
The TransferableShapeContainer can return both the ShapeContainer objects (SerializableElement objects) and the image object. The former is used for the operations within this application, and the later is used for exchanging an image with external applications.
paste Checks the contents in the clipboard by the isDataFlavorSupported method of the TransferableShapeContainer.

∙ The contents are shape objects (Edit.DrawShapeFlavor)

The cut/copy operation was performed in this application. Converts the SerializableElement objects to the ShapeContainer objects and pastes them.
=> pasteContainers method


∙ The content is an image object (DataFlavor.imageFlavor)

The cut/copy operation was performed in a external application. Gets the image and pastes it.
=>pasteImage method



2. Class Edit return=>page top
public class Edit implements ClipboardOwner
Field Description
systemClipboard Clipboard systemClipboard
The system clipboard object which can be got as follow.<

Toolkit.getDefaultToolkit().getSystemClipboard();

lostOwnership boolean lostOwnership
True if this object is no longer the clipboard owner. The lostOwnership method sets true to this field.
pasteContainerSave PasteContainer pasteContainerSave
Saves a PasteContainer object to this field.
clickedPointSave
Point2D clickedPointSave
If the mouse is clicked on the canvas before pasting, then saves the clicked point to this field.
AttributedStringFlavor public static final DataFlavor AttributedStringFlavor
The proprietary dataFlavor of this application. The data format is SerializableAttributedString.
=> TransferableAttributedString
DrawShapeFlavor public static final DataFlavor DrawShapeFlavor
The proprietary dataFlavor of this application. The data format is a SerializableElement.
=> TransferableShapeContainer
PlainString public static final int PlainString = 1;
This field represents a type of the data which is stored in the systemClipboard, and is passed to the hasClipboad method as its parameter. The "PlainString" means a text having no attributes like font family, font size, bold, underline and so on. Usually the "PlainString" in the systemClipboard is copied in other application like Microsoft words.
AttributedString public static final int AttributedString = 2;
systemClipboardにストアされているデータのタイプを表し、 hasClipboadメソッドの引数で使われる。 属性のある文字列で、このアプリケーションでコピーされた文字列。
This field represents a type of the data which is stored in the systemClipboard, and is passed to the hasClipboad method as its parameter. The "AttributedString" means a text having some attributes like font family, font size, bold, underline and so on. Usually the "AttributedString" in the systemClipboard is copied in this application.
Shape public static final int Shape = 3;
This field represents a type of the data which is stored in the systemClipboard, and is passed to the hasClipboad method as its parameter. The "Shape" means a shape object created in this application.
Image public static final int Image = 4;
This field represents a type of the data which is stored in the systemClipboard, and is passed to the hasClipboad method as its parameter. The "Image" means an image object created in this application or copied in other applications like Microsoft paint and so on.

Method Description
cut public void cut()
This method performs the operation of "cut" command.
Sets the selected text in a TextBox or the selected shape objects to the SystemClipboard by the copyString method or the copyContainers method with false of the copy parameter.
copy public void copy()
This method performs the operation of "copy" command.
Sets the selected text in a TextBox or the selected shape objects to the SystemClipboard by the copyString or the copyContainers method with true of the copy parameter.
copyString private boolean copyString(ShapeContainer shapeContainer, boolean copy)
Parameters:
shapeContainer - The ShapeContainer which has a TextBox of the selected text.
copy - If the parameter is false, deletes the selected text, otherwise leaves it.
Processing:
∙ Gets the selected text (AttributedString object) as follow.

TextBox textBox=shapeContainer.getTextBox();
AttributedString attribStr=textBox.getSelectedText();

∙ If the copy is false, then deletes the selected text.
∙ Creates a new TransferableAttributedString object of the AttributedString and sets it to the systemClipboard.
copyContainers private void copyContainers(ShapeContainer[] containers, boolean copy)
Parameters:
containers - The array of the selected shape objects (ShapeContainer objects).
copy - If the parameter is false, deletes the selected shape objects, otherwise leaves them.
Processing:
∙ Creates a new TransferableShapeContainer object of the selected shape objects and sets it to the systemClipboard.
∙ If the copy is false, then deletes the selected shape objects and sets the undo setup for deleting.
hasClipboad public boolean hasClipboad(int type)
Parameters:
type - The type of the data stored in SystemClipboard which is Edit.PlainString, Edit.AttributedString, Edit.Shape or Edit.Image
Returns:
If data specified by type is stored in the systemClipboard, then returns true.
paste public void paste()
This method performs the operation of "paste" command.
Gets the contents from the SystemClipboard. Transferable contents=this.systemClipboard.getContents(null); 
According the DataFlavor of the contents, performs the following processings.

Edit.AttributedStringFlavor

The content is an AttributedString copied from a TextBox in this application. Pastes the content to an editable TextBox by the pasteAttributedString method.


DataFlavor.stringFlavor

The content is a plain text copied from an external application. Pastes the content to an editable TextBox by the pasteString method.


Edit.DrawShapeFlavor

The contents are shape objects (SerializableElement objects) in this application. Creates a new PasteContainer object with the parameter of the SerializableElement objects. If the new PasteContainer object doesn't equal the pasteContainerSave, then sets the new PasteContainer object to the pasteContainerSave field.
Pastes shape objects on the canvas by the pasteContainers method.


DataFlavor.imageFlavor

The content is an image object copied from an external application. Creates a new PasteContainer object with the parameter of the image object. Pastes the image on the canvas by the pasteImage method.

pasteString public boolean pasteString(String str)
Parameters:
str - The string to be pasted.
Returns:
True if the pasting is successful.
Processing:
∙ Gets an editable TextBox by the ContainerManager.getEditableTextBox method.
∙ Converts the str to the AttributedString and inserts it to the TextBox

textBox.insertText (TextBox.COMMAND, position, attribStr.getIterator());

pasteAttributed
String
private boolean pasteAttributedString(SerializableAttributedString sString)
Parameters:
sString - The SerializableAttributedString object to be pasted.
Returns:
True if the pasting is successful.
Processing:
∙ Gets an editable TextBox by the ContainerManager.getEditableTextBox method .
∙ Gets the AttributedString from the SerializableAttributedString and Inserts the AttributedString to the TextBox

textBox.insertText (TextBox.COMMAND, position, attribStr.getIterator());

pasteContainers private boolean pasteContainers(PasteContainer pasteContainer)
Parameters:
pasteContainer - The PasteContainer object.
Returns:
True if the pasting is successful.
Processing:
∙ Converts the contents (an array of the SerializableElement objects) retrieved from the clipboard to ShapeContainer objects by the SerializableElementUtil.invertShapeContainer method.
∙ Determines the pasting position by the getPastePosition method.
∙ Adds the the ShapeContainer objects by the ContainerManager.addContainer method and sets the undo setup for adding.
pasteImage private boolean pasteImage(PasteContainer pasteContainer)
Parameters:
pasteContainer - The PasteContainer object.
Returns:
True if the pasting is successful.
Processing:
∙ Gets the image from the pasteContainer, creates the SerializableElement object storing the image and converts the SerializableElement object to the ShapeContainer object also storing the image.
∙ Adds the the ShapeContainer objects to the ContainerList by the ContainerManager.addContainer method.
getPastePosition private Vector2D getPastePosition(PasteContainer pasteContainer, ShapeContainer[] containers)
Parameters:
pasteContainer - The PasteContainer object.
containers - The ShapeContainer objects to be pasted.
Returns:
The translation vector of the shape objects.
Processing:
According to the rule of paste position, this method returns the Vector2D object.
∙ If the mouse is clicked before pasting, gets the clicked point by the ExecCommand.getClickedPoint method and set it to clickedPointSave field.
∙ Gets the shift count by calling the getCurrentShiftCount method of the pasteContainer and then calculates the translation vector based on the shift count and returns it.
createSerializableElement private SerializableElement createSerializableElement(Image image)
Parameters:
iamge - The Image object.
Returns:
The SerializableElement object holding the image object.
Processing:
Creates the SerializableElement object corresponding to the ImageElement holding the image object.
Performs the following operation on the image object.
∙ Sets the size data of the SerializableElement so that the width and the height of the ImageElement are less than 128 pixels.
∙ Converts the image to a byte array by the getByteImage method of this class and sets the byte array to the imageByteData field of the SerializableElement.
getByteImage public byte[] getByteImage(Image image) Performs the same operation as the ImageElement.getByteImage method.
lostOwnership public void lostOwnership(Clipboard clipboard, Transferable contents)
Sets true to the lostOwnerShip field.
getClipboard
Information
private String getClipboardInformation(Clipboard clipboard, ClipboardOwner clipboardOwner)
Return the string representing the contents in the clipboard.
delete public void delete()
This method performs the operation of "delete" command.
Deletes the text in a TextBox or the selected shape objects (ElementContaine objects).
∙ Deletes the text in a TextBox.

Gets an editable TextBox by the ContainerManager.getEditableTextBox method.
Deletes the selected text or the one character before the text cursor by "delete" command or "Back space" key.

∙ Deletes the selected shape objects.

Gets the selected shape objects by the ContainerManager.getSelectedContainers method and calls the deleteContainer method to remove each of the selected shapes from the ContainerList.
Sets the undo setup for deleting.


: Rule of paste position
The paste position is depend on whether or not the shape objects are pasted to the same file and page, and also depend on whether or not the mouse is clicked on the canvas before pasting.
(1) If the shape objects are pasted to the same file and page, then the pasting will be as follow.

The shape objects are pasted with translating slightly to the right lower position to avoid overlap. If repeating the paste, the same operation will be performed.

(2) If the shape objects are pasted to a different file or a page, then the pasting will be as follow.

At first, the shape objects are pasted to the same position of the original shape objects. If repeating the paste, the shape objects will be pasted with translating slightly to the right lower position to avoid overlap.

(3) If the mouse is clicked on the canvas before pasting, then the pasting will be as follow.

The shape objects are pasted around the clicked point. If repeating the paste, the shape objects will be pasted with translating slightly to the right lower position to avoid overlap.

Figure 1 Changing the paste position by the mouse clicking
The mouse was clicked on the canvas before 3rd past.



3. Class TransferableAttributedString return=>page top
public class TransferableAttributedString implements Transferable, Serializable

An AttributedString object is set to this object. This object can return both the AttributedString object and the String object (plain text). The former is used for the operation within this application, and the later is used for exchanging a text with external applications.

DataFlavor
The two types of the DataFlavor objects are used in this application.
Edit.AttributedStringFlavor: The proprietary DataFlavor of this application. The data format is SerializableAttributedString.
DataFlavor.stringFlavor: The DataFlavor representing a Java Unicode String class, which is used for exchanging a plain text with external applications.
Field Description
sString private SerializableAttributedString sString
The SerializableAttributedString object.

Method Description
Constructor public TransferableAttributedString(AttributedCharacterIterator iterator)
Parameters:
iterator - The AttributedCharacterIterator object.
Processing:
Creates the SerializableAttributedString object and sets it to the sString field.
getTransferData public Object getTransferData(DataFlavor flavor)
Parameters:
flavor - The data flavor.
Processing:
∙ Returns the sString field, if the flavor equals Edit.AttributedStringFlavor.
∙ Returns the plain text of the sString field, if the flavor equals DataFlavor.stringFlavor.
getTransferDataFlavors public DataFlavor[] getTransferDataFlavors()
Returns the array which stores Edit.AttributedStringFlavor and DataFlavor.stringFlavor.
isDataFlavorSupported public boolean isDataFlavorSupported(DataFlavor flavor)
Parameters:
flavor - The data flavor.
Processing:
Returns true if the flavor equals Edit.AttributedStringFlavor or DataFlavor.stringFlavor.


4. Class TransferableShapeContainer return=>page top
public class TransferableShapeContainer implements Transferable, Serializable

Sets the shape objects (ShapeContainer objects) to this class to transfer them via the clipboard. The shape objects are converted to the SerializableElement objects for the cut/copy/paste operations of this application and converted to the Image objects for exchanging the data between this application and external applications (e.g. MS Word, Paint).

(1) DataFlavor
The two types of the DataFlavor objects are used in this application.
Edit.DrawShapeFlavor: The proprietary DataFlavor of this application. The data format is SerializableElement.
DataFlavor.imageFlavor: The DataFlavor representing a Java Image class which is standard and used for exchanging an image data with external applications.
(2) Data conversion
ShapeContainer -> SerializableElement

Converts by the convertShapeContainer by the SerializableElementUtil.

ShapeContainer -> Image

Converts by the ScreenImage constructor and gets the Image object by the getImage method the ScreenImage.

Field Description
sElements SerializableElement[] sElements
The array of the SerializableElement objects.
image
private Image image
The image object.

Method Description
Constructor public TransferableShapeContainer(ShapeContainer[] containers)
Parameters:
containers - The ShapeContainer objects
Processing:
∙ Converts the containers to the SerializableElement objects by the SerializableElementUtil.convertShapeContainer method and sets them to the sElements field.
∙ Converts the containers to the image objects by the the ScreenImage and sets the image to the image field.
getTransferData public Object getTransferData(DataFlavor flavor) Parameters:
flavor - The data flavor.
Processing:
∙ Returns the sElements field, if the flavor equals Edit.DrawShapeFlavor.
∙ Returns the image field, if the flavor equals DataFlavor.imageFlavor.
getTransferDataFlavors public DataFlavor[] getTransferDataFlavors()
Returns the array which stores Edit.DrawShapeFlavor and DataFlavor.imageFlavor.
isDataFlavorSupported public boolean isDataFlavorSupported(DataFlavor flavor)
Parameters:
flavor - The data flavor.
Processing:
Returns true if the flavor equals Edit.DrawShapeFlavor or DataFlavor.imageFlavor.


5. Class ScreenImage return=>page top
public class ScreenImage implements Serializable, Cloneable
This class provides the methods to create an image of the selected shape objects which will be copied to the clipboard. The created image is used for exchanging an image data with external applications (e.g. MS Word, Paint).
Field Description
containers ShapeContainer[] containers
The array of the ShapeContainer objects to be converted to image.
Usually, the containers are the selected shape objects.<
imageArea Rectangle2D imageArea
The imagable area.
bufferedImage
BufferedImage bufferedImage
The buffer where the image data is created.

Method
Description
Constructor
public ScreenImage(ShapeContainer[] containers)
Sets the parameter to the containers field.
Processing:
∙ Sets the returned value of the getImageSize method to the imageArea field.
∙ Creates the BufferedImage objects whose size is determined by the imageArea and calls the drawImage method.
getImageSize private Rectangle2D getImageSize(double scale, int marginX, int marginY)
Parameters:
scale - The scale factor
margineX, margineY - The margines (pixels) of the image size.
Returns:
Gets the bounding box enclosing all the containers and creates a new rectangle object from the bounding box by multiplying it by the scale and adding the (margineX, margineY), and finally returns the new rectangle object.
drawImage private void drawImage(Graphics g, double scale)
Paremeters:
g - The Graphics object created from the bufferedImage.
Graphics2D g2 = this.bufferedImage.createGraphics();
scale - The scale factor.
Processing:
∙ Sets the DrawParameters.DrawOnImage to the DrawParameters.DrawMode.
∙ Draws the image of the containers by the ShapeContainer.drawShape method.
getImage public Image getImage()
Returns the bufferedImage.
toString public String toString()
Returns the string representing this object.


6. Class PasteContainer return=>page top
=> Rule of paste position
public class PasteContainer implements Serializable, Cloneable
Field Description
originalFileName String originalFileName
The name of the file which stores the shape object to be copied to the systemClipboard. This field is created by extracting a file name part from the fileNameAndPage field of the SerializableElement.
: If the contents (text or image) are copied from external applications (e.g. MS Word, Paint), this field is ineffective.
originalPage int originalPage
The page of the file which stores the shape object to be copied to the systemClipboard. This field is created by extracting a page part from the fileNameAndPage field of the SerializableElement.
: If the contents are copied from external applications (e.g. MS Word, Paint), this field is ineffective.
newContents boolean newContents
True if new contents (sElements or image field) are set to this object.
pastedFileName Buffe String pastedFileName
The name of the file to which the contents of the the clipboard is pasted.
pastedPage int pastedPage
The number of the page to which the contents of the the clipboard is pasted.
shiftCount int shiftCount
The paste count on the same page (pastedPage) of the same file (pastedFileName).
id String id
The identifier of the contents (the shape or the image object) copied to the clipboard. This identifier is used for checking whether the contents is newly copied to the clipboard or not.
∙ Identifier of a shape object

The identifier is the date field of the SerializableElement copied to the clipboard.

∙ Identifier of a image object

The image object must be copied to clipboard by the standard format of the DataFlavor.imageFlavor, so the identifier must be created using only the image data. The createImageId method creates this type of identifier.

sElements SerializableElement[] sElements
The array of the SerializableElement objects which are copied to the clipboard.
image BufferedImage image
The Image data object which are copied to the clipboard.

Method Description
Constructor public PasteContainer(SerializableElement[] sElements)
Parameters:
sElements - The array of the SerializableElement objects.
Processing:
Creates a new PasteContainer object storing shape objects. Gets the data from the SerializableElement and sets them to the originalFileName, originalPage, id and sElements fields. And sets true to the newContents and sets 0 to the shiftCount.
Constructor
public PasteContainer(BufferedImage bufferedImage)
Parameters:
bufferedImage - The Image object.
Processing:
Creates a new PasteContainer object storing an image object.
Creates the identifier by the createImageId method and sets it to the Id field. And sets the bufferedImage to the image, sets true to the newContents and sets 0 to the shiftCount.
toString public String toString()
Returns the string representing this object.
createImageId private long createImageId(BufferedImage bufferedImage)
Parameters:
bufferedImage - The Image object.
Processing:
Sums up integer pixels (RGBA values) along the center line and the middle line of the image and returns the sum as long type.
isNewContents public boolean isNewContents()
Returns the newContents field.
setNewContents public void setNewContents(boolean newContents)
Sets the parameter to the newContents field.
getId
public String getId()
Returns the Id field.
getOriginalFileName public String getOriginalFileName()
Returns the originalFileName field.
getOriginalPage public int getOriginalPage()
Returns the originalPage field.
getPastedFileName public String getPastedFileName()
Returns the pastedFileName field.
setPastedFileName public void setPasteFileName(String pasteFileName)
Sets the parameter to the pastedFileName field.
getPastedPage public int getPastedPage()
Returns the pastedPage field.
setPastedPage public void setPastedPage(int pastePage)
Sets the parameter to the pastedPage field.
getShiftCount public int getShiftCount()
Returns the shiftCount field.
setShiftCount public void setShiftCount(int shiftCount)
Sets the parameter to the shiftCount field.
getElementList public SerializableElement[] getElementList()
Returns the sElements field.
getImage public Image getImage()
Returns the image field.
getCurrentShiftCount private int getCurrentShiftCount()
This method is called by the getPastePosition method of the Edit.
Returns how many times the shape object or the image in the clipboard was pasted to the same file and the same page.
pasteToOriginalFilePage private boolean pasteToOriginalFilePage()
Returns true if the currently displaying file and page is the same as the file and page from which the shape object or the image is cut or copied.
pasteToSameFilePage private boolean pasteToSameFilePage()
Returns true if the currently displaying file and page is the same as the file and page to which the previous paste operation was executed.


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