程序大概工作原理：

1． 新建一个画图区域，生成一个 FreePanel 的实例对象。

2． 鼠标点击工具栏的，记住点击的内容(图形或颜色)，记录到当前状态。

3． 鼠标在画图区域内按下鼠标，移动，再抬起，生成一个 Specific 对象(记录起止位置和颜色)，根据当前状态(图形)把这个 Specific 对象放入对应的 Vector(矩形，椭圆，直线等)中。

4． 使用 repaint()方法来重新绘制整个画图区域，具体参考 FreePanel.paintComponent()方法，就是把所有 Vector 中的内容重新绘制一遍

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.Cursor;

import java.awt.Graphics;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;

import java.util.Vector;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JCheckBoxMenuItem;

import javax.swing.JColorChooser;

import javax.swing.JDesktopPane;

import javax.swing.JFrame;

import javax.swing.JInternalFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class PaintIt extends JFrame {

private JButton DrawRectBut, DrawBut, ClearBut, DrawOvalBut, DrawLineBut,

RectBut, OvalBut, ColorBut, EOvalBut, ERectBut;

private int mode = 0, toolVisible = 1, statusVisible = 1, CurSet = 0;

private JPanel panel, StatusPanel, RectPanel, OvalPanel;

private JLabel label, label2, label3, label4, label5;

private JDesktopPane desktop;

private Color color = Color.BLACK;

private Cursor Cur = new Cursor(1), Cur1 = new Cursor(0), Cur2 = new Cursor(

12);

// ----------储存画图鼠标按下和抬起的起止点，和颜色---------------------------------------------

private class Specific {

private int x1, y1, x2, y2;

private Color col;

public Specific(int a1, int b1, int a2, int b2, Color c) {

x1 = a1;

y1 = b1;

x2 = a2;

y2 = b2;

col = c;

}

public int gX1() {

return x1;

}

public int gY1() {

return y1;

}

public int gX2() {

return x2;

}

public int gY2() {

return y2;

}

public Color gCol() {

return col;

}

} // 内部类结束 Specific

// ----------一个画板，所有画的图形都在这里边----------------

private class FreePanel extends JPanel {

private int X1, X2, Y1, Y2, LX1, LX2, LY1, LY2, height, width, topX, topY,

ptCounter, PolyCounter = 0;

// 记录所有的画过的图形：里面的元素都是Specific的实例

private Vector RectVec, fRectVec, OvalVec, fOvalVec, freeVec, LineVec;

public FreePanel() {

RectVec = new Vector();

fRectVec = new Vector();

OvalVec = new Vector();

fOvalVec = new Vector();

freeVec = new Vector();

LineVec = new Vector();

// 在一个画板内加入侦听鼠标动作的内部内部匿名类

addMouseMotionListener(new MouseMotionListener() {

public void mouseDragged(MouseEvent act2) {// 侦听按下鼠标后移动

X2 = act2.getX();

Y2 = act2.getY();

if (mode == 6) {// 自由画线模式，每移动一下，就往freeVec放入一个点

LX1 = LX2;

LY1 = LY2;

LX2 = X2;

LY2 = Y2;

freeVec.add(new Specific(LX1, LY1, LX2, LY2, color));

}

// 重画图形

repaint();

// 修改状态栏

label.setText(" 鼠标位置 ( X: " + act2.getX() + " , Y: " + act2.getY()

+ " )");

}

public void mouseMoved(MouseEvent act3) {// 侦听鼠标移动

// 修改状态栏

label.setText(" 鼠标位置 ( X: " + act3.getX() + " , Y: " + act3.getY()

+ " )");

}

});// 内部匿名类结束

addMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent act1) {// 按下鼠标时

LX1 = LX2 = X1 = act1.getX();

LY1 = LY2 = Y1 = act1.getY();

}

public void mouseReleased(MouseEvent act2) {// 松开鼠标时候

if (mode == 1) // 画实心矩形模式

fRectVec.add(new Specific(X1, Y1, act2.getX(), act2.getY(), color));

if (mode == 2) // 画实心矩形椭圆

fOvalVec.add(new Specific(X1, Y1, act2.getX(), act2.getY(), color));

if (mode == 3) // 画空心矩形

RectVec.add(new Specific(X1, Y1, act2.getX(), act2.getY(), color));

if (mode == 4) // 画空心圆

OvalVec.add(new Specific(X1, Y1, act2.getX(), act2.getY(), color));

if (mode == 5) // 画线

LineVec.add(new Specific(X1, Y1, act2.getX(), act2.getY(), color));

}

public void mouseEntered(MouseEvent e) {// 鼠标进入画板区域

if (CurSet == 1)

setCursor(Cur);// 画椭圆和矩形时，使用“+”样的光标

else if (CurSet == 2)

setCursor(Cur2);// 自由图画和画线时，使用“手”样的光标

}

public void mouseExited(MouseEvent e) {// 鼠标离开画板区域

setCursor(Cur1);// 恢复鼠标箭头形式

}

});// 内部匿名类结束

// 清除画板，实际就是清除所有Vector中的内容，最后在重绘

ClearBut.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

RectVec.removeAllElements();

fRectVec.removeAllElements();

OvalVec.removeAllElements();

fOvalVec.removeAllElements();

freeVec.removeAllElements();

LineVec.removeAllElements();

CurSet = 0;

mode = 0;

repaint();

}

});

}

// 根据Vector中的数据内容，绘制画板的图形

public void paintComponent(Graphics g) {

super.paintComponent(g);

for (int i = 0; i < fRectVec.size(); i++) {// 实心矩形

g.setColor(((Specific) fRectVec.elementAt(i)).gCol());

g.fillRect(Math.min(((Specific) fRectVec.elementAt(i)).gX1(),

((Specific) fRectVec.elementAt(i)).gX2()), Math.min(

((Specific) fRectVec.elementAt(i)).gY2(), ((Specific) fRectVec

.elementAt(i)).gY1()), Math.abs(((Specific) fRectVec

.elementAt(i)).gX1()

- ((Specific) fRectVec.elementAt(i)).gX2()), Math

.abs(((Specific) fRectVec.elementAt(i)).gY1()

- ((Specific) fRectVec.elementAt(i)).gY2()));

}

for (int i = 0; i < fOvalVec.size(); i++) {// 实心椭圆

g.setColor(((Specific) fOvalVec.elementAt(i)).gCol());

g.fillOval(Math.min(((Specific) fOvalVec.elementAt(i)).gX1(),

((Specific) fOvalVec.elementAt(i)).gX2()), Math.min(

((Specific) fOvalVec.elementAt(i)).gY2(), ((Specific) fOvalVec

.elementAt(i)).gY1()), Math.abs(((Specific) fOvalVec

.elementAt(i)).gX1()

- ((Specific) fOvalVec.elementAt(i)).gX2()), Math

.abs(((Specific) fOvalVec.elementAt(i)).gY1()

- ((Specific) fOvalVec.elementAt(i)).gY2()));

}

for (int i = 0; i < OvalVec.size(); i++) {// 空心椭圆

g.setColor(((Specific) OvalVec.elementAt(i)).gCol());

g.drawOval(Math.min(((Specific) OvalVec.elementAt(i)).gX1(),

((Specific) OvalVec.elementAt(i)).gX2()), Math.min(

((Specific) OvalVec.elementAt(i)).gY2(), ((Specific) OvalVec

.elementAt(i)).gY1()), Math.abs(((Specific) OvalVec

.elementAt(i)).gX1()

- ((Specific) OvalVec.elementAt(i)).gX2()), Math

.abs(((Specific) OvalVec.elementAt(i)).gY1()

- ((Specific) OvalVec.elementAt(i)).gY2()));

}

for (int i = 0; i < RectVec.size(); i++) {// 空心矩形

g.setColor(((Specific) RectVec.elementAt(i)).gCol());

g.drawRect(Math.min(((Specific) RectVec.elementAt(i)).gX1(),

((Specific) RectVec.elementAt(i)).gX2()), Math.min(

((Specific) RectVec.elementAt(i)).gY2(), ((Specific) RectVec

.elementAt(i)).gY1()), Math.abs(((Specific) RectVec

.elementAt(i)).gX1()

- ((Specific) RectVec.elementAt(i)).gX2()), Math

.abs(((Specific) RectVec.elementAt(i)).gY1()

- ((Specific) RectVec.elementAt(i)).gY2()));

}

for (int i = 0; i < LineVec.size(); i++) {// 线

g.setColor(((Specific) LineVec.elementAt(i)).gCol());

g.drawLine(((Specific) LineVec.elementAt(i)).gX1(), ((Specific) LineVec

.elementAt(i)).gY1(), ((Specific) LineVec.elementAt(i)).gX2(),

((Specific) LineVec.elementAt(i)).gY2());

}

for (int i = 0; i < freeVec.size(); i++) {// 自由图画

g.setColor(((Specific) freeVec.elementAt(i)).gCol());

g.drawLine(((Specific) freeVec.elementAt(i)).gX1(), ((Specific) freeVec

.elementAt(i)).gY1(), ((Specific) freeVec.elementAt(i)).gX2(),

((Specific) freeVec.elementAt(i)).gY2());

}

// 恢复到当前（重绘前）画图颜色

g.setColor(color);

// 恢复到当前（重绘前）画图状态

if (mode == 1)

g.fillRect(Math.min(X1, X2), Math.min(Y1, Y2), Math.abs(X1 - X2), Math

.abs(Y1 - Y2));

if (mode == 2)

g.fillOval(Math.min(X1, X2), Math.min(Y1, Y2), Math.abs(X1 - X2), Math

.abs(Y1 - Y2));

if (mode == 3)

g.drawRect(Math.min(X1, X2), Math.min(Y1, Y2), Math.abs(X1 - X2), Math

.abs(Y1 - Y2));

if (mode == 4)

g.drawOval(Math.min(X1, X2), Math.min(Y1, Y2), Math.abs(X1 - X2), Math

.abs(Y1 - Y2));

if (mode == 5)

g.drawLine(X1, Y1, X2, Y2);

if (mode == 6)

g.drawLine(LX1, LY1, LX2, LY2);

}// 画图结束

}// 画板类结束

// --------------画图时，侦听鼠标的响应---------------------------------------------------------------------//

private class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent event) {

if (event.getSource() == DrawRectBut) {// 来自画实心矩形

mode = 1;

CurSet = 1;

} else if (event.getSource() == DrawOvalBut) {// //来自实心椭圆

mode = 2;

CurSet = 1;

} else if (event.getSource() == ERectBut) {// 来自画空心矩形

mode = 3;

CurSet = 1;

} else if (event.getSource() == EOvalBut) {// 来自画空心

mode = 4;

CurSet = 1;

} else if (event.getSource() == DrawLineBut) {// 来自画线

mode = 5;

CurSet = 2;

} else if (event.getSource() == DrawBut) {// 来自任意图画

mode = 6;

CurSet = 2;

}

}

} // 内部类结束

// ----------------------------------------------------------------------------------------//

public PaintIt() {

super("超级画板 2006");

Container c = getContentPane();

desktop = new JDesktopPane();

c.add(desktop);

StatusPanel = new JPanel();

StatusPanel.setLayout(new GridLayout(1, 3));

// 创建菜单

JMenuBar Bar = new JMenuBar();

setJMenuBar(Bar);

JMenu File = new JMenu("文件");

File.setMnemonic('F');

JMenu Help = new JMenu("帮助");

Help.setMnemonic('H');

JMenu Ed = new JMenu("编辑");

Ed.setMnemonic('E');

JMenuItem New1 = new JMenuItem("新建");

JMenuItem Open1 = new JMenuItem("打开");

JMenuItem About1 = new JMenuItem("关于");

JMenuItem Save1 = new JMenuItem("保存");

JMenuItem Exit1 = new JMenuItem("退出");

JMenuItem Print1 = new JMenuItem("打印");

JMenuItem Col = new JMenuItem("颜色");

JMenuItem View1 = new JMenu("视图");

// 工具栏

JCheckBoxMenuItem toolBar = new JCheckBoxMenuItem("工具栏");

toolBar.setState(true);

// 状态栏

JCheckBoxMenuItem statusBar = new JCheckBoxMenuItem("状态栏");

statusBar.setState(true);

File.add(New1);

File.add(Open1);

File.add(Save1);

File.add(Print1);

File.addSeparator();

File.add(Exit1);

Ed.add(View1);

View1.add(toolBar);

View1.add(statusBar);

Help.add(About1);

Exit1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent act1) {

System.exit(0);

}

}); // 内部匿名类结束

// 鼠标点“关于”时出现的信息

About1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent act2) {

JOptionPane.showMessageDialog(null,

"这个程序已由TradeMark注册, 版权归属：ALi Kashani.");

}

}); // 内部匿名类结束

// 侦听“新图画”

New1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent act) {

JInternalFrame newFrame = new JInternalFrame("新图画", true, true, true,

true);

Container con = newFrame.getContentPane();

FreePanel myPanel = new FreePanel();

con.add(myPanel, BorderLayout.CENTER);

myPanel.setBackground(Color.WHITE);

newFrame.pack();

desktop.add(newFrame);

newFrame.setVisible(true);

}

});

// 侦听工具栏的隐藏和显示

toolBar.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent item) {

if (toolVisible == 1) {

panel.setVisible(false);

toolVisible = 0;

} else {

panel.setVisible(true);

toolVisible = 1;

}

}

});

// 侦听状态栏的隐藏和显示

statusBar.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent item) {

if (statusVisible == 1) {

StatusPanel.setVisible(false);

statusVisible = 0;

} else {

StatusPanel.setVisible(true);

statusVisible = 1;

}

}

});

// -------创建和组合工具栏，贴图

Bar.add(File);

Bar.add(Ed);

Bar.add(Help);

setJMenuBar(Bar);

RectPanel = new JPanel();

OvalPanel = new JPanel();

RectPanel.setLayout(new GridLayout(1, 2));

OvalPanel.setLayout(new GridLayout(1, 2));

ERectBut = new JButton();

ERectBut.setIcon(new ImageIcon("Rect1.gif"));

ERectBut.setRolloverIcon(new ImageIcon("Rect2.gif"));

EOvalBut = new JButton();

EOvalBut.setIcon(new ImageIcon("Oval1.gif"));

EOvalBut.setRolloverIcon(new ImageIcon("Oval2.gif"));

panel = new JPanel();

panel.setLayout(new GridLayout(6, 1));

ColorBut = new JButton();

ColorBut.setIcon(new ImageIcon("Color1.gif"));

ColorBut.setRolloverIcon(new ImageIcon("Color2.gif"));

DrawRectBut = new JButton();

DrawRectBut.setIcon(new ImageIcon("FRect1.gif"));

DrawRectBut.setRolloverIcon(new ImageIcon("FRect2.gif"));

DrawBut = new JButton();

DrawBut.setIcon(new ImageIcon("FreeDrw1.gif"));

DrawBut.setRolloverIcon(new ImageIcon("FreeDrw2.gif"));

label = new JLabel();

label2 = new JLabel();

label3 = new JLabel();

label4 = new JLabel();

label5 = new JLabel();

ClearBut = new JButton();

ClearBut.setIcon(new ImageIcon("Clear1.gif"));

ClearBut.setRolloverIcon(new ImageIcon("Clear2.gif"));

DrawOvalBut = new JButton();

DrawOvalBut.setIcon(new ImageIcon("FOval1.gif"));

DrawOvalBut.setRolloverIcon(new ImageIcon("FOval2.gif"));

DrawLineBut = new JButton();

DrawLineBut.setIcon(new ImageIcon("Line1.gif"));

DrawLineBut.setRolloverIcon(new ImageIcon("Line2.gif"));

ButtonHandler handler = new ButtonHandler();

OvalPanel.add(EOvalBut);

OvalPanel.add(DrawOvalBut);

RectPanel.add(DrawRectBut);

RectPanel.add(ERectBut);

ColorBut.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

color = JColorChooser.showDialog(null, "请选择颜色", color);

}

});

panel.add(RectPanel);

panel.add(OvalPanel);

panel.add(DrawBut);

panel.add(DrawLineBut);

panel.add(ColorBut);

panel.add(ClearBut);

StatusPanel.add(label2);

StatusPanel.add(label3);

StatusPanel.add(label4);

StatusPanel.add(label5);

StatusPanel.add(label);

c.add(StatusPanel, BorderLayout.SOUTH);

DrawRectBut.addActionListener(handler);

DrawBut.addActionListener(handler);

ClearBut.addActionListener(handler);

DrawOvalBut.addActionListener(handler);

DrawLineBut.addActionListener(handler);

ERectBut.addActionListener(handler);

EOvalBut.addActionListener(handler);

c.add(panel, BorderLayout.WEST);

setSize(900, 700);

setVisible(true);

}

// -------------------------------------------------------------------------------------------------------//

public static void main(String args[]) {

PaintIt pn = new PaintIt();

pn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

} // 主方法结束

}// 画板类结束

/*

* newFrame.addWindowListener( new WindowListener() { public void windowClosed(

* WindowEvent e ) { } public void windowOpened( WindowEvent e ) { } public void

* windowActivated( WindowEvent e ) { } public void windowDeactivated(

* WindowEvent e ) { } public void windowClosing( WindowEvent e )

* {StatusPanel.setVisible( false); slide.setVisible( false);

* JOptionPane.showMessageDialog(null, "This Program is a Registered TradeMark,

* and All Rights\n Reserved To Engineer ALi Kashani."); } public void

* windowIconified( WindowEvent e ) { } public void windowDeiconified(

* WindowEvent e ) { } });

*/