?? baseobject.java
字號:
/**
* @(#)G2d.java
*/
package project.paint;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
public class BaseObject implements Cloneable{
public static final int PENCIL=0;
public static final int LINE=1;
public static final int RECTANGLE=2;
public static final int ROUNDRECT=3;
public static final int OVAL=4;
public static final int POLYGON=5;
public static final int FILL=6;
public static final int ERASE=7;
public static final int ROTATE=8;
public int type; //對象類型
public float stroke;//畫筆粗細
public Color color;//漸變色
public Color color2;
public int gradientPaint;//漸變類型
public Point p1,p2;//漸變端點
protected Paint ptInterface;
protected Shape shape;
protected Shape shape2;
public boolean filled;
public boolean isSquare;
public double rotateNum;
protected LinkedList<Integer> X,Y;
public BaseObject() {
X=new LinkedList<Integer>();
Y=new LinkedList<Integer>();
stroke=1.0f;
color=Color.BLACK;
color2=Color.WHITE;
gradientPaint=0;
filled=false;
isSquare=false;
rotateNum=0;
type=99;
}
//判斷是否在對象內
public boolean isCursorIn(int x,int y) {
try {
if (shape.contains(x,y))
return true;
else
return false;
} catch (NullPointerException npe) {
return false;
}
}
//判斷對象是否可以旋轉
public boolean canRotate(int x,int y) {
int x2=getPointX(1);
int y2=getPointY(1);
int x1=getPointX(0);
int y1=getPointY(0);
if ((x>x2-6) && (y>y2-6) && (x<x2+6) && (y<y2+6))
return true;
else if((x>x1-6) && (y>y1-6) && (x<x1+6) && (y<y1+6))
return true;
else if((x>x2-6) && (y>y1-6) && (x<x2+6) && (y<y1+6))
return true;
else if((x>x1-6) && (y>y2-6) && (x<x1+6) && (y<y2+6))
return true;
else
return false;
}
//判斷對象是否可以改變大小
public boolean canResizeObject(int x,int y) {
int buff=(int)(stroke/2);
//int x2=getMaxX()+buff;
//int y2=getMaxY()+buff;
int x2=getPointX(1);
int y2=getPointY(1);
if ((x>x2-4) && (y>y2-4) && (x<x2+4) && (y<y2+4))
return true;
else
return false;
}
public void draw(Graphics2D g) {}
public void fill(){}
public void drawLastLine(Graphics2D g) {}
public void addPoint(int x,int y) {}
public void setPoint(int i,int x,int y) {
X.set(i,x);
Y.set(i,y);
}
public int getPointX(int i) {
return (int)(X.get(i));
}
public int getPointY(int i) {
return (int)(Y.get(i));
}
public int getLength() {
return X.size();
}
public int getWidth() {
return Math.abs(getPointX(0)-getPointX(1));
}
public int getHeight() {
return Math.abs(getPointY(0)-getPointY(1));
}
public int getMinX() {
return Math.min(getPointX(0),getPointX(1));
}
public int getMinY() {
return Math.min(getPointY(0),getPointY(1));
}
public int getMaxX() {
return Math.max(getPointX(0),getPointX(1));
}
public int getMaxY() {
return Math.max(getPointY(0),getPointY(1));
}
public String toString() {
return "未知二維圖形";
}
//設置對象的漸變色
public void setGradientPaint(Point p1,Point p2) {
try {
this.p1=p1;
this.p2=p2;
ptInterface=new GradientPaint(p1,color,p2,color2);
} catch (Exception e) {
System.out.println ("==============================");
System.out.println ("setGradientPaint(Point,Point)方法中出現異常: ");
}
}
//改變對象的大小
public void changeSize(int x,int y) {
//判斷二維圖形
if (isSquare) {
int w=x-getPointX(0);
int h=y-getPointY(0);
int s=Math.min(Math.abs(w),Math.abs(h));
if (w>0 && h>0)
addPoint(getPointX(0)+s,getPointY(0)+s);
else if (w>0 && h<0)
addPoint(getPointX(0)+s,getPointY(0)-s);
else if (w<0 && h>0)
addPoint(getPointX(0)-s,getPointY(0)+s);
else if (w<0 && h<0)
addPoint(getPointX(0)-s,getPointY(0)-s);
} else {
addPoint(x,y);
}
}
public void rotate(){
}
//移動對象
public void moveObject(int xBuff,int yBuff) {
for (int i=getLength()-1; i>=0; i--) {
setPoint(i,getPointX(i)+xBuff,getPointY(i)+yBuff);
}
if (gradientPaint>0) { //漸變填充處理
try {
p1=new Point(p1.x+xBuff,p1.y+yBuff);
p2=new Point(p2.x+xBuff,p2.y+yBuff);
setGradientPaint(p1,p2);
} catch (Exception e) { //沒有兩個點
System.out.println ("==============================");
System.out.println ("moveObject(int,int)方法出現異常:");
}
}
}
//畫對象獲取焦點的8個點
public void drawMoveFocus(Graphics2D g,Color backColor) {
AffineTransform af=g.getTransform();
if(rotateNum!=0)
{
g.translate(getMinX(),getMinY());
}
g.rotate(rotateNum);
int x1=getMinX();
int y1=getMinY();
int x2=getMaxX();
int y2=getMaxY();
int buff=0;
if(rotateNum!=0)
{
x2=x2-x1;
y2=y2-y1;
x1=0;y1=0;
}
if (!filled) {
buff=(int)(stroke/2)+1;
}
g.setStroke(new BasicStroke(1.0f));
g.setXORMode(backColor);
g.drawRect(x1-6-buff,y1-6-buff,5,5);
g.drawRect(x1-6-buff,y2+buff,5,5);
g.drawRect(x2+buff,y1-6-buff,5,5);
g.drawRect(x2+buff,y2+buff,5,5);
g.drawRect((x1+x2)/2-2-buff,y1-6-buff,5,5);
g.drawRect(x1-6-buff,(y1+y2)/2-2-buff,5,5);
g.drawRect((x1+x2)/2-2-buff,y2+buff,5,5);
g.drawRect(x2+buff,(y1+y2)/2-2-buff,5,5);
if (isChangablePoint(x1,y1))
g.fillRect(x1-5-buff,y1-5-buff,5,5);
else if (isChangablePoint(x1,y2))
g.fillRect(x1-5-buff,y2+buff,5,5);
else if (isChangablePoint(x2,y1))
g.fillRect(x2+buff,y1-5-buff,5,5);
else if (isChangablePoint(x2,y2))
g.fillRect(x2+buff,y2+buff,5,5);
// 繪制旋轉點
/* g.drawLine((x1+x2)/2-buff,y1-14-buff,(x1+x2)/2-buff,y1-7-buff);
g.setColor(new Color(0x22,0xB1,0x4C));
g.fillRect((x1+x2)/2-2-buff,y1-20-buff,6,6);*/
g.setXORMode(backColor);
if(rotateNum!=0)
g.setTransform(af);
}
//畫對象旋轉焦點的4個點
public void drawRotateFocus(Graphics2D g){
AffineTransform af=g.getTransform();
if(rotateNum!=0)
{
g.translate(getMinX(),getMinY());
}
g.rotate(rotateNum);
int x1=getMinX();
int y1=getMinY();
int x2=getMaxX();
int y2=getMaxY();
int buff=0;
if (!filled) {
buff=(int)(stroke/2)+1;
}
if(rotateNum!=0)
{
x2=x2-x1;
y2=y2-y1;
x1=0;
y1=0;
}
g.setColor(new Color(0x22,0xB1,0x4C));
g.fillRect(x1-6-buff,y1-6-buff,8,8);
g.drawRect(x1-6-buff,y2+buff,5,5);
g.drawRect(x2+buff,y1-6-buff,5,5);
g.drawRect(x2+buff,y2+buff,5,5);
if(rotateNum!=0)
g.setTransform(af);
// g.fillRect((x1+x2)/2-2-buff,y1-6-buff,5,5);
// g.fillRect(x1-6-buff,(y1+y2)/2-2-buff,5,5);
// g.fillRect((x1+x2)/2-2-buff,y2+buff,5,5);
// g.fillRect(x2+buff,(y1+y2)/2-2-buff,5,5);
}
protected boolean isChangablePoint(int x,int y) {
if (x==getPointX(1) && y==getPointY(1))
return true;
else
return false;
}
public void doDraw(Graphics2D g2d,Shape shape) {
AffineTransform af=g2d.getTransform();
Shape temp;
temp=shape;
if(rotateNum!=0)
{
g2d.translate(getMinX(),getMinY());
shape=shape2;
}
g2d.rotate(rotateNum);
if (filled) {
if (gradientPaint>0) {
g2d.setTransform(new AffineTransform());
g2d.setPaint(ptInterface);
} else {
g2d.setPaint(color);
}
//g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
//g2d.rotate(0);
g2d.fill(shape);
} else {
g2d.setPaint(color);
g2d.setStroke(new BasicStroke(stroke));
g2d.draw(shape);
}
if(rotateNum!=0)
{
g2d.setTransform(af);
shape=temp;
}
}
public Object clone() throws CloneNotSupportedException {
BaseObject bo = (BaseObject)super.clone();
//sheep = (Sheep)fold.sheep.clone();
X=(LinkedList<Integer>) bo.X.clone();
Y=(LinkedList<Integer>) bo.Y.clone();
return bo;
}
}
//星星類
class ObjPolygon extends BaseObject {
private int x1,y1,x2,y2;
private int xBase,yBase;
private int radians;
private int[] size={0,0};
private double increase;
public ObjPolygon() {
this(5);
}
public ObjPolygon(int rad) {
super();
radians=rad;
increase=Math.toRadians(360.0/(rad*2));
for (int i=0; i<radians*2; i++) {
X.add(0);
Y.add(0);
}
type=BaseObject.POLYGON;
}
public String toString() {
return "星星 "+String.valueOf(getLength()/2)+" 個角";
}
public void addPoint(int x,int y) {
xBase=x;
yBase=y;
}
public void moveObject(int xBuff,int yBuff) {
super.moveObject(xBuff,yBuff);
xBase+=xBuff;
yBase+=yBuff;
}
public void draw(Graphics2D g2d) {
int len=getLength();
int[] x=new int[len];
int[] y=new int[len];
for (int i=0; i<len; i++) {
x[i]=(Integer)(X.get(i));
y[i]=(Integer)(Y.get(i));
}
// g2d.drawPolyline(x,y,len);
shape=new Polygon(x,y,len);
/* */
doDraw(g2d,shape);
getXYs();
}
public void changeSize(int x,int y) {
size[1]=Math.max(Math.abs(x-xBase),Math.abs(y-yBase));
size[0]=size[1]/2;
double rad=0.0;
for (int i=0; i<radians*2; i++) {
X.set(i,(int)(size[i%2]*Math.cos(rad))+xBase);
Y.set(i,(int)(size[i%2]*Math.sin(rad))+yBase);
rad+=increase;
}
getXYs();
if (gradientPaint>0) {
setGradientPaint(new Point(getMinX(),getMinY()),new Point(getMaxX(),getMaxY()));
}
}
public int getMinX() {
return x1;
}
public int getMinY() {
return y1;
}
public int getMaxX() {
return x2;
}
public int getMaxY() {
return y2;
}
public boolean canResizeObject(int x,int y) {
int x2=getMaxX();
int y2=getMaxY();
if ((x>x2-4) && (y>y2-4) && (x<x2+4) && (y<y2+4))
return true;
else
return false;
}
private void getXYs() {
x1=getPointX(0);
y1=getPointY(0);
x2=x1;
y2=y1;
for (int i=0; i<getLength(); i++) {
int xx=getPointX(i);
int yy=getPointY(i);
if (xx<x1)
x1=xx;
if (yy<y1)
y1=yy;
if (xx>x2)
x2=xx;
if (yy>y2)
y2=yy;
}
}
protected boolean isChangablePoint(int x,int y) {
if (x==getMaxX() && y==getMaxY())
return true;
else
return false;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -