📄 mypanel.java
字号:
*/
public void addShape( Object element) {
shapes.add( element );
undo.clear();
}
/**
* 保存文件时获取相关数据
* @return
*/
public String getShapes() {
int size = shapes.size();
StringBuffer buffer = new StringBuffer();
for (int i=0; i<size; i++) {
MyShape shape = (MyShape) shapes.get(i);
if(shape.isImage())
{
((CusImage)shape).setSaveImage();
}
buffer.append("\n");
buffer.append(shape.getClass().getName());
buffer.append("\t");
buffer.append(shape.getShapeData());
}
return buffer.toString();
}
/**
* 打开文件时设置图片的相关数据
* @param list
* @throws Exception
*/
public void setShapes(ArrayList list) throws Exception {
try {
int size = list.size();
for (int i=0; i<size; i++) {
String[] split2 = ((String) list.get(i)).split("\t");
MyShape shape;
if (split2[0].equals("test.paint.Line")) {
shape = new Line();
} else if (split2[0].equals("test.paint.Rect")) {
shape = new Rect();
} else if (split2[0].equals("test.paint.Oval")) {
shape = new Oval();
} else if (split2[0].equals("test.paint.Diamond")) {
shape = new Diamond();
} else if (split2[0].equals("test.paint.PolyLine")) {
shape = new PolyLine();
} else if (split2[0].equals("test.paint.Eraser")) {
shape = new Eraser(this);
} else if (split2[0].equals("test.paint.Word")) {
shape = new Word();
} else if (split2[0].equals("test.paint.RoundRect")){
shape = new RoundRect();
}
else {
throw new Exception("Invalid Shape Data!");
}
shape.setShapeData(split2[1]);
list.set(i, shape);
}
shapes = list;
repaint();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int size = shapes.size();
Graphics2D g2d = (Graphics2D) g;
for (int i=0; i<size; i++) {
((MyShape) shapes.get(i)).draw(g2d);
}
}
/**
* 松开鼠标时产生的事件
*/
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1 ) {
switch (tool) {
case TOOL_LINE:
flag=0;
currentShape = new Line(this.getForeground(),
STROKES[strokeIndex], e.getX(), e.getY());
break;
case TOOL_RECT:
flag=0;
currentShape = new Rect(this.getForeground(),
STROKES[strokeIndex], e.getX(), e.getY(), m );
break;
case TOOL_OVAL:
flag=0;
currentShape = new Oval(this.getForeground(),
STROKES[strokeIndex], e.getX(), e.getY(), m );
break;
case TOOL_DIAMOND:
flag=0;
currentShape = new Diamond(this.getForeground(),
STROKES[strokeIndex], e.getX(), e.getY(),m );
break;
case TOOL_PENCIL:
flag=0;
currentShape = new PolyLine(this.getForeground(),
STROKES[strokeIndex], e.getX(), e.getY(),0,1 );
break;
case TOOL_ERASER:
flag=0;
currentShape = new Eraser(this, ERASER_STROKES[eraserIndex],
e.getX(), e.getY() );
break;
case TOOL_POLYGON:
point.addPoint( e.getX(), e.getY() );
if(flag==0)
{
currentShape = new Line(this.getForeground(),
STROKES[strokeIndex], e.getX(), e.getY());
flag++;
s1=e.getX();
s2=e.getY();
j=1;
}
else
{
if(j!=0)
{
float t1 = Math.abs( e.getX() - s1 );
float t2 = Math.abs( e.getY() - s2 );
if(t1 <= 3 && t2 <= 3)
{
flag=0;
j=0;
}
int i=shapes.size()-1;
int x=( (MyShape) shapes.get(i)).getX();
int y=( (MyShape) shapes.get(i)).getY();
currentShape = new Line(this.getForeground(),
STROKES[strokeIndex], x, y, e.getX(), e.getY());
}
}
break;
case TOOL_WORD:
flag=0;
currentShape = new Word(this.getForeground(),
STROKES[strokeIndex], e.getX(), e.getY(), x1, x2, x3, temp );
break;
case TOOL_SQUARE1:
flag=0;
currentShape = new PolyLine(this.getForeground(),
STROKES[2], e.getX(), e.getY(), 0,1 );
break;
case TOOL_SQUARE2:
flag=0;
currentShape = new PolyLine(this.getForeground(),
STROKES[4], e.getX(), e.getY(),0, 1 );
break;
case TOOL_SQUARE3:
flag=0;
currentShape = new PolyLine(this.getForeground(),
STROKES[5], e.getX(), e.getY(), 0,1 );
break;
case TOOL_CIRCLE1:
flag=0;
currentShape = new PolyLine(this.getForeground(),
STROKES[strokeIndex], e.getX(), e.getY(), 4, 0 );
break;
case TOOL_CIRCLE2:
flag=0;
currentShape = new PolyLine(this.getForeground(),
STROKES[strokeIndex], e.getX(), e.getY(), 10, 0 );
break;
case TOOL_CIRCLE3:
flag=0;
currentShape = new PolyLine(this.getForeground(),
STROKES[strokeIndex], e.getX(), e.getY(), 18, 0 );
break;
case TOOL_FILL:
flag=0;
shapeID=getIntersectsShape(e.getX(), e.getY(), 1, 1);
if (shapeID!= -1)
fillShape( shapeID, Color.black );
fillShape( shapeID, this.getForeground() );
currentShape = (MyShape) shapes.get( shapeID );
break;
case TOOL_ROUNDRECT:
flag = 0;
currentShape = new RoundRect(this.getForeground(),STROKES[strokeIndex],e.getX(),e.getY(),m);
break;
}
shapes.add(currentShape);
repaint();
} else if (e.getButton() == MouseEvent.BUTTON3 && currentShape != null) {
currentShape.processCursorEvent(e, MyShape.RIGHT_PRESSED);
repaint();
}
}
public void mouseDragged(MouseEvent e) {
if (currentShape != null) {
currentShape.processCursorEvent(e, MyShape.CURSOR_DRAGGED);
repaint();
}
}
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1 && currentShape != null) {
currentShape.processCursorEvent(e, MyShape.LEFT_RELEASED);
currentShape = null;
repaint();
}
}
public int getID()
{
return shapeID;
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -