?? diamond.java
字號:
package test.paint;
import java.awt.*;
/**
* Diamond類,實現畫菱形的功能
* 作者:王珍,鐘雯
* 初始時間:2007 5-17
* 最后一次修改時間:2007 6-17
*/
public class Diamond extends RectBoundedShape {
//記錄菱形四個點的橫坐標
private int[] xS;
//記錄菱形四個點的縱坐標
private int[] yS;
//標記是否需要填充
private boolean fill = false;
//聲明一個Polygon對象
private Polygon polyShape = new Polygon();
/**
*無參構造函數
*/
public Diamond() {
super();
xS = new int[4];
yS = new int[4];
}
/**
* 有參構造函數
*/
public Diamond(Color c, Stroke s, int x, int y, int z) {
super(c, s, x, y, z);
xS = new int[4];
yS = new int[4];
}
/**
* 設置當前顏色
*/
public void setColor(Color col)
{
this.color=col;
}
/**
* 設置fill的值,是否需要填充
* 作者:鐘雯
* 初始時間:2007 5-17
* 最后一次修改時間:2007 5-17
*/
public void setIsFill(boolean isFill)
{
fill=isFill;
}
/**
* 得到fill的值
* @return boolean
* 作者:鐘雯
* 初始時間:2007 5-17
* 最后一次修改時間:2007 5-17
*/
public boolean getIsFill()
{
return fill;
}
/**
* Draw method得到當前顏色,得到當前寬度,得到菱形的四個點坐標,
* 根據四個點畫一個多邊形,即得到菱形
* 作者:王珍
* 初始時間:2007 5-17
* 最后一次修改時間:2007 6-17
*/
public void draw(Graphics2D g) {
g.setColor(color);
g.setStroke(stroke);
int x, y, w, h;
if (startX > endX) {
x = endX;
w = startX - endX;
} else {
x = startX;
w = endX - startX;
}
if (startY > endY) {
y = endY;
h = startY - endY;
} else {
y = startY;
h = endY - startY;
}
//菱形四個點的坐標
xS[0] = x + w/2;
yS[0] = y;
xS[1] = x + w;
yS[1] = y + h/2;
xS[2] = x + w/2;
yS[2] = y + h;
xS[3] = x;
yS[3] = y + h/2;
//畫圖函數的三種模式
switch( model )
{
case 0:
g.drawPolygon(xS, yS, 4);
break;
case 1:
g.setColor(color.WHITE);
g.fillPolygon(xS, yS, 4);
g.setColor( color );
g.drawPolygon(xS, yS, 4);
break;
case 2:
g.fillPolygon(xS, yS, 4);
break;
}
//填充
if(fill)
{
g.fill(getShape());
}
}
/**
* 得到圖形邊界
* 作者:鐘雯
* 初始時間:2007 5-17
* 最后一次修改時間 2007 6-17
* @return Rectangle
*/
public Rectangle getBounds() {
return getShape().getBounds();
}
/**
* 得到多邊形形狀
* 作者:鐘雯
* 初始時間:2007 5-17
* 最后一次修改時間 2007 6-17
* @return Polygon
*/
public Polygon getShape()
{
return new Polygon(xS, yS, 4 );
}
/**
* 判斷是否與x,y,w,h構成的矩形相交
* 作者:鐘雯
* 初始時間:2007 5-17
* 最后一次修改時間 2007 6-17
* @param x double
* @param y double
* @param w double
* @param h double
* @return boolean
*/
public boolean intersects(double x,double y,double w,double h)
{
return this.getBounds().intersects(x,y,w,h);
}
public boolean isImage() {
// TODO Auto-generated method stub
return false;
}
/**
* Get shapeData,聲明一個StringBuffer變量,把菱形相關信息加入StringBuffer
* 臨時變量中,用于保存菱形的相關信息
* 作者:王珍,鐘雯
* 初始時間:2007 5-17
* 最后一次修改時間 2007 6-17
*/
public String getShapeData() {
int si = 0;
for (int i=0; i<MyPanel.STROKES.length; i++) {
if (stroke == MyPanel.STROKES[i]) {
si = i;
break;
}
}
StringBuffer buffer = new StringBuffer();
buffer.append(color.getRGB());
buffer.append(":");
buffer.append(si);
buffer.append(":");
buffer.append(startX);
buffer.append(":");
buffer.append(startY);
buffer.append(":");
buffer.append(endX);
buffer.append(":");
buffer.append(endY);
buffer.append( ":" );
buffer.append( model );
buffer.append( ":" );
buffer.append(fill);
buffer.append(":");
return buffer.toString();
}
/**
* Set shapeData,聲明一個String數組,獲得文件中所保存的相關信息,當打開圖片的
* 時候,把數組中的內容還原,顯示所保存的菱形
* 作者:王珍,鐘雯
* 初始時間:2007 5-17
* 最后一次修改時間 2007 6-17
*/
public void setShapeData(String data) throws Exception {
String[] splits = data.split(":");
String flag;
color = new Color(Integer.parseInt(splits[0]));
stroke = MyPanel.STROKES[Integer.parseInt(splits[1])];
startX = Integer.parseInt(splits[2]);
startY = Integer.parseInt(splits[3]);
endX = Integer.parseInt(splits[4]);
endY = Integer.parseInt(splits[5]);
model = Integer.parseInt(splits[6]);
flag = splits[7];
if(flag.equals("false"))
fill=false;
else
fill=true;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -