📄 huaban.java
字号:
package Huaban;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Huaban extends JFrame //主类,扩展了JFrame类,用来生成主界面
{
private ObjectInputStream input;
private ObjectOutputStream output; //定义输入输出流,用来调用和保存图像文件
private String names[]={"","","","Pencil","Line", "Rect", "Oval","Circle",
"Rubber","Stroke","Word" /*个绘图单元按钮*/};
private JButton choices[]; //按钮数组,存放以下名称的功能按钮
private Icon items[];
private String tipText[]={ "","","",//鼠标移动到按钮上面上的说明
"用铅笔随心画",
"画一条直线",
"画空心的矩形",
"画一个空心椭圆",
"画一个圆",
"橡皮擦工具",
"设置线条的粗细",
"在鼠标点击位置输入文字"
};
Icon wei=new ImageIcon("wang.gif") ;//介绍我的
Icon icon=new ImageIcon("mm.gif") ;//说明的ICON
Icon icon1=new ImageIcon("nn.gif") ;//输入文字的ICON
JToolBar buttonPanel ; //定义按钮面板
private JLabel statusBar; //显示鼠标状态的提示条
private DrawPanel drawingArea;//画图区域
drawings[] itemList=new drawings[5000]; //用来存放基本图形的数组
private int currentChoice=3; //设置默认画图状态为随笔画
int index=0; //当前已经绘制的图形数目
private Color color=Color.black; //当前画笔颜色
int R,G,B; //用来存放当前色彩值
int f1,f2; //用来存放当前字体风格
private float brush=1.0f;
JCheckBox bold,italic;
public Huaban() //构造函数
{
super("画板测试版 1.00");
JMenuBar bar=new JMenuBar();
//定义文件菜单
JMenu fileMenu=new JMenu("文件(F)");
fileMenu.setMnemonic('F');
//定义文件菜单的菜单条和快捷键
JMenuItem newItem=new JMenuItem("新建(N)");
JMenuItem saveItem=new JMenuItem("保存(S)");
JMenuItem openItem=new JMenuItem("打开(L)");
JMenuItem exitItem=new JMenuItem("退出(X)");
newItem.setMnemonic('N');
saveItem.setMnemonic('S');
openItem.setMnemonic('L');
exitItem.setMnemonic('X');
//添加4个菜单选项的触发事件
newItem.addActionListener(
new ActionListener(){ public void actionPerformed(ActionEvent e)
{ newFile();}
} );//如果被触发,则调用新建文件函数段
saveItem.addActionListener(
new ActionListener(){public void actionPerformed(ActionEvent e)
{ saveFile();}
} );//如果被触发,则调用保存文件函数段
openItem.addActionListener(
new ActionListener(){public void actionPerformed(ActionEvent e)
{ openFile();}
} );//如果被触发,则调用打开文件函数段
exitItem.addActionListener(
new ActionListener(){public void actionPerformed(ActionEvent e)
{ System.exit(0); }
} ); //如果被触发,则退出画图板程序
//在文件菜单中添加组件然后添加菜单fileMenu(Jmenu)到bar(Menubar)中
fileMenu.add(newItem);
fileMenu.add(saveItem);//保存文件菜单项
fileMenu.add(openItem); //打开文件菜单项
fileMenu.addSeparator();
fileMenu.add(exitItem);//退出菜单项
bar.add(fileMenu);
/********************************************************************************/
//设置图形绘制菜单
JMenu shapeMenu=new JMenu("更多形状(P)");
shapeMenu.setMnemonic('P');
//定义图形绘制菜单的菜单条
JMenuItem frectItem=new JMenuItem("画实心矩形(F)");
JMenuItem fovalItem=new JMenuItem("画实心椭圆(T)");
JMenuItem fcircleItem=new JMenuItem("画实心圆(Q)");
frectItem.setMnemonic('F');
fovalItem.setMnemonic('T');
fcircleItem.setMnemonic('Q');
//添加3 个菜单选项的触发事件
frectItem.addActionListener(
new ActionListener(){public void actionPerformed(ActionEvent e)
{ currentChoice=11;
createNewItem();
repaint();//如果被触发,则调用画实心矩形
}
} );
fovalItem.addActionListener(
new ActionListener(){public void actionPerformed(ActionEvent e)
{ currentChoice=12;
createNewItem();
repaint();//如果被触发,则调用画实心椭圆
}
} );
fcircleItem.addActionListener(
new ActionListener(){public void actionPerformed(ActionEvent e)
{ currentChoice=13;
createNewItem();
repaint();//如果被触发,则调用画实心圆
}
} );
//添加更多图形的菜单项和菜单
shapeMenu.add(frectItem);
shapeMenu.add(fovalItem);
shapeMenu.add(fcircleItem);
bar.add(shapeMenu);
/***********************************************************************************/
//设置颜色菜单条
JMenu colorMenu=new JMenu("颜色(C)");
colorMenu.setMnemonic('C');
//选择颜色菜单项
JMenuItem colorItem=new JMenuItem("选择颜色(O)");
colorItem.setMnemonic('O');
colorItem.addActionListener (
new ActionListener() {
public void actionPerformed(ActionEvent e)
{chooseColor();}//如果被触发,则调用选择颜色函数
} );
colorMenu.add(colorItem);
bar.add(colorMenu);
//设置提示菜单条
JMenu helpMenu=new JMenu("帮助(H)");
helpMenu.setMnemonic('H');
//设置提示菜单项
JMenuItem aboutItem=new JMenuItem("关于mini画板!(A)");
JMenuItem howItem=new JMenuItem("帮助主题(W)");
aboutItem.setMnemonic('A');
howItem.setMnemonic('W');
aboutItem.addActionListener (
new ActionListener(){ public void actionPerformed(ActionEvent e)
{ JOptionPane.showMessageDialog(null,
"您现在使用的是王伟开发的画板 1.00测试版\n华东交通大学05软件工程多媒体3班\nQQ:554684729",
" 画板开发介绍",
JOptionPane.INFORMATION_MESSAGE ,wei);
}
} );
howItem.addActionListener (
new ActionListener(){ public void actionPerformed(ActionEvent e)
{ JOptionPane.showMessageDialog(null,
"可以使用它打开已保存的图像文件\n也可以将画的图像保存起来\n该画板的使用比较简单,用它可以画出各种\n基本的图形,不足之处望指出,以待改进!",
" 画板使用介绍",
JOptionPane.INFORMATION_MESSAGE ,icon);
}
} );
//添加帮助的菜单项和菜单
helpMenu.add(aboutItem);
helpMenu.add(howItem);
bar.add(helpMenu);
/********************************************************************************************/
//创建各种基本图形的按钮
items=new ImageIcon[names.length];
drawingArea=new DrawPanel();
choices=new JButton[names.length];
buttonPanel = new JToolBar( JToolBar.VERTICAL ) ;
buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ;
ButtonHandler handler=new ButtonHandler();
ButtonHandler1 handler1=new ButtonHandler1();
//导入我们需要的图形图标,这些图标都存放在与源文件相同的目录下面
for(int i=3;i<choices.length;i++)
{items[i]=new ImageIcon(names[i] + ".gif");
choices[i]=new JButton("",items[i]);
choices[i].setPreferredSize(new Dimension(35,30));
choices[i].setToolTipText(tipText[i]);
buttonPanel.add(choices[i]);
}
//将动作侦听器加入按钮里面
for(int i=3;i<choices.length-2;i++)
{ choices[i].addActionListener(handler); }
choices[choices.length-2].addActionListener(handler1);
choices[choices.length-1].addActionListener(handler1);
bold=new JCheckBox("粗体");
italic=new JCheckBox("斜体字");
checkBoxHandler cHandler=new checkBoxHandler();
bold.addItemListener(cHandler);
italic.addItemListener(cHandler);
buttonPanel.add(bold);
buttonPanel.add(italic);
statusBar=new JLabel();
statusBar.setText(" 欢迎使用迷你画板1.00版~_~!!! ");
//设计JFrame整体布局
Container c=getContentPane();
super.setJMenuBar( bar );
c.add(buttonPanel,BorderLayout.NORTH);
c.add(drawingArea,BorderLayout.CENTER);
c.add(statusBar,BorderLayout.SOUTH);
createNewItem();//创建基本图形绘制区域,默认为Pencil绘画
setSize(720,450);
setVisible(true);
}//构造函数结束
//2个按钮侦听器,内部类,用来侦听基本按钮的操作
public class ButtonHandler implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ for(int j=1;j<choices.length-2;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-2])
{ setStroke(); }
if(e.getSource()==choices[choices.length-1])
{ JOptionPane.showMessageDialog(null,
"你可以点击画板用来添加文字!",
"文字输入:",
JOptionPane.INFORMATION_MESSAGE,icon1 );
currentChoice=9;
createNewItem();
repaint();
} } }
//鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作
class mouseA extends MouseAdapter
{ public void mousePressed(MouseEvent e)
{ statusBar.setText(" 鼠标点击绘画面板的位置:["
+ e.getX() + ", " + e.getY() + "]");
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
//如果当前选择的图形是随笔画或者橡皮擦,则进行下面的操作
if(currentChoice==3||currentChoice==8)
{ index++;
createNewItem();
}
//如果当前选择的图形式文字输入,则进行下面操作
if(currentChoice==9)
{
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;
index++;
currentChoice=9;
createNewItem();
drawingArea.repaint();
}
}
public void mouseReleased(MouseEvent e)
{ statusBar.setText(" 鼠标释放绘画面板的位置:["
+ e.getX() +", " + e.getY() + "]");
if(currentChoice==3||currentChoice==8)
{
itemList[index].x1=e.getX();
itemList[index].y1=e.getY();
}
itemList[index].x2=e.getX();
itemList[index].y2=e.getY();
repaint();
index++;
createNewItem();
}
}
//鼠标事件mouseB类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的相应操作
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -