📄 freeshape.java
字号:
import java.awt.*;
import java.awt.event.MouseEvent;
/**
*
* @author hysun
*/
public class FreeShape implements IShape {
protected Color color;
protected Stroke stroke;
protected PointsSet pointsSet;
protected FreeShape(Color c, Stroke s, int x, int y) {
this();
color = c;
stroke = s;
pointsSet.addPoint(x, y);
}
protected FreeShape() {
pointsSet = new PointsSet(50);
}
public void processCursorEvent(MouseEvent e, int t) {
if (t != IShape.CURSOR_DRAGGED)
return;
pointsSet.addPoint(e.getX(), e.getY());
}
/**
* This method is only used by PolyLine in this application, so it can be
* put in the implementation of PolyLine instead of here. However, considering
* further extension by adding subclasses to the FreeShape class, i choose
* to put it here.
*/
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);
int[][] ps = pointsSet.getPoints();
for (int i=0; i<ps[0].length; i++) {
buffer.append(":");
buffer.append(ps[0][i]);
buffer.append(":");
buffer.append(ps[1][i]);
}
return buffer.toString();
}
/**
* This method is only used by PolyLine in this application, so it can be
* put in the implementation of PolyLine instead of here. However, considering
* further extension by adding subclasses to the FreeShape class, i choose
* to put it here.
*/
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])];
for (int i=2; i<splits.length; i+=2) {
pointsSet.addPoint(Integer.parseInt(splits[i]),
Integer.parseInt(splits[i+1]));
}
}
public Point getSouthEastMostPoint() {
int[][] points = pointsSet.getPoints();
if (points == null)
return new Point(0, 0);
int mx=0, my=0;
for (int i=0; i<points[0].length; i++) {
if (points[0][i] > mx)
mx = points[0][i];
if (points[1][i] > my)
my = points[1][i];
}
return new Point(mx, my);
}
public void draw(Graphics2D g) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -