📄 diamond.java
字号:
package test.paint;
import java.awt.*;
/**
* Diamond类,实现画菱形的功能
* 作者:王珍,钟雯
* 初始时间:2007 5-17
* 最后一次修改时间:2007 6-17
*/
public class Diamond extends RectBoundedShape {
//记录菱形四个点的横坐标
private int[] xS;
//记录菱形四个点的纵坐标
private int[] yS;
//标记是否需要填充
private boolean fill = false;
//声明一个Polygon对象
private Polygon polyShape = new Polygon();
/**
*无参构造函数
*/
public Diamond() {
super();
xS = new int[4];
yS = new int[4];
}
/**
* 有参构造函数
*/
public Diamond(Color c, Stroke s, int x, int y, int z) {
super(c, s, x, y, z);
xS = new int[4];
yS = new int[4];
}
/**
* 设置当前颜色
*/
public void setColor(Color col)
{
this.color=col;
}
/**
* 设置fill的值,是否需要填充
* 作者:钟雯
* 初始时间:2007 5-17
* 最后一次修改时间:2007 5-17
*/
public void setIsFill(boolean isFill)
{
fill=isFill;
}
/**
* 得到fill的值
* @return boolean
* 作者:钟雯
* 初始时间:2007 5-17
* 最后一次修改时间:2007 5-17
*/
public boolean getIsFill()
{
return fill;
}
/**
* Draw method得到当前颜色,得到当前宽度,得到菱形的四个点坐标,
* 根据四个点画一个多边形,即得到菱形
* 作者:王珍
* 初始时间:2007 5-17
* 最后一次修改时间:2007 6-17
*/
public void draw(Graphics2D g) {
g.setColor(color);
g.setStroke(stroke);
int x, y, w, h;
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;
}
//菱形四个点的坐标
xS[0] = x + w/2;
yS[0] = y;
xS[1] = x + w;
yS[1] = y + h/2;
xS[2] = x + w/2;
yS[2] = y + h;
xS[3] = x;
yS[3] = y + h/2;
//画图函数的三种模式
switch( model )
{
case 0:
g.drawPolygon(xS, yS, 4);
break;
case 1:
g.setColor(color.WHITE);
g.fillPolygon(xS, yS, 4);
g.setColor( color );
g.drawPolygon(xS, yS, 4);
break;
case 2:
g.fillPolygon(xS, yS, 4);
break;
}
//填充
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 Polygon
*/
public Polygon getShape()
{
return new Polygon(xS, yS, 4 );
}
/**
* 判断是否与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 + -