package project;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class MiniDrawPad extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private ObjectInputStream input;
private ObjectOutputStream output;
private JMenuItem choices[];
private String names[]={
"New",
"Open",
"Save",
"Pencil",
"Line",
"Rect",
"fRect",
"Oval",
"fOval",
"Circle",
"fCircle",
"RoundRect",
"frRect",
"Rubber",
"Color",
"Stroke",
"Word"
};
private String styleNames[]={" 宋体 " , " 隶书 " , " 华文彩云 " , " 仿宋_GB2312 " , " 华文行楷 " ," 方正舒体 " , " Times New Roman " , " Serif " , " Monospaced " ," SonsSerif " , " Garamond "}; //可供选择的字体项
JPopupMenu pmu = new JPopupMenu();// 创建弹出式菜单选项
private String tipText[]={ "New(N)","Open(O)","Save(S)","随心画(1)", "直线(2)","矩形(3)", "实矩形(4)", "空椭圆(5)","实椭圆(6)", "空心圆(7)", "实心圆(8)","圆角矩形(9)", "实心圆角矩形(10)","橡皮(E)", "颜色(C)","粗细","写字" };
JToolBar buttonPanel ; //定义按钮面板
private JLabel statusBar; //显示鼠标状态的提示条
private DrawPanel drawingArea; //画图区域
private int width=800,height=500;
drawings[] itemList=new drawings[5000]; //用来存放基本图形的数组
private int currentChoice=3;
int index=0; //当前已经绘制的图形数目
private Color color=Color.black;
int R,G,B;
int f1,f2; //用来存放当前字体风格
String style1;
private float stroke=1.0f;
JCheckBox bold,italic;
JComboBox<?> styles;
JMenuBar mb = new JMenuBar();// 创建菜单栏组件
JMenu a1 = new JMenu("文件(F)");// 创建菜单组件
JMenu a2 = new JMenu("形状(C)");
JMenu a3 = new JMenu("颜色(H)");
JMenu a4 = new JMenu("工具(T)");
@SuppressWarnings({ "unchecked", "rawtypes" })
public MiniDrawPad(){
super("Drawing Pad");
setLocation(100,50);
setTitle("涂鸦板");
mb.add(a1);
mb.add(a2);
mb.add(a3);
mb.add(a4);
setJMenuBar(mb);
drawingArea=new DrawPanel();
choices=new JMenuItem[names.length];
buttonPanel = new JToolBar( JToolBar.VERTICAL ) ;
buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ;
ButtonHandler handler=new ButtonHandler();
ButtonHandler1 handler1=new ButtonHandler1(); //添加按钮
for (int i = 0; i < choices.length; i++) {
choices[i]=new JMenuItem(tipText[i]);
choices[i].setIcon(new ImageIcon("/project/images/IMG"+i+".jpg"));
buttonPanel.add(choices[i]);
}
choices[0].setMnemonic(KeyEvent.VK_N);a1.add(choices[0]);// 文件
choices[1].setMnemonic(KeyEvent.VK_O);a1.add(choices[1]);
choices[2].setMnemonic(KeyEvent.VK_S);a1.add(choices[2]);
choices[3].setMnemonic(KeyEvent.VK_1);a2.add(choices[3]);//形状
choices[4].setMnemonic(KeyEvent.VK_2);a2.add(choices[4]);
choices[5].setMnemonic(KeyEvent.VK_3);a2.add(choices[5]);
choices[6].setMnemonic(KeyEvent.VK_4);a2.add(choices[6]);
choices[7].setMnemonic(KeyEvent.VK_S);a2.add(choices[7]);
choices[8].setMnemonic(KeyEvent.VK_S);a2.add(choices[8]);
choices[9].setMnemonic(KeyEvent.VK_S);a2.add(choices[9]);
choices[10].setMnemonic(KeyEvent.VK_S);a2.add(choices[10]);
choices[11].setMnemonic(KeyEvent.VK_S);a2.add(choices[11]);
choices[12].setMnemonic(KeyEvent.VK_S);a2.add(choices[12]);
choices[14].setMnemonic(KeyEvent.VK_C);a3.add(choices[14]); //颜色
choices[13].setMnemonic(KeyEvent.VK_E);a4.add(choices[13]);//工具
for(int i=3;i<choices.length-3;i++){
choices[i].addActionListener(handler);
}
choices[0].addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){newFile();}}
);
choices[1].addActionListener( new ActionListener() {public void actionPerformed(ActionEvent e){loadFile();}}
);
choices[2].addActionListener( new ActionListener(){public void actionPerformed(ActionEvent e) {saveFile();}}
);
choices[choices.length-3].addActionListener(handler1);
choices[choices.length-2].addActionListener(handler1);
choices[choices.length-1].addActionListener(handler1);
//字体风格选择
styles=new JComboBox(styleNames);
styles.setMaximumRowCount(8);
styles.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e){style1=styleNames[styles.getSelectedIndex()];}});
//字体选择
bold=new JCheckBox("粗体");
italic=new JCheckBox("斜体");
pmu.add("制作者:");
pmu.add("刘中江 梁倩 陈玉琪 李倩倩");
a1.setMnemonic(KeyEvent.VK_F);// 下拉框 快捷键
a2.setMnemonic(KeyEvent.VK_C);
a3.setMnemonic(KeyEvent.VK_H);
a4.setMnemonic(KeyEvent.VK_T);
checkBoxHandler cHandler=new checkBoxHandler();
bold.addItemListener(cHandler);
italic.addItemListener(cHandler);
buttonPanel.add(bold);
buttonPanel.add(italic);
buttonPanel.add(styles);
styles.setMinimumSize( new Dimension ( 50, 20 ) );
styles.setMaximumSize(new Dimension ( 100, 20 ) );
Container c=getContentPane();
super.setJMenuBar( mb );
//super.setJMenuBar( bar );
c.add(buttonPanel,BorderLayout.NORTH);
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);
add(pmu);// 将弹出式菜单加入到窗口
pmu.show(this, 700, 300);// 在指定坐标显示弹出式菜单
pmu.setBackground(Color.gray);
validate();
}
public class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
for(int j=3;j<choices.length-3;j++){
if(e.getSource()==choices[j])
{
currentChoice=j;
createNewItem();
repaint();
}
}
}
}
public class ButtonHandler1 implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==choices[choices.length-3])
{chooseColor();}
if(e.getSource()==choices[choices.length-2])
{setStroke();}
if(e.getSource()==choices[choices.length-1])
{JOptionPane.showMessageDialog(null,
"请点击您要输入字符的位置",
"Hint",JOptionPane.INFORMATION_MESSAGE );
currentChoice=14;
createNewItem();
repaint();
}
}
}
//鼠标事件 mouseA 类,继承了 MouseAdapter,用来完成鼠标相应事件操作
class mouseA extends MouseAdapter{
public void mousePressed(MouseEvent e){
statusBar.setText(" Mouse Pressed @:[" + e.getX() +", " + e.getY() + "]");//设置状态提示
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
//如果当前选择的图形是随笔画或者橡皮擦,则进行下面的操作
if(currentChoice==3||currentChoice==13)
{
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
index++;
createNewItem();
}
//如果当前选择的图形式文字输入,则进行下面操作
if(currentChoice==14)
{
itemList[index].x1=e.getX();
itemList[index].y1=e.getY();
String input;
input=JOptionPane.showInputDialog(
"请输入您要输入的字符!");
itemList[index].s1=input;
itemList[index].x2=f1;
itemList[index].y2=f2;
itemList[index].s2=style1;
index++;
currentChoice=14;
createNewItem();
drawingArea.repaint();
}
}
public void mouseReleased(MouseEvent e){
statusBar.setText(" Mouse Released @:[" + e.getX() +", " + e.getY() + "]");
if(currentChoice==3||currentChoice==13)
{
itemList[index].x1=e.getX();
itemList[index].y1=e.getY();
}
itemList[index].x2=e.getX();
itemList[index].y2=e.getY();
repaint();
index++;
create