📄 drawplat.java
字号:
package DrawPlat;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
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.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
public class DrawPlat extends JFrame {
private ObjectInputStream input;
private ObjectOutputStream output;
Image offScreenImage = null;
DrawPlat mdp;
private JButton choices[]; //按钮数组,存放一下名称的功能按钮
private String names[] = {
"New",
"Open",
"Save",
"Pencil",
"Line",
"Rect",
"fRect",
"Oval",
"fOval",
"Circle",
"fCircle",
"RoundRect",
"frRect",
"Rubber",
"Color",
"Stroke"
};
private Icon items[];
private String tipText[] = {
//这里是鼠标移动到相应按钮上面停留时给出的提示说明条
//读者可以参照上面的按钮定义对照理解
"Draw a new picture",
"Open a saved picture",
"Save current drawing",
"Draw at will",
"Draw a straight line",
"Draw a rectangle",
"Fill a rectangle",
"Draw a oval",
"Fill a oval",
"Darw a circle",
"Fill a circle",
"Draw a round rectangle",
"Fill a round rectangle",
"Erase at will",
"Choose current drawing color",
"Set current drawing stroke",
};
JToolBar buttonPanel; //定义按钮面板
public JLabel statusBar; //显示鼠标状态的提示条
public DrawPanel drawingArea; //定义画图区域
private int width = 800,height = 650; //定义画图区域的初始大小
public drawings[] itemList = new drawings[5000];//用来存放基本图形的数组
public int[] records= new int[5000];
public int currentChoice = 3; //设置默认画图状态为随笔画
public int index = 0; //当前已经绘制的图形的数目
private Color color = Color.black; //设置当前画笔颜色
int R,G,B; //用来存放当前的颜色值
private float stroke = 1.0f; //设置画笔粗细,默认值为1.0f
public DrawPlat(){
super("Drawing Pad");
mdp = this;
JMenuBar bar = new JMenuBar();//定义菜单栏
JMenu fillMenu = new JMenu("File");
fillMenu.setMnemonic('F');
//新建文件菜单项
JMenuItem newItem = new JMenuItem("New");
newItem.setMnemonic('N');
newItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
newFile(); //如果被触发,则调用新建文件函数段
}
}
);
fillMenu.add(newItem);
//保存文件菜单项
JMenuItem saveItem = new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile(); //如果被触发,则调用保存文件函数段
}
}
);
fillMenu.add(saveItem);
//打开文件菜单项
JMenuItem loadItem = new JMenuItem("Load");
loadItem.setMnemonic('L');
loadItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadFile(); //如果被触发,则调用打开文件函数段
}
}
);
fillMenu.add(loadItem);
fillMenu.addSeparator();
//退出菜单项
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('X');
exitItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0); //如果触发。则推出画图板程序
}
}
);
fillMenu.add(exitItem);
bar.add(fillMenu);
//设置颜色菜单栏
JMenu colorMenu = new JMenu("Color");
colorMenu.setMnemonic('C');
//选择颜色菜单项
JMenuItem colorItem = new JMenuItem("Choose Color");
colorItem.setMnemonic('O');
colorItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooseColor(); //如果被触发,则调用选择颜色函数段
}
}
);
colorMenu.add(colorItem);
bar.add(colorMenu);
//设置线条粗细菜单栏
JMenu strokeMenu = new JMenu("Stroke");
strokeMenu.setMnemonic('S');
//设置线条粗细菜单项
JMenuItem strokeItem = new JMenuItem("Set Stroke");
strokeItem.setMnemonic('K');
strokeItem.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setStroke();
}
}
);
strokeMenu.add(strokeItem);
bar.add(strokeMenu);
//设置提示菜单栏
JMenu helpMenu = new JMenu("help");
helpMenu.setMnemonic('H');
//设置提示菜单项
JMenuItem aboutItem = new JMenuItem("About this Drawing Pad");
aboutItem.setMnemonic('A');
aboutItem.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showConfirmDialog(null,
"This is a mini drawing pad!\n" +
"Copyright (c) 2008 xidian University",
"画图板程序说明",
JOptionPane.INFORMATION_MESSAGE);
}
}
);
helpMenu.add(aboutItem);
bar.add(helpMenu);
items = new ImageIcon[names.length];
//创建各种基本图形的按钮
drawingArea = new DrawPanel(this);
choices = new JButton[names.length];
buttonPanel = new JToolBar(JToolBar.VERTICAL);
ButtonHandler handler = new ButtonHandler();
ButtonHandler1 handler1 = new ButtonHandler1();
for(int i = 0; i < choices.length; i++) {
items[i] = new ImageIcon(getClass().getResource("/Images/"+names[i]+".GIF"));
choices[i] = new JButton(items[i]);
choices[i].setToolTipText(tipText[i]);
buttonPanel.add(choices[i]);
}
//将动作侦听器加入按钮里
for(int i = 3; i < choices.length-2; i++ ) {
choices[i].addActionListener(handler);
}
choices[0].addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
newFile();
}
}
);
choices[1].addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
loadFile();
}
}
);
choices[2].addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
saveFile();
}
}
);
// choices[choices.length - 3].addActionListener(handler1);
choices[choices.length - 2].addActionListener(handler1);
choices[choices.length - 1].addActionListener(handler1);
Container c = getContentPane();
super.setJMenuBar(bar);
c.add(buttonPanel, BorderLayout.WEST);
c.add(drawingArea,BorderLayout.CENTER);
statusBar = new JLabel();
c.add(statusBar, BorderLayout.SOUTH);
statusBar.setText(" Welcome To The Little Drawing Pad!!! :");
createNewItem();
setSize(width,height);
setVisible(true);
}
//按钮侦听器ButtonHandler类,内部类,用来侦听基本按钮的操作
public class ButtonHandler implements ActionListener {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -