📄 oval.java
字号:
package test.paint;
import java.awt.*;
import java.awt.geom.Arc2D;
/**
* Oval类,实现画椭圆的功能
* 作者:王珍,钟雯
* 初始时间:2007 5-17
* 最后一次修改时间:2007 6-17
*/
public class Oval extends RectBoundedShape {
//标记是否填充
private boolean fill=false;
//画椭圆需要的参数,x,y起始点的坐标,w是长,h是高
int x, y, w, h;
/**
* 无参构造函数
* Call constuctor of RectBoundedShape
*/
public Oval() {
super();
}
/**
* 有模式参数构造函数
* Call constuctor of RectBoundedShape
*/
public Oval(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的值,是否需要填充
* 作者:钟雯
* 初始时间: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);
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.drawOval(x, y, w, h);
break;
case 1:
g.setColor(color.WHITE);
g.fillOval(x, y, w, h);
g.setColor( color );
g.drawOval(x, y, w, h);
break;
case 2:
g.fillOval(x, y, w, h);
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 Arc2D.Double getShape()
{
return new Arc2D.Double( x, y, w, h, 0,360,0);
}
/**
* 判断是否与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 + -