Java Drawing DrawTop

Language

JP  US  UK

 

Component Library

 H. Jyounishi, Tokyo Japan
 

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

Classes on this page: ComponentLibDialog, ComponentAction, ComponentManager, ComponentLibPanel, TransferableComponent
Unit Test =>DrawTest Component library

1. Overview 1.1 Operations
=> User's guide Library components
(1) A shape and a group of shapes can be registered to the component library. A registered component can be removed from the component library.
(2) If multiple shapes are selected and registered to the component library, they are registered one by one. To register as one component, the multiple shapes must be grouped.
(3) A component can be moved to the desired position by the mouse drag and drop operation. First, press the mouse button on a component to be moved , then the component will be displayed with the black border (Figure 1.3 (a)). Next, drags the component to the target position, then the vertical black line will be displayed at the target position (Figure 1.3 (a)). Finally release the mouse button at the target position. (Figure 1.3 (b))
(4) To create a new shape on the canvas from a component, moves the component from the "component lib" panel to the canvas by "Drag and Drop" operation (Figure 1.4).



Figure 1.1 Component dialog


Polygon Polygon Group

Figure 1.2 Created shapes on the canvas by "Drag and Drop" operation.

(a) Drag a component
to the left-top position
.
(b) The component is dropped
at the left-top position
.

Figure 1.3 Move a component in the "Component lib" panel



Figure 1.4 Move a component to the canvas
by "Drag and Drop" operation.


1.2 ToolTip 

An "easy-to-understand, multiple lines" tool tip is used for the "Component lib" panel and the buttons, because of the operation is a little complicated. The setting of the dismissal delay time for a tool tip is 60 seconds that is longer than the Java standard tool tip.


Figure 1.5 Tool tips on the "Component lib" panel and "add component" button.


2. Class ComponentLibDialog return=>page top
public class ComponentLibDialog extends JDialog

The component is registered to the "ComponentLibrary" file and accessed by the methods of the ContainerManeger.

Field Description
action ComponentAction action
Sets the ComponentAction object to this field.
componentManager ComponentManager componentManager
Sets the ComponentManager object to this field.
componentLibPanel ComponentLibPanel componentLibPanel
Sets the ComponentLibPanel object to this field.
messageLabel1 JLabel messageLabel1
Sets the label object on which the showMessage method displays a message.
messageLabel2 JLabel messageLabel2
Sets the label object on which the showMessage method displays a message.
If "\n" is contained in the parameter(message) of the showMessage method, then the text string after "\n" is displayed on this label.
addButton JButton addButton
The button object of "add component"
deleteButton JButton deleteButton
The button object of "delete component"
scrollPane JScrollPane scrollPane
The JScrollPane object of this object.

Method Description
Constructor public ComponentLibDialog(int commandId)
∙ Create a new ComponentAction object, ComponentManager object and ComponentLibPanel object.
∙ Calls the createDialog method.
createDialog public void createDialog()
Arranges the message panel, the scroll pane and the button panel on the dialog.
∙ Adds the WindowListener to the dialog.

When the dialog is closed, the windowClosing method will be called.

∙ Adds all the necessary components to this dialog (Figure 1.1)
setButtonAttribute private void setButtonAttribute(JButton button, String actionCommand, ActionListener action, Dimension dimension)
Parameters:
button - JButton object.
actionCommand - An action command to be set to the button.
action - An ActionLisner to be added to the button.
dimension - The button size.
showDialog public void showDialog()
Shows the dialog as follows.
∙ Reads components by the ComponentManager.readComponentList method.
∙ Registers the components to the ComponentLibPanel.

After that, the components are displayed on the dialog.

∙ Sets the size of the scroll pane(setPreferredSize method).
∙ Sets this dialog location..
∙ Show the dialog.

this.pack();
this.setVisible(true);
∙ Registered this object to the MenuUtil object by the MenuUtil.setMenuComponent method.

showMessage public void showMessage(String message, Color color)
Parameters:
massage - The massage text
color - The color of the message
Processing:
Shows the message to the messageLabel1 and the messageLabel2.
If "\n" is contained in the message, the part of message string after "\n" is shown on the messageLabel2.
getName public String getName()
Returns "LIB".
setSelected public void setSelected(boolean selected)
Nothing is done.
isSelected public boolean isSelected()
Return false always.
getSelectedComponent public ShapeContainer getSelectedComponent()
Return the selected component on the ComponentLibPanel.
getContainerManager protected ContainerManager getContainerManager()
Returns the ContainerManager object which manages the shapes on the canvas.
getComponentLibPanel protected ComponentLibPanel getComponentLibPanel()
Returns the ComponentLibPanel object.
getComponentManager protected ComponentManager getComponentManager()
Returns the ComponentManager object which manages the registerd components.
getScrollPane protected JScrollPane getScrollPane()
Return the JScrollPane object.
getDialog protected JDialog getDialog()
Return the dialog object.


3 Class ComponentAction return=>page top
class ComponentAction extends AbstractAction implements WindowListener

This class performs the action events of the buttons (addButton, deleteButton) in the dialog and the window event.

Field Description
dialog ComponentLibDialog dialog
Sets the ComponentLibDialog object.

Method Description
actionPerformed public void actionPerformed(ActionEvent e)
When the button is clicked in the dialog, performs the action processing of the button.
∙ "add component" button

Calls the addComponent method.

∙ "delete component" button

Calls deleteComponent method.

addComponent private void addComponent()
Registers the selected shape to the component library.
∙ Creates the clone of the selected shape.
∙ Creates the component shape by resizing the selected shape small to fit the display cell in the component dialog.
∙ Adds the component shape to the ContainerList by the ContainerManager.addContainer method.
∙ Writes the ContainerList to ComponentLibrary file by the ComponentManger.writeComponentList method.
∙ Redraws the ComponentLibPanel by the followings.
 ComponentLibPanel.revalidate();
 ComponentLibPanel.repaint();
deleteComponent private void deleteComponent()
Removes the selected shape from the component library.
∙ Shows the confirmation dialog by the JOptionPane.showConfirmDialog method.
∙ Remove the selected component from the ContainerList by the ContainerManager.deleteContainer method.
∙ Writes the ContainerList to ComponentLibrary file by the ComponentManger.writeComponentList method.
∙ Redraws the ComponentLibPanel by the following.
 ComponentLibPanel.revalidate();
 ComponentLibPanel.repaint();
closeDialog private void closeDialog()
Closes the dialog.
Calls the MenuUtil.removeMenuComponent method.
windowClosing public void windowClosing(WindowEvent e)
Calls the closeDialog method.


4. Class ComponentManager return=>page top
class ComponentManager

This class provides the input and output methods to the component library file (ComponentLibrary).

Field Description
dialog ComponentLibDialog dialog
Sets the ComponentLibDialog object.
containerManager ContainerManager containerManager
The ContainerManager object which manages shape objects.
drawFileIo DrawFileIo drawFileIo
The DrawFileIo object which performs the file I/O of the component library file.
filePath String filePath
The file path name of the component library file.
version String version="ver0.0"
The version of the format of the component library file.

Method Description
Constructor ∙ Create a new objects of the ContainerManager
∙ Sets the filePath.
getContainerManager public ContainerManager getContainerManager()
Returns the containerManager.
writeComponentList public void writeComponentList()
Writes the ArrayList storing all the components to the component library file by the DrawFileIo.writeContainerList method.
readComponentList public void readComponentList()
Reads the component library file to the ArrayList by the DrawFileIo.readContainerList method
moveComponent public void moveComponent(int selectedComponent, int moveTo)
Moves the display position of the selected component.
=>Figure 1.3


5. Class ComponentLibPanel return=>page top
public class ComponentLibPanel extends JPanel implements MouseListener, MouseMotionListener,
DragGestureListener, DropTargetListener

This panel is set to the scrollPane of the ComponentLibDialog as the view panel and provides the following functions.

∙ Displays the component in the small gridded cell.
∙ Displays the black border of the selected component (Figure 1.3).
∙ The selected component can be moved on the dialog by dragging it with the mouse.
∙ The selected component can be copied on the canvas by "Drag and Drop" operation (Figure 1.4).

Field Description
dialog ComponentLibDialog dialog
Sets the ComponentLibDialog object.
sizeOfComponent final Dimension sizeOfComponent
The display size of the component.
xGap final int xGap=12
The width of the grid lines.
yGap final int yGap=12
The height of the grid lines.
xNum final int xNum=8
The number of the gridded cell in x direction.
yNum int yNum
The current number of the gridded cell in y direction.
components ShapeContainer[] components
The array of the components to be displayed on the view panel.
panelSize Dimension panelSize
The size of the view panel.
componentRects Rectangle2D[] componentRects
The array of the rectangles representing the gridded cells.
selectedComponent int selectedComponent
The number of the selected component (>0).
gapIndex int gapIndex
The index of the gap between two cells. The index of the gap before the first cell is 0 and the index of the gap after the first cell is 1. If the value of this field is equal to or greater than 0, then the vertical black line will be displayed at the gap indicated by the index.
mToolTip MultiLineToolTip mToolTip=new MultiLineToolTip(500, 60000);
y time in milliseconds and the second parameter is the dismissal delay time also in in milliseconds.

Method Description
Constructor public ComponentLibPanel()
Adds the MouseListener, MouseMotionListener, DragGestureListener and DropTargetListener to this object.
createToolTip public JToolTip createToolTip()
Returns the MultiLineToolTip object. This method is oerriding the same name method of the JComponent.
getSizeOfComponent public Dimension getSizeOfComponent()
Returns the value of the sizeOfComponent field.
getSelectedComponentByIndex public int getSelectedComponentByIndex()
Returns the value of the selectedComponent field.
setSelectedComponentByIndex public void setSelectedComponentByIndex(int selectedComponent)
Sets the parameter to the selectedComponent field.
getSizeOfScrollPane public Dimension getSizeOfScrollPane()
Returns the size of the view panel.
The size is calculated by using the xNum, yNum, xGap, yGap and the sizeOfComponent fields.
paint public void paint(Graphics g)
This method draws the following shapes.
∙ The thin gray border enclosing the component.
∙ The bold black border of the selected component.
∙ The vertical bold black line between the two components, if the gapIndex is equal to or greater than 0.
∙ The shape of the components which are drawn by the drawComponent method.。
setComponentPanel private void setComponentPanel()
Calculates the layout of the view panel based on the number of the registered component. Here the layout of the view panel is represented by the yNum, panelSize and the componentRects.
drawComponent private void drawComponent(Graphics g, int i, Rectangle2D rect)
Parameters:
i - The number of the component to be displayed (0,1,2....).
rect - The rectangle in which the component is displayed.
Processing:
Draws the shape of the components[i].
dragGestureRecognized public void dragGestureRecognized(DragGestureEvent e)
This is a method defined by the DragGestureListener interface.
Sets the selected shapes to the TransferableComponent object and calls the startDrag method of the DragGestureEvent.
drop public void drop(DropTargetDropEvent e)
This is a method defined by the DragTargetListener interface.
If the drop position of "Drag and Drop" operation is located on the ComponentLibPanel, then this method calls the mouseReleased method of the MouseListener which moves the selected component.
: The proccedure to move the selected component had been performed by the mousePressed, mouseDragged and mouseReleased methods. However, after the DragGestureListener was implemented in this class, the mouseDragged and mouseReleased method didn't work correctly, so the procedure to call the mouseReleased from this drop method is currently used for the purpose to move the selected component.
dragOver public void dragOver(DropTargetDragEvent e)
This is a method defined by the DragTargetListener interface.
Calls the mouseDragged method of the MouseListener to move the selected component.
mousePressed public void mousePressed(MouseEvent e)
Gets the number of the component on which the mouse button is pressed by the ptInside method.
If the number is equal to or greater than 0, then set the number to the selectedComponent field.
mouseDragged public void mouseDragged(MouseEvent e)
Gets the number of the gap between the two components by the getNearestGap method and set the number to the gapIndex field.
mouseReleased public void mouseReleased(MouseEvent e)
When the mouse button is released, the display position of the selected component is decided.
So, this method moves the selected component to the position indicated by thegapIndex by the moveComponent method. After that, writes all the components to the component library file by the ComponentManger.writeComponentList.
mouseClicked public void mouseClicked(MouseEvent e)
⋅ Nothing is done.
mouseEntered public void mouseEntered(MouseEvent e)
Nothing is done.
mouseExited
public void mouseExited(MouseEvent e)
⋅ Nothing is done.
mouseMoved public void mouseMoved(MouseEvent e)
⋅ Nothing is done.
ptInside public int ptInside(Point2D point)
Calculates which element of the componentRects contains the specified point and returns the index of the componentRects.
Returns -1 if no element of the componentRects contains the point.
getNearestGap public int getNearestGap(Point2D point)
Return the index of the gap which is nearest to the specified point.
moveComponent private void moveComponent(int selectedComponent, int gapIndex)
Paremter:
selectedComponent - The index of the selected component.
gapIndex - The index of the gap.
Processing:
Move the display position of the selected component to the position specified by the gapIndex by the ComponentManger.moveComponent method.


6.Class TransferableComponent return=>page top
public class TransferableComponent implements Transferable

This class is used to used to provide data for a "Drag and Drop" transfer operation.

Field Description

componentFlavor

public static DataFlavor componentFlavor = new DataFlavor(ShapeContainer.class, "ShapeContainer")

supportedFlavors

public static DataFlavor[] supportedFlavors = {componentFlavor}

container ShapeContainer container
Stores the ShapeContainer object which is transferred by a "Drag and Drop" operation.

Method Description
Constructor public TransferableComponent(ShapeContainer container)
Sets the argument to the field variable.
getTransferDataFlavors public DataFlavor[] getTransferDataFlavors()
Returns the value of supportedFlavors.
isDataFlavorSupported public boolean isDataFlavorSupported(DataFlavor flavor)
Returns true if the argument equals the value of componentFlavor.
getTransferData public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException
Returns the value of container if the argument equals the value of componentFlavor.


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