📄 drawpanapp.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class DrawPan extends JPanel {
private LinkedList drawAction = new LinkedList();
public void save(String file) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
file));
oos.writeObject(drawAction);
oos.close();
}
public void load(String file) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
drawAction = (LinkedList) ois.readObject();
ois.close();
repaint();
}
public void clear() {
drawAction.clear();
repaint();
}
public void paint(Graphics g) {
super.paint(g);
ListIterator iter = drawAction.listIterator();
while (iter.hasNext()) {
LinkedList ll = (LinkedList) iter.next();
ListIterator iter2 = ll.listIterator();
int x1 = -1, y1 = -1;
while (iter2.hasNext()) {
Point temp = ((Point) iter2.next());
if (x1 != -1 || y1 != -1)
g.drawLine(x1, y1, temp.x, temp.y);
x1 = temp.x;
y1 = temp.y;
}
}
}
public DrawPan() {
drawAction.push(new LinkedList());
this.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
((LinkedList) (drawAction.getFirst())).push(e.getPoint());
repaint();
}
public void mouseMoved(MouseEvent e) {
}
});
this.addMouseListener(new java.awt.event.MouseListener() {
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
drawAction.push(new LinkedList());
}
public void mouseReleased(MouseEvent e) {
}
});
}
}
public class DrawPanApp extends JFrame {
DrawPan drawPan = new DrawPan();
public DrawPanApp() {
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenu menuDrawPan = new JMenu("DrawPan");
JMenuItem itemSave = new JMenuItem("Save");
JMenuItem itemLoad = new JMenuItem("Load");
JMenuItem itemQuit = new JMenuItem("Quit");
JMenuItem itemClear = new JMenuItem("Clear");
menuDrawPan.add(itemClear);
menuFile.add(itemLoad);
menuFile.add(itemSave);
menuFile.add(new JSeparator());
menuFile.add(itemQuit);
itemQuit.addActionListener(new ActionQuit());
itemSave.addActionListener(new ActionSave(this, drawPan));
itemLoad.addActionListener(new ActionLoad(this, drawPan));
itemClear.addActionListener(new ActionClear(drawPan));
menuBar.add(menuFile);
menuBar.add(menuDrawPan);
this.setJMenuBar(menuBar);
this.add("Center", drawPan);
}
class ActionQuit implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
class ActionClear implements ActionListener {
DrawPan dP;
public ActionClear(DrawPan dP) {
this.dP = dP;
}
public void actionPerformed(ActionEvent e) {
dP.clear();
}
}
class ActionSave implements ActionListener {
ObjectOutputStream oos;
DrawPan dP;
FileDialog d;
public ActionSave(JFrame app, DrawPan dP) {
this.dP = dP;
d = new FileDialog(app, "What file do you want to Save?");
}
public void actionPerformed(ActionEvent e) {
try {
d.setFile("*.draw");
d.setDirectory(".");
d.setVisible(true);
String file;
if ((file = d.getFile()) != null) {
dP.save(d.getDirectory()+file);
}
} catch (IOException err) {
err.printStackTrace();
}
}
}
class ActionLoad implements ActionListener {
ObjectInputStream ois;
DrawPan dP;
FileDialog d;
public ActionLoad(JFrame app, DrawPan dP) {
this.dP = dP;
d = new FileDialog(app, "What file do you want to load?");
}
public void actionPerformed(ActionEvent e) {
try {
d.setFile("*.draw");
d.setDirectory(".");
d.setVisible(true);
String file;
if ((file = d.getFile()) != null)
dP.load(d.getDirectory()+file);
} catch (ClassNotFoundException err) {
err.printStackTrace();
} catch (IOException err) {
err.printStackTrace();
}
}
}
public static void main(String[] args) {
DrawPanApp drawPanApp = new DrawPanApp();
drawPanApp.setSize(200, 200);
drawPanApp.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -