📄 eraser.java
字号:
package test.paint;
import test.paint.*;
import java.awt.*;
import javax.swing.JComponent;
/**
*Eraser类,实现橡皮擦的功能
*作者:曾燕秋
*初始时间:2007 5-17
*最后一次修改时间:2007 6-17
*
*/
public class Eraser extends ResShape {
//声明一个JComponet对象,获取当前的背景色
private JComponent board;
/**
* 有参构造函数
*/
public Eraser(JComponent board, Stroke s, int x, int y) {
super(null, s, x, y,0,0);
this.board = board;
}
/**
* 无参构造函数
*/
public Eraser(JComponent board) {
this.board = board;
}
/**
* 用当前背景色画线作为橡皮擦的功能
*/
public void draw(Graphics2D g) {
g.setColor(board.getBackground());
g.setStroke(stroke);
int[][] points = pointsSet.getPoints();
if (points == null)
return;
int s = points[0].length;
if (s == 1) {
int x = points[0][0];
int y = points[1][0];
g.drawLine(x, y, x, y);
} else {
g.drawPolyline(points[0], points[1], s);
}
}
/**
* Get shapeData,声明一个StringBuffer变量,把图片相关信息加入StringBuffer
* 临时变量中,用于保存图片信息
* 初始时间:2007 5-17
* 最后一次修改时间 2007 6-17
*/
public String getShapeData() {
int si = 0;
for (int i=0; i<MyPanel.ERASER_STROKES.length; i++) {
if (stroke == MyPanel.ERASER_STROKES[i]) {
si = i;
break;
}
}
StringBuffer buffer = new StringBuffer();
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();
}
/**
* Set shapeData,声明一个String数组,获得文件中所保存的相关信息,当打开图片的
* 时候,把数组中的内容还原,显示所保存的图片
* 初始时间:2007 5-17
* 最后一次修改时间 2007 6-17
*/
public void setShapeData(String data) throws Exception {
String splits[] = data.split(":");
stroke = MyPanel.ERASER_STROKES[Integer.parseInt(splits[0])];
for (int i=1; i<splits.length; i+=2) {
pointsSet.addPoint(Integer.parseInt(splits[i]),
Integer.parseInt(splits[i+1]));
}
}
public void setIsFill(boolean x) {
// TODO Auto-generated method stub
}
public void setColor(Color col) {
// TODO Auto-generated method stub
}
public boolean intersects(double x, double y, double w, double h) {
// TODO Auto-generated method stub
return false;
}
public Rectangle getBounds() {
// TODO Auto-generated method stub
return null;
}
public int getX() {
// TODO Auto-generated method stub
return 0;
}
public int getY() {
// TODO Auto-generated method stub
return 0;
}
/**
* 判断是否是图形
*/
public boolean isImage() {
// TODO Auto-generated method stub
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -