📄 shape.java
字号:
//Shape.java
import java.util.*;
import java.awt.*;
/**
* Shape
* @author
* @version 1.0 05/24/03
*
* Graphic is a compositer pattern,Picture is the note,and all others are leaves
*
*/
abstract class Graphic
{
abstract public void draw(Graphics g);
}
/**
* Picture is the note
*/
class Picture extends Graphic
{
private ArrayList gList = new ArrayList(1);
private int curNumber;
public void add(Graphic g) {
// First remove the objects after index curNumber,which is due to undo operation
if(gList.size() > curNumber)
for(int i=gList.size()-1; i>=curNumber; --i)
gList.remove(i);
gList.add(g);
++curNumber;
}
public Graphic getLast() {
if(curNumber>0)
return (Graphic)gList.get(curNumber-1);
else
return null;
}
// This is just for redo operation
public void add() {
if( gList.size()>curNumber )
++curNumber;
}
// This is just for undo operation
public void remove() {
if( curNumber>0 )
--curNumber;
}
public void clear() {
gList.clear();
curNumber = 0;
}
public void draw(Graphics g) {
for(int i=0; i<curNumber; ++i)
((Graphic)gList.get(i)).draw(g);
}
}
/**
* AShape is the base class of leaves: Line, Rect, FillRect, Oval, FilOval
*
* Every shape is built by two steps: setFirstPoint, setSecondPoint,
* which will be called by Tool,a builder pattern
*/
abstract class AShape extends Graphic implements Cloneable
{
protected int x1, y1, x2, y2;
protected Color c = null;
public AShape() {c = Color.black;}
public AShape(Color c) {this.c = c;}
public void setColor(Color c) {this.c = c;}
public Color getColor() {return c;}
public void setFirstPoint(int x, int y) {
x2=x1 = x; y2=y1 =y;
}
public void setSecondPoint(int x, int y) {
x2 = x; y2 =y;
}
public Object clone() {
Object obj = null;
try{
obj = super.clone();
} catch (CloneNotSupportedException e) {}
return obj;
}
}
/**
* The following classes are different just in the draw operation
*/
class Line extends AShape
{
public Line() {}
public Line(Color c) {super(c);}
public void draw(Graphics g) {
g.setColor(c);
g.drawLine(x1, y1, x2, y2);
}
}
class Rect extends AShape
{
public Rect() {}
public Rect(Color c) {super(c);}
public void draw(Graphics g) {
g.setColor(c);
g.drawRect(x2>x1?x1:x2, y2>y1?y1:y2, Math.abs(x2-x1)+1, Math.abs(y2-y1)+1);
}
}
class FillRect extends AShape
{
public FillRect() {}
public FillRect(Color c) {super(c);}
public void draw(Graphics g) {
g.setColor(c);
g.fillRect(x2>x1?x1:x2, y2>y1?y1:y2, Math.abs(x2-x1)+1, Math.abs(y2-y1)+1);
}
}
class Oval extends AShape
{
public Oval() {}
public Oval(Color c) {super(c);}
public void draw(Graphics g) {
g.setColor(c);
g.drawOval(x2>x1?x1:x2, y2>y1?y1:y2, Math.abs(x2-x1)+1, Math.abs(y2-y1)+1);
}
}
class FillOval extends AShape
{
public FillOval() {}
public FillOval(Color c) {super(c);}
public void draw(Graphics g) {
g.setColor(c);
g.fillOval(x2>x1?x1:x2, y2>y1?y1:y2, Math.abs(x2-x1)+1, Math.abs(y2-y1)+1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -