?? rectboundedshape.java
字號:
package test.paint;
import java.awt.*;
import java.awt.event.MouseEvent;
/**
* RectBoundedShape抽象類,實現MyShape接口
* 作者:王珍,鐘雯
* 初始時間:2007 5-17
* 最后一次修改時間:2007 6-17
*/
public abstract class RectBoundedShape implements MyShape {
//記錄所選夜色
protected Color color;
//記錄所選寬度
protected Stroke stroke;
//起始點和終止點
protected int startX, startY, endX, endY;
//圖形的模式
protected int model;
/**
* 無參構造函數
*/
protected RectBoundedShape() {
}
/**
* 不需要選三種模式的構造函數
* @param Color
* @param Stroke
* @param pointX
* @param pointY
*/
protected RectBoundedShape(Color c, Stroke s, int x, int y ) {
color = c;
stroke = s;
startX = endX = x;
startY = endY = y;
}
/**
* 需要三種模式的構造函數
* @param Color
* @param Stroke
* @param pointX
* @param pointY
* @param model
*/
protected RectBoundedShape(Color c, Stroke s, int x, int y, int z) {
color = c;
stroke = s;
startX = endX = x;
startY = endY = y;
model = z;
}
/**
* 當拖動鼠標時,得到pointX,pointY
*/
public void processCursorEvent(MouseEvent e, int t) {
if (t != MyShape.CURSOR_DRAGGED)
return;
int x = e.getX();
int y = e.getY();
if (e.isShiftDown()) {
regulateShape(x, y);
} else {
endX = x;
endY = y;
}
}
/**
*取得圖形的起始很終止點
*/
protected void regulateShape(int x, int y) {
int w = x - startX;
int h = y - startY;
int s = Math.min(Math.abs(w), Math.abs(h));
if (s == 0) {
endX = startX;
endY = startY;
} else {
endX = startX + s * (w / Math.abs(w));
endY = startY + s * (h / Math.abs(h));
}
}
/**
* 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( ":" );
return buffer.toString();
}
/**
* Set shapeData,聲明一個String數組,獲得文件中所保存的相關信息,當打開圖片的
* 時候,把數組中的內容還原,顯示所保存的圖片
* 作者:王珍,鐘雯
* 初始時間:2007 5-17
* 最后一次修改時間 2007 6-17
*/
public void setShapeData(String data) throws Exception {
String[] splits = data.split(":");
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] );
}
/**
* 設置是否需要填充
*/
public void setIsFill(boolean x)
{
}
/**
* 設置當前顏色
*/
public void setColor(Color col)
{
}
/**
* 判斷是否與x,y,w,h構成的矩形相交
* @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 false ;
}
/**
* 得到終止點的橫坐標
*/
public int getX()
{
return endX;
}
/**
* 得到終止點的縱坐標
*/
public int getY()
{
return endY;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -