📄 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 + -