📄 roundrect.java
字号:
package test.paint;
import java.awt.*;
import java.awt.geom.*;
/**
* RoundRect类,实现画圆角矩形的类
* 作者:王珍,钟雯
* 初始时间:2007 5-17
* 最后一次修改时间:2007 6-17
*/
public class RoundRect extends RectBoundedShape {
//标记是否填充
private boolean fill=false;
//画圆角矩形需要的参数,x,y起始点的坐标,w是长,h是高
int x, y, w, h;
/**
* 无参构造函数
* Call constuctor of RectBoundedShape
*/
public RoundRect() {
super();
}
/**
* 有模式参数构造函数
* Call constuctor of RectBoundedShape
*/
public RoundRect(Color c, Stroke s, int x, int y, int z) {
super(c, s, x, y, z);
}
/**
* 设置当前颜色
*/
public void setColor(Color col)
{
this.color = col ;
}
/**
* 设置fill的值,是否需要填充
*/
public void setIsFill(boolean isFill)
{
fill=isFill;
}
/**
* 得到fill的值
* @return boolean
*/
public boolean getIsFill()
{
return fill;
}
/**
* Draw method 得到当前颜色,得到当前宽度,根据三种模式画圆角矩形
* 作者:王珍
* 初始时间:2007 5-17
* 最后一次修改时间:2007 6-17
*/
public void draw(Graphics2D g) {
g.setColor(color);
g.setStroke(stroke);
if (startX > endX) {
x = endX;
w = startX - endX;
}
else {
x = startX;
w = endX - startX;
}
if (startY > endY) {
y = endY;
h = startY - endY;
}
else {
y = startY;
h = endY - startY;
}
//根据模式画图
switch( model )
{
case 0:
g.drawRoundRect(x, y, w, h,50,50);
break;
case 1:
g.setColor(color.WHITE);
g.fillRoundRect(x, y, w, h, 50, 50);
g.setColor( color );
g.drawRoundRect(x, y, w, h, 50, 50);
break;
case 2:
g.fillRoundRect(x, y, w, h, 50, 50);
break;
}
//如果fill为true,则填充
if(fill)
{
g.fill(getShape());
}
}
/**
* 得到边界
* 作者:钟雯
* 初始时间:2007 5-17
* 最后一次修改时间:2007 6-17
* @return Rectangle
*/
public Rectangle getBounds() {
return getShape().getBounds();
}
/**
* 得到圆角矩形
* 作者:钟雯
* 初始时间:2007 5-17
* 最后一次修改时间:2007 6-17
* @return Double
*/
public RoundRectangle2D.Double getShape(){
return new RoundRectangle2D.Double(x, y, w, h, 50, 50);
}
/**
* 判断是否与x,y,w,h构成的矩形相交
* 作者:钟雯
* 初始时间:2007 5-17
* 最后一次修改时间:2007 6-17
* @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 this.getBounds().intersects(x,y,w,h);
}
public boolean isImage() {
// TODO Auto-generated method stub
return false;
}
/**
* 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( ":" );
buffer.append(fill);
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(":");
String flag;
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]);
flag = splits[7];
if(flag.equals("false"))
fill=false;
else
fill=true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -