📄 drawer.java
字号:
/**
* 画图程序主面板。界面的初始化。
* created by LHand on May 12th.
*/
package cn.edu.xmu.LHand;
import javax.swing.*;
import java.util.ArrayList;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Drawer extends JFrame{
private JMenuBar menuBar;
private JToolBar toolbar;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private DrawBoard drawboard;
private ButtonGroup graphButtonGroup;
private JComboBox stroke;
private JButton foreColor;
private JButton backColor;
private JButton eraser;
private ObjectInputStream input;
private ObjectOutputStream output;
private ArrayList shapes = new ArrayList();
public static JLabel statusBar = new JLabel(" |"
+" |");
public Drawer(){
super("画图");
initInterface();
}
//初始化界面
private void initInterface(){
toolbar = new JToolBar("工具栏"); //初始化工具栏
drawboard = new DrawBoard(); //初始化画板
menuBar = new JMenuBar(); //初始化菜单栏
//界面布局
Container container = getContentPane();
container.add(toolbar,BorderLayout.WEST);
container.add(new JScrollPane(drawboard),BorderLayout.CENTER);
container.add(menuBar,BorderLayout.NORTH);
container.add(statusBar,BorderLayout.SOUTH);
this.setBackground(Color.gray);
this.setMinimumSize(new Dimension(650,600));
this.setLocation(200,100);
setVisible(true);
//布局结束
//菜单栏JMenuBar
JMenuItem openItem = new JMenuItem("打开(O)");
openItem.setMnemonic('O');
//添加选择打开文件事件
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
openFile();
}
});
JMenuItem saveItem = new JMenuItem("保存(S)");
saveItem.setMnemonic('S');
saveItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
saveFile();
}
}
);
JMenuItem exitItem = new JMenuItem("退出(E)");
exitItem.setMnemonic('E');
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
}
);
JMenu fileMenu = new JMenu("文件(F)");
fileMenu.setMnemonic('F');
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
//编辑文件选项
JMenuItem moveItem = new JMenuItem("撤销(U)");
moveItem.setMnemonic('U');
moveItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
int index = DrawBoard.IShapes.size();
index--;
if(index<0)
return;
shapes.add(DrawBoard.IShapes.get(index));
DrawBoard.IShapes.remove(index);
repaint();
}
}
);
JMenuItem repetitionItem = new JMenuItem("重复('R')");
repetitionItem.setMnemonic('R');
repetitionItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
int index = shapes.size();
index--;
if(index<0)
return;
DrawBoard.IShapes.add(shapes.get(index));
shapes.remove(index);
repaint();
}
}
);
JMenu editMenu = new JMenu("编辑(E)");
editMenu.add(moveItem);
editMenu.add(repetitionItem);
menuBar.add(editMenu);
//画笔粗细JPanel
String[] strokes = new String[]{
"Stroke Weight 1f","Stroke Weight 2f","Stroke Weight 5f",
"Stroke Weight 7.5f","Stroke Weight 10f",
};
stroke = new JComboBox(strokes);
//添加内部类处理事件
stroke.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent event){
if(event.getStateChange() == ItemEvent.SELECTED){
DrawBoard.strokeIndex = stroke.getSelectedIndex();
}
}
}
);
stroke.setMaximumRowCount(3);
stroke.setMaximumSize(new Dimension(160,50));
stroke.setBorder(new javax.swing.border.TitledBorder("stroke changes"));
//工具栏JPanel
JPanel toolPanel = new JPanel();
toolPanel.setBorder(new javax.swing.border.TitledBorder("Drawing Tools"));
toolPanel.setLayout(new GridLayout(4, 2, 5, 5));
toolPanel.setMaximumSize(new Dimension(185,135));
buttonsActionHandle handle = new buttonsActionHandle();
button1 = new JButton("Word");
button1.setToolTipText("文字");
button1.addActionListener(handle);
button2 = new JButton("Line");
button2.setToolTipText("直线");
button2.addActionListener(handle);
button3 = new JButton("Rect");
button3.setToolTipText("矩形");
button3.addActionListener(handle);
button4 = new JButton("ArcRect");
button4.setToolTipText("圆角矩形");
button4.addActionListener(handle);
button5 = new JButton("Oval");
button5.setToolTipText("椭圆");
button5.addActionListener(handle);
button6 = new JButton("Polygon");
button6.setToolTipText("多边形");
button6.addActionListener(handle);
button7 = new JButton("Brush");
button7.setToolTipText("刷子");
button7.addActionListener(handle);
button8 = new JButton("Pencil");
button8.setToolTipText("铅笔");
button8.addActionListener(handle);
graphButtonGroup = new ButtonGroup();
graphButtonGroup.add(button1);
graphButtonGroup.add(button2);
graphButtonGroup.add(button3);
graphButtonGroup.add(button4);
graphButtonGroup.add(button5);
graphButtonGroup.add(button6);
graphButtonGroup.add(button7);
graphButtonGroup.add(button8);
toolPanel.add(button1);
toolPanel.add(button2);
toolPanel.add(button3);
toolPanel.add(button4);
toolPanel.add(button5);
toolPanel.add(button6);
toolPanel.add(button7);
toolPanel.add(button8);
//前景与背景色JPanel
JPanel colorPanel = new JPanel();
colorPanel.setBorder(new javax.swing.border.TitledBorder("Color Settings"));
foreColor = new JButton();
foreColor.setToolTipText("前景色");
foreColor.setPreferredSize(new Dimension(50, 50));
foreColor.setBorder(new javax.swing.border.LineBorder(new Color(0,0,0)));
foreColor.setBackground(Color.BLACK);
foreColor.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
Color color = new Color(0,0,0);
color = JColorChooser.showDialog(Drawer.this,"前景色选择",color);
if(color != null)
foreColor.setBackground(color);
drawboard.setForeground(foreColor.getBackground());
}
}
);
backColor = new JButton();
backColor.setToolTipText("背景色");
backColor.setPreferredSize(new Dimension(50, 50));
backColor.setBorder(new javax.swing.border.LineBorder(new Color(0,0,0)));
backColor.setBackground(Color.WHITE);
backColor.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
Color color = new Color(0,0,0);
color = JColorChooser.showDialog(Drawer.this,"背景色选择",color);
if(color != null)
backColor.setBackground(color);
drawboard.setBackground(backColor.getBackground());
}
}
);
colorPanel.add(foreColor);
colorPanel.add(backColor);
colorPanel.setMaximumSize(new Dimension(160,100));
//清除面板
JPanel clearPanel = new JPanel();
JButton clearButton = new JButton("Clear");
clearButton.setToolTipText("清空面板");
clearButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
drawboard.clearBoard();
shapes.clear();
}
});
clearPanel.add(clearButton);
clearPanel.setBorder(new javax.swing.border.TitledBorder("ClearBoard"));
clearPanel.setMaximumSize(new Dimension(160,60));
//橡皮插 JPanel
JPanel eraserPanel = new JPanel();
eraserPanel.setBorder(new javax.swing.border.TitledBorder("EraserBoard"));
eraser = new JButton("Eraser");
eraser.setToolTipText("橡皮插");
eraser.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
DrawBoard.buttonPressed = 9;
}
}
);
eraserPanel.add(eraser);
eraserPanel.setMaximumSize(new Dimension(160,60));
toolbar.add(toolPanel);
toolbar.add(stroke);
toolbar.add(colorPanel);
toolbar.add(clearPanel);
toolbar.add(eraserPanel);
toolbar.setOrientation(JToolBar.VERTICAL);
}
private void openFile(){
JFileChooser fileChooser = new JFileChooser("C:\\Documents and Settings\\" +
"Administrator\\My Documents\\My Pictures\\");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
//fileChooser.setFileFiter(Filefiter()); //设置文件过滤
int result =fileChooser.showOpenDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
return ;
File fileName=fileChooser.getSelectedFile();
if (fileName==null||fileName.getName().equals("")){
JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE);
openFile();
}
if(!fileName.canRead()){
JOptionPane.showMessageDialog(this,"The file can't be read or not exit",
"ErrorRend",JOptionPane.ERROR_MESSAGE);
openFile();
}
try{
FileInputStream fis=new FileInputStream(fileName);
input = new ObjectInputStream(fis);
DrawBoard.IShapes = (ArrayList)input.readObject();
repaint();
}
catch (IOException ioException){
JOptionPane.showMessageDialog(this,"error during read from file",
"read Error",JOptionPane.ERROR_MESSAGE );
}
catch(ClassNotFoundException classNotFoundException){
JOptionPane.showMessageDialog(this,"Unable to Create Object",
"end of file",JOptionPane.ERROR_MESSAGE );
}
}
private void saveFile(){
JFileChooser fileChooser = new JFileChooser("C:\\Documents and Settings\\" +
"Administrator\\My Documents\\My Pictures\\");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showSaveDialog(this);
if(result == JFileChooser.CANCEL_OPTION)
return;
File fileName = fileChooser.getSelectedFile();
fileName.canWrite();
if (fileName==null||fileName.getName().equals(""))
JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE);
else{
try{
fileName.delete();
FileOutputStream fos = new FileOutputStream(fileName);
output = new ObjectOutputStream(fos);
output.writeObject(DrawBoard.IShapes);
output.close();
fos.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
return;
}
}
//画图工具监听器。
private class buttonsActionHandle implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getSource() == button1){
DrawBoard.buttonPressed = 1;
}
if(event.getSource() == button2){
DrawBoard.buttonPressed = 2;
}
if(event.getSource() == button3){
DrawBoard.buttonPressed = 3;
}
if(event.getSource() == button4){
DrawBoard.buttonPressed = 4;
}
if(event.getSource() == button5){
DrawBoard.buttonPressed = 5;
}
if(event.getSource() == button6){
DrawBoard.buttonPressed = 6;
}
if(event.getSource() == button7){
DrawBoard.buttonPressed = 7;
}
if(event.getSource() == button8){
DrawBoard.buttonPressed = 8;
}
}
}
//main
public static void main(String[] args) {
Drawer appDrawTool = new Drawer();
appDrawTool.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -