?? roundrect.java
字號(hào):
package test.paint;
import java.awt.*;
import java.awt.geom.*;
/**
* RoundRect類,實(shí)現(xiàn)畫圓角矩形的類
* 作者:王珍,鐘雯
* 初始時(shí)間:2007 5-17
* 最后一次修改時(shí)間:2007 6-17
*/
public class RoundRect extends RectBoundedShape {
//標(biāo)記是否填充
private boolean fill=false;
//畫圓角矩形需要的參數(shù),x,y起始點(diǎn)的坐標(biāo),w是長(zhǎng),h是高
int x, y, w, h;
/**
* 無(wú)參構(gòu)造函數(shù)
* Call constuctor of RectBoundedShape
*/
public RoundRect() {
super();
}
/**
* 有模式參數(shù)構(gòu)造函數(shù)
* Call constuctor of RectBoundedShape
*/
public RoundRect(Color c, Stroke s, int x, int y, int z) {
super(c, s, x, y, z);
}
/**
* 設(shè)置當(dāng)前顏色
*/
public void setColor(Color col)
{
this.color = col ;
}
/**
* 設(shè)置fill的值,是否需要填充
*/
public void setIsFill(boolean isFill)
{
fill=isFill;
}
/**
* 得到fill的值
* @return boolean
*/
public boolean getIsFill()
{
return fill;
}
/**
* Draw method 得到當(dāng)前顏色,得到當(dāng)前寬度,根據(jù)三種模式畫圓角矩形
* 作者:王珍
* 初始時(shí)間:2007 5-17
* 最后一次修改時(shí)間: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;
}
//根據(jù)模式畫圖
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());
}
}
/**
* 得到邊界
* 作者:鐘雯
* 初始時(shí)間:2007 5-17
* 最后一次修改時(shí)間:2007 6-17
* @return Rectangle
*/
public Rectangle getBounds() {
return getShape().getBounds();
}
/**
* 得到圓角矩形
* 作者:鐘雯
* 初始時(shí)間:2007 5-17
* 最后一次修改時(shí)間:2007 6-17
* @return Double
*/
public RoundRectangle2D.Double getShape(){
return new RoundRectangle2D.Double(x, y, w, h, 50, 50);
}
/**
* 判斷是否與x,y,w,h構(gòu)成的矩形相交
* 作者:鐘雯
* 初始時(shí)間:2007 5-17
* 最后一次修改時(shí)間: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,聲明一個(gè)StringBuffer變量,把圓角矩形相關(guān)信息加入StringBuffer
* 臨時(shí)變量中,用于保存圓角矩形的相關(guān)信息
* 作者:王珍,鐘雯
* 初始時(shí)間:2007 5-17
* 最后一次修改時(shí)間 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,聲明一個(gè)String數(shù)組,獲得文件中所保存的相關(guān)信息,當(dāng)打開圖片的
* 時(shí)候,把數(shù)組中的內(nèi)容還原,顯示所保存的圓角矩形
* 作者:王珍,鐘雯
* 初始時(shí)間:2007 5-17
* 最后一次修改時(shí)間 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;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -