?? mypanel.java
字號(hào):
package test.paint;
import java.awt.*;
import java.awt.event.*;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.*;
import javax.swing.undo.*;
import java.util.ArrayList;
import javax.swing.filechooser.*;
/**
* 設(shè)置畫(huà)板
*
*/
/**
* 作者:鐘雯 王珍 曾燕秋
* 實(shí)現(xiàn)畫(huà)板畫(huà)圖的各項(xiàng)功能
* 初始時(shí)間:2007 5-17
* 最后一次修改時(shí)間:2007 6-17
*/
public class MyPanel extends JPanel implements
MouseListener, MouseMotionListener {
//flag判斷多邊形是否是第一筆畫(huà)
public int flag=0;
//s,s2分別記錄多邊形第一個(gè)點(diǎn)的橫,縱坐標(biāo),j=0代表第一次畫(huà)多邊形
public int s1, s2, j=1;
public static final int TOOL_LINE = 0;
public static final int TOOL_RECT = 1;
public static final int TOOL_OVAL = 2;
public static final int TOOL_DIAMOND = 3;
public static final int TOOL_PENCIL = 4;
public static final int TOOL_ERASER = 5;
public static final int TOOL_POLYGON = 6;
public static final int TOOL_WORD = 7;
public static final int MODEL1 = 0;
public static final int MODEL2 = 1;
public static final int MODEL3 = 2;
public static final int TOOL_SQUARE1=8;
public static final int TOOL_SQUARE2=9;
public static final int TOOL_SQUARE3=10;
public static final int TOOL_CIRCLE1=11;
public static final int TOOL_CIRCLE2=12;
public static final int TOOL_CIRCLE3=13;
public static final int TOOL_FILL = 14;
public static final int TOOL_ROUNDRECT = 15;
//public MyPanel myPanel;
//聲明Stroke的大小
public static final Stroke[] STROKES = new Stroke[] {
new BasicStroke(1.0f),
new BasicStroke(2.0f),
new BasicStroke(5.0f),
new BasicStroke(7.5f),
new BasicStroke(10.0f),
new BasicStroke(15.0f)
};
//定義ERASER_STROKE的大小
public static final Stroke[] ERASER_STROKES = new Stroke[] {
new BasicStroke(15.0f),
new BasicStroke(20.0f),
new BasicStroke(30.0f),
new BasicStroke(50.0f),
new BasicStroke(100.0f)
};
//聲明undo,用于撤消
private ArrayList undo = new ArrayList();
private ArrayList shapes;
//定義PointSet的對(duì)象
private PointsSet point;
private MyShape currentShape;
private int tool;
private int x1, x2, x3 = 7;
//m用于模板
private int m;
private String temp ;
private int shapeID;
private int strokeIndex, eraserIndex;
public MyPanel() {
point = new PointsSet();
shapes = new ArrayList();
tool = TOOL_LINE;
currentShape = null;
strokeIndex = 0;
eraserIndex = 0;
x1 =0;
x2 =0;
temp = "宋體";
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
setOpaque(true);
setForeground(Color.black);
setBackground(Color.white);
addMouseListener(this);
addMouseMotionListener(this);
}
public int getIntersectsShape(double x, double y, double w, double h) {
for (int i = shapes.size() - 1; i >= 0; i--) {
if ( ( (MyShape)shapes.get(i)).intersects(x, y, w, h)) {
return i;
}
}
return -1;
}
public void fillShape(int index, Color col) {
( (MyShape) shapes.get(index)).setIsFill(true);
( (MyShape)shapes.get(index)).setColor(col);
repaint();
}
/**
* 撤消
*
*/
public void unDo() {
if ( canUnDo()) {
undo.add(shapes.remove(shapes.size() - 1));
//rePaintArea();
repaint();
}
}
/**
* 判斷是否可以撤消
* @return
*/
public boolean canUnDo() {
return !shapes.isEmpty();
}
/**
* 重做
*/
public void reDo() {
if (canReDo()) {
shapes.add(undo.remove(undo.size() - 1));
//rePaintArea();
repaint();
}
}
/**
* 判斷是否可以重做
* @return
*/
public boolean canReDo(){
return !undo.isEmpty();
}
/**
* 判斷畫(huà)布是否為空
* @return boolean
*/
public boolean isEmpty()
{
return shapes.isEmpty();
}
public void setTool(int t) {
if (t < TOOL_LINE || t > TOOL_ROUNDRECT )
throw new IllegalArgumentException("Invaild Tool Specified!");
tool = t;
}
public void setModel(int n)
{
if( n < 0 || n > 2)
throw new IllegalArgumentException("Invaild Tool Specified!");
m = n;
}
public void setStyle1(String x)
{
temp = x;
}
public void setStyle2(int a, int b)
{
x1 = a;
x2 = b;
}
public void setNumber(int c)
{
x3 = c;
}
public void setStrokeIndex(int i) {
if (i < 0 || i > 5)
throw new IllegalArgumentException("Invaild Weight Specified!");
strokeIndex = i;
}
/**
* 返回選中的橡皮擦大小
* @param i
*/
public void setEraserIndex(int i) {
if (i < 0 || i > 4)
throw new IllegalArgumentException("Invaild Size Specified!");
eraserIndex = i;
}
/**
* 清除畫(huà)板
*
*/
public void clearBoard() {
shapes.clear();
repaint();
flag = 0;
}
/**
*
* @param element
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -