📄 icomponent.java
字号:
AttributedString as = new AttributedString(text);
Shape shape = new Rectangle2D.Double(0,25,12,12);
ShapeGraphicAttribute sga;
sga = new ShapeGraphicAttribute(shape, GraphicAttribute.TOP_ALIGNMENT, false);
as.addAttribute(TextAttribute.CHAR_REPLACEMENT, sga, 0, 1);
*/
//float fontSize = 12;
//Font f = ((Font) fonts.get(0)).deriveFont(Font.PLAIN, fontSize);
//FontMetrics fm = getFontMetrics(f);
//int strH = (int) (fm.getAscent()+fm.getDescent());
// middle point of the component
double middleX = boundary.getX() + boundary.getWidth() / 2;
double middleY = boundary.getY() + boundary.getHeight() / 2;
Color saveColor = this.drawColor;
int borderDistance = 2;
FontMetrics fm = g2.getFontMetrics();
Rectangle2D rect = fm.getStringBounds(text, g2);
double startX = middleX - ((double) rect.getWidth() / 2);
double startY = middleY - ((double) rect.getHeight() / 2) + 2; // correct rounding mistakes
rect = new Rectangle2D.Double(startX - borderDistance, startY - borderDistance,
rect.getWidth() + (2 * borderDistance), rect.getHeight() + (2 * borderDistance));
if (!transparentBackground) {
g2.setColor(Color.WHITE);
g2.fill3DRect((int) rect.getX(), (int) rect.getY(), (int) rect.getWidth(), (int) rect.getHeight(), true);
g2.setColor(saveColor);
g2.draw3DRect((int) rect.getX(), (int) rect.getY(), (int) rect.getWidth(), (int) rect.getHeight(), true);
}
java2dTools.drawCenteredText(g2, text, (int) middleX, (int) middleY);
g2.setColor(saveColor);
} else {
throw new java.lang.UnsupportedOperationException("text drawing for pos " + vertical_pos + ", " + horizontal_pos + " not implemented");
}
}
public void validate() {
super.validate();
this.setBounds(drawPanel.getBounds());
}
/**
* Repaints this component.
* <p/>
* If this component is a lightweight component, this method
* causes a call to this component's <code>paint</code>
* method as soon as possible. Otherwise, this method causes
* a call to this component's <code>update</code> method as soon
* as possible.
* <p/>
* <b>Note</b>: For more information on the paint mechanisms utilitized
* by AWT and Swing, including information on how to write the most
* efficient painting code, see
* <a href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">Painting in AWT and Swing</a>.
*
* @see #update(java.awt.Graphics)
* @since JDK1.0
*/
public void repaint() {
super.repaint(); //To change body of overridden methods use File | Settings | File Templates.
//System.out.println("ICOMPONENT: repaint: " + this.getBounds().toString());
}
//--------------- some tool methods ----------------------------
/**
* Add a component to the Object List for the drawing Panel.
* This ensures the component is repainted automatically
*/
protected void addOtherComponent(IComponent component1) {
this.add(component1);
component1.addMouseListener(component1);
component1.addMouseMotionListener(component1);
component1.revalidate();
}
// ensure that width and heigh are positive
protected static Rectangle validateBounds(Rectangle rect1) {
Rectangle returnValue = new Rectangle(rect1);
int x = (int) returnValue.getX();
int y = (int) returnValue.getY();
int w = (int) returnValue.getWidth();
int h = (int) returnValue.getHeight();
if (w < 0) {
x = x - w;
w = -w;
}
if (h < 0) {
y = y - h;
h = -h;
}
returnValue.setBounds(x, y, w, h);
return returnValue;
}
// override the original contains because the borders don't match
public boolean contains(int posX, int posY) {
if (boundary == null) {
return false;
}
//cat.debug("Contains check for: " + posX + ", " + posY);
int x = (int) boundary.getX();
int y = (int) boundary.getY();
int w = (int) boundary.getWidth();
int h = (int) boundary.getHeight();
boolean thisResult = ((posX >= x) && ((posX - x) <= w) && (posY >= y) && ((posY - y) <= h));
//boolean superResult;
//superResult = super.contains(posX, posY);
//cat.debug("Contains this: " + x + ", " + y + ", " + w + ", " + h + ", " + thisResult);
//cat.debug("Contains super: " + superResult);
return thisResult;
}
protected void finalize() throws Throwable {
super.finalize();
}
public String toString() {
return "IComponent: " + this.getX() + ", " +
this.getY() + ", " + this.getWidth() + ", " +
this.getHeight() + ", Color" + drawColor.getRGB();
}
public boolean isDoDnd() {
return doDnd;
}
public void setDoDnd(boolean doDnd) {
this.doDnd = doDnd;
}
public Color getDrawColor() {
return drawColor;
}
public void setDrawColor(Color drawColor) {
this.drawColor = drawColor;
}
public Color getBackgroundColor() {
return backgroundColor;
}
public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
}
// makes a deep clone of this IComponent
// implementing classes should override this
public Object clone() {
IComponent returnValue = null;
cat.debug("make a IComponent clone");
returnValue = new IComponent(drawPanel, parent, (int) boundary.getX(), (int) boundary.getY(),
(int) boundary.getWidth(), (int) boundary.getHeight(),
drawColor, backgroundColor, isResizeable, isMoveable,
isSelected, doDnd);
return returnValue;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
protected void setComponentAsSelected() {
if (selectedComponent != null) {
selectedComponent.setSelected(false);
}
selectedComponent = this;
selectedComponent.setSelected(true);
}
/**
* use for serialization
*/
public IComponentData getComponentData() {
IComponentData returnValue = new IComponentData(this);
returnValue.setBackGround(backgroundColor);
returnValue.setBoundary(boundary);
return returnValue;
}
/**
* use for deserialization
*/
public void setComponentData(IComponentData data1) {
//cat.debug("v: " + data1.getBoundary().toString());
//cat.debug("n: " + this.boundary.toString());
setComponentBounds(data1.getBoundary());
this.backgroundColor = data1.getBackGround();
setName(data1.getName());
}
//--------------------- Mouse Events ----------------------------------------
public void mouseClicked(MouseEvent e) {
//cat.debug("mouseClicked: " + e.getX() + ", " + e.getY());
if (SwingUtilities.isRightMouseButton(e)) {
menuTools.showPopupMenu(this, e.getX(), e.getY());
} else if (SwingUtilities.isLeftMouseButton(e)) {
setComponentAsSelected();
}
}
public void mousePressed(MouseEvent e) {
//cat.debug("mousePressed: " + e.getX() + ", " + e.getY());
isSelected = true;
if (SwingUtilities.isLeftMouseButton(e)) {
if (!isMoveable) {
return;
}
//move the rectangle if it was already painted
//if not (new instance), this event doesn't occur
if (contains(e.getX(), e.getY())) {
offsetX = (int) (boundary.getX() - e.getX());
offsetY = (int) (boundary.getY() - e.getY());
}
}
repaint(this.getBounds());
}
public void mouseReleased(MouseEvent e) {
//cat.debug("mouseReleased: " + e.getX() + ", " + e.getY());
}
public void mouseEntered(MouseEvent e) {
//cat.debug("mouseEntered: " + e.getX() + ", " + e.getY() + ", DragMode: " + isDragMode);
if ((!isMouseOverComponent) && (doDrawEffectOnMouseOver) && (!isDragMode)) {
isMouseOverComponent = true;
repaint();
}
}
public void mouseExited(MouseEvent e) {
//cat.debug("mouseExited: " + e.getX() + ", " + e.getY());
if ((isMouseOverComponent) && (doDrawEffectOnMouseOver)) {
isMouseOverComponent = false;
repaint();
}
}
public void mouseDragged(MouseEvent e) {
//cat.debug("mouseDragged: " + e.getX() + ", " + e.getY());
if (SwingUtilities.isLeftMouseButton(e)) {
if (!isMoveable) {
//cat.debug("not movable");
return;
}
//int oldX = boundary.x;
boundary.setBounds((e.getX() + offsetX),
(e.getY() + offsetY),
(int) boundary.getWidth(), (int) boundary.getHeight());
//cat.debug(" boundary moved: " + oldX + ", " + boundary.x);
this.repaint();
}
}
public void mouseMoved(MouseEvent e) {
//cat.debug("mouseMoved: " + e.getX() + ", " + e.getY());
}
//--------------------- Mouse Events Dnd Source things -------------------------------
public void dragGestureRecognized(DragGestureEvent event) {
//cat.debug("DRAG: dragGestureRecognized: " + event.toString());
if (doDnd) {
IComponentTransferable trans = new IComponentTransferable(this);
Transferable transferable = trans;
//cat.debug("before start drag");
dragSource.startDrag(event, DragSource.DefaultCopyDrop, transferable, this);
//cat.debug("after start drag");
isMouseOverComponent = false;
isDragMode = true;
repaint();
}
}
public void dragEnter(DragSourceDragEvent event) {
//cat.debug("DRAG: dragEnter: (not implemented)" + event.getX() + ", " + event.getY());
}
public void dragOver(DragSourceDragEvent event) {
//cat.debug("DRAG: dragOver: (not implemented)" + event.getX() + ", " + event.getY());
// cat.debug("Name: <" + this.getComponentName());
isDragMode = false;
}
public void dropActionChanged(DragSourceDragEvent event) {
//cat.debug("DRAG: dropActionChanged: (not implemented)" + event.getX() + ", " + event.getY());
}
public void dragExit(DragSourceEvent event) {
//cat.debug("DRAG: dragExit: (not implemented)" + event.getX() + ", " + event.getY());
}
public void dragDropEnd(DragSourceDropEvent event) {
//cat.debug("DRAG: dragDropEnd: (not implemented)" + event.getX() + ", " + event.getY());
}
// this is invoked by a child to inform this class about changes
// has to be ovveridden when using
protected void updateParent(IComponent child1) {
throw new java.lang.UnsupportedOperationException("class " + this.getClass().getName() + " not implemented");
}
public void setSize(Dimension d) {
boundary.setSize(d);
}
// inserts the popup menu stuff
protected void setPopupMenuEntries() {
// actionlisterner for removing the element
ActionListener removeComponentAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionRemoveComponent(e);
}
};
ActionListener moveComponentToToptAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionMoveComponentToTop(e);
}
};
ActionListener moveComponentToBackAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionMoveComponentToBack(e);
}
};
menuTools.addPopupMenuEntry("&Remove", "Remove this element", "remove_component.gif", removeComponentAction);
menuTools.addPopupMenuEntry("Group &Top", "Move this Component to the Top", "", moveComponentToToptAction);
menuTools.addPopupMenuEntry("Group &Back", "Move this Component to the Background", "", moveComponentToBackAction);
menuTools.addPopupMenuEntry("&Help", "Display help information", "help.gif", (ActionListener) null);
//menuTools.addPopupMenuEntry("&Analysis", "Extract feature information out of this Element", "", null);
}
protected void removePopupMenuEntries() {
menuTools.removeAllPopupEntries();
}
public void actionRemoveComponent(ActionEvent e) {
try {
drawPanel.removeIComponent(this);
this.setEnabled(false);
revalidate();
drawPanel.repaint();
} catch (Exception e1) {
cat.error(e1);
e1.printStackTrace();
}
}
// first draw all lines, then the rest
public void actionMoveComponentToTop(ActionEvent e) {
try {
int newPos = 0;
/* if (!ILine.class.isInstance(this)) {
newPos = drawPanel.getComponentCount() - drawPanel.getILineComponentsCount();
}*/
drawPanel.remove(this);
drawPanel.add(this, newPos);
revalidate();
drawPanel.repaint();
} catch (Exception e1) {
cat.error(e1);
e1.printStackTrace();
}
}
public void actionMoveComponentToBack(ActionEvent e) {
try {
drawPanel.remove(this);
/*if (ILine.class.isInstance(this)) {
int newPos = drawPanel.getComponentCount() - drawPanel.getILineComponentsCount();
drawPanel.add(this, newPos);
} else {*/
drawPanel.add(this);
//}
revalidate();
drawPanel.repaint();
} catch (Exception e1) {
cat.error(e1);
e1.printStackTrace();
}
}
} // end class IComponent
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -