📄 rectboundedshape.java
字号:
import java.awt.*;
import java.awt.event.MouseEvent;
public class RectBoundedShape implements IShape {
protected Color color;
protected Stroke stroke;
protected int startX, startY, endX, endY;
protected RectBoundedShape(Color c, Stroke s, int x, int y) {
color = c;
stroke = s;
startX = endX = x;
startY = endY = y;
}
protected RectBoundedShape() {
}
public void processCursorEvent(MouseEvent e, int t) {
if (t != IShape.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));
}
}
public String getShapeData() {
int si = 0;
for (int i=0; i<DrawingBoard.STROKES.length; i++) {
if (stroke == DrawingBoard.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);
return buffer.toString();
}
public void setShapeData(String data) throws Exception {
String[] splits = data.split(":");
color = new Color(Integer.parseInt(splits[0]));
stroke = DrawingBoard.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]);
}
public void draw(Graphics2D g) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -