📄 ishape.java
字号:
/*
* This file is part of Caliph & Emir.
*
* Caliph & Emir is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Caliph & Emir is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Caliph & Emir; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Copyright statement:
* --------------------
* (c) 2005 by Werner Klieber (werner@klieber.info)
* http://caliph-emir.sourceforge.net
*/
package at.wklieber.gui;
import at.wklieber.gui.data.IComponentData;
import org.apache.log4j.Category;
import org.apache.log4j.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
public class IShape
extends IComponent
implements DropTargetListener {
private static Category cat = Logger.getLogger(IShape.class.getName());
private boolean doBuildShape = false;
//private GeneralPath path = null;
private java.util.List dotList = null; // list of IDot elements
private Point mousePos = null;
private IRectangle rectangle = null;
private boolean drawDots = true; // define, wheter to draw or not draw the IDot's
private Color shapeColor = null;
private BufferedImage image = null; // image to use when extracting the dominant color
private Rectangle imageSize = null; // image size on the screen (may be streched)
private boolean isTransparent = false; // if transparent, just the shape is drawn
private boolean acceptDnd = true;
private DropTarget dropTarget = null;
/**
* use this just for getting an instance. Object is not ready to use
*/
public IShape() {
}
// use for building a new shape
public IShape(JPanel drawPanel1, Color drawColor1, Color backgroundColor1,
BufferedImage image1, Rectangle imageSize1) {
init(drawPanel1, 0, 0, 0, 0, drawColor1, backgroundColor1, null, image1, imageSize1,
false, false, true, false, true);
}
// use for restore from a pointlist (uses setComponentData)
// use in Objectpalette
public IShape(JPanel drawPanel1, IComponentData data1) {
init(drawPanel1, 0, 0, 0, 0, Color.BLACK, Color.GRAY, null, null, null,
false, false, true, true, false);
setComponentData(data1);
}
protected void init(JPanel drawPanel1, int posX1, int posY1, int width1, int heigh1,
Color drawColor1, Color backgroundColor1, Color shapeColor1,
BufferedImage image1, Rectangle imageSize1, boolean resizeable1,
boolean isMoveable1, boolean selected1, boolean doDnd1,
boolean doBuildShape1) {
super.init(drawPanel1, null, posX1, posY1, width1, heigh1,
drawColor1, backgroundColor1, resizeable1,
isMoveable1, selected1, doDnd1, "");
setDoDrawBorder(true);
doBuildShape = doBuildShape1;
shapeColor = shapeColor1;
image = image1;
imageSize = imageSize1;
rectangle = new IRectangle(drawPanel1, posX1, posY1, width1, heigh1,
drawColor1, backgroundColor1, resizeable1,
isMoveable1, selected1, doDnd1);
if (!doBuildShape) {
rectangle.setVisible(false);
}
//path = new GeneralPath();
dotList = new Vector();
//this.setBounds(drawPanel.getBounds());
this.setComponentBounds(drawPanel.getBounds());
//cat.debug(this.getBounds().toString());
//cat.debug(this.getComponentBounds().toString());
//--------- dnd trop stuff
dropTarget = new DropTarget(this, (int) DnDConstants.ACTION_COPY,
(DropTargetListener) this, true);
dropTarget.setActive(true);
this.setDropTarget(dropTarget);
validate();
repaint();
}
// makes a deep clone of this IComponent
// implementing classes should override this
public Object clone() {
IComponent returnValue = null;
//cat.debug("make a IShape clone");
IComponentData data = this.getComponentData();
data.setBoundary(new Rectangle(0, 0, 0, 0)); // no size, so the surrounding border of dots is used
returnValue = new IShape(drawPanel, data);
return returnValue;
}
public Color getShapeColor() {
return shapeColor;
}
public void setShapeColor(Color shapeColor) {
this.shapeColor = shapeColor;
}
public boolean isTransparent() {
return isTransparent;
}
public void setTransparent(boolean transparent) {
isTransparent = transparent;
}
public boolean isAcceptDnd() {
return acceptDnd;
}
public void setAcceptDnd(boolean acceptDnd) {
this.acceptDnd = acceptDnd;
}
/**
* store persistent componenet data in the external data
*/
public IComponentData getComponentData() {
IComponentData returnValue = super.getComponentData();
returnValue.setFillColor(getShapeColor());
returnValue.setImage(image);
returnValue.setImageSize(imageSize);
java.util.List pointList = new Vector(dotList.size());
if (dotList != null) {
for (Iterator it = dotList.iterator(); it.hasNext();) {
IDot dot = (IDot) it.next();
cat.debug("write dot: " + dot.toString());
pointList.add(dot.getComponentPoint());
}
}
returnValue.setDotList(pointList);
return returnValue;
}
/**
* configure the component from the external data model
*/
public void setComponentData(IComponentData data1) {
endDoBuildShape();
super.setComponentData(data1);
if (data1.getBoundary() == null) {
boundary = new Rectangle(0, 0, 0, 0);
}
if (dotList != null) {
while (!dotList.isEmpty()) {
IDot dot = (IDot) dotList.remove(0);
this.remove(dot);
}
} else {
dotList = new Vector();
}
for (Iterator it = data1.getDotList().iterator(); it.hasNext();) {
Point p = (Point) it.next();
IDot dot = new IDot(drawPanel, this, p.x, p.y,
drawColor, backgroundColor);
this.addNewDot(dot);
//cat.debug("ADD DOT: " + p.x + ", " + p.y + ", " + toString());
}
setShapeColor(data1.getFillColor());
image = data1.getImage();
imageSize = data1.getImageSize();
// fit dots to a the border, set in data1 if avalable
// if boundary == 0, then the dots are unchanged and the border is set around them
if (boundary.width != 0 && boundary.height != 0) {
resizeShape();
}
boundary = calculateBorder();
repaint();
}
public boolean isDrawDots() {
return drawDots;
}
public void setDrawDots(boolean drawDots1) {
if (drawDots == drawDots1)
return;
for (Iterator it = dotList.iterator(); it.hasNext();) {
IDot dot = (IDot) it.next();
dot.setDoShow(drawDots1);
}
drawDots = drawDots1;
}
public void setDrawPanel(JPanel drawPanel) {
super.setDrawPanel(drawPanel);
if (dotList != null) {
Iterator it = dotList.iterator();
while (it.hasNext()) {
IDot dot = (IDot) it.next();
dot.setDrawPanel(drawPanel);
}
}
//cat.debug("drawPanel set to " + getBounds().toString());
}
protected void addNewDot(IDot component1) {
//super.addOtherComponent(component1);
this.add(component1);
component1.addMouseListener(component1);
component1.addMouseMotionListener(component1);
this.revalidate();
//cat.debug("dot added");
dotList.add(component1);
component1.setId(dotList.size());
}
/**
* a dot has changed -now a shape resize is necessary
*/
protected void updateParent(IComponent component1) {
if (!isResizeable()) {
return;
}
IDot dot1 = (IDot) component1;
boundary = calculateBorder();
repaint();
}
// by default paint a rectangle
protected void drawComponent(Graphics2D g2) {
//Graphics2D g2 = (Graphics2D) g;
// The IDots draw themself
g2.setPaint(drawColor);
//cat.debug("draw lines: " + dotList.size());
Iterator it = dotList.iterator();
Point pStart = null;
if (it.hasNext()) {
IDot dot = (IDot) it.next();
pStart = dot.getComponentPoint();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -