items[i]=newImageIcon("Icons/"+names[i]+".gif");
choices[i]=newJButton(items[i]);
choices[i].setToolTipText(tipText[i]);
choices[i].setPreferredSize(newDimension(33,33));
choices[i].setBackground(newColor(0,255,0));
buttonPanel.add(choices[i]);
}
//画图面板类,用来画图
classDrawPanelextendsJPanel{
publicDrawPanel(){
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setBackground(Color.white);
addMouseListener(newmouseA());
addMouseMotionListener(newmouseB());
}
@Override
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
Graphics2Dg2d=(Graphics2D)g;//定义画笔
intj=0;
while(j<=index){
draw(g2d,itemList[j]);
j++;
}
}
voiddraw(Graphics2Dg2d,drawingsi){
i.draw(g2d);//将画笔传入到各个子类中,用来完成各自的绘图
}
}
代码:
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
Importjava.io.*
publicclassMiniDrawPadextendsJFrame//主类,扩展了JFrame类,用来生成主界面
{
privateObjectInputStreaminput;
privateObjectOutputStreamoutput;//定义输入输出流,用来调用和保存图像文件
privateJLabelstatusBar;//显示鼠标状态的提示条
privateDrawPaneldrawingArea;//画图区域
privateintwidth=850,height=600;//定义画图区域初始大小
drawings[]itemList=newdrawings[5000];//用来存放基本图形的数组
privateJButtonchoices[];//按钮数组,存放以下名称的功能按钮
JToolBarbuttonPanel;//定义按钮面板
privateintcurrentChoice=3;//设置默认画图状态为随笔画
intindex=0;//当前已经绘制的图形数目
privateColorcolor=Color.black;//当前画笔颜色
intR,G,B;//用来存放当前色彩值
intf1,f2;//用来存放当前字体风格
Stringstyle1;//用来存放当前字体
privatefloatstroke=1.0f;//设置画笔粗细,默认值为1.0f
JCheckBoxbold,italic;//定义字体风格选择框
JComboBoxstyles;
privateStringnames[]={
"New",
"Open",
"Save",
"Pencil",
"Line",
"Rect",
"fRect",
"Oval",
"fOval",
"Circle",
"fCircle",
"RoundRect",
"frRect",
"Rubber",
"Color",
"Stroke",
"Word"
};
privateStringstyleNames[]={
"宋体","隶书","华文彩云","仿宋","华文行楷",
"方正舒体","TimesNewRoman","Serif","Monospaced",
"SonsSerif","Garamond"
};
privateIconitems[];
privateStringtipText[]={
"新建",
"打开",
"保存",
"铅笔画",
"直线",
"空心矩形",
"填充矩形",
"空心椭圆",
"填充椭圆",
"空心圆形",
"填充圆形",
"空心圆角矩形",
"填充圆角矩形",
"橡皮擦",
"颜色",
"线条",
"输入文本"
};
publicMiniDrawPad()//构造函数
{
super("绘图板姓名:
郄利飞学号:
201101010303");
JMenuBarbar=newJMenuBar();
JMenufileMenu=newJMenu("文件(F)");
fileMenu.setMnemonic('F');
//新建文件菜单项
JMenuItemnewItem=newJMenuItem("新建-N");
newItem.setMnemonic('N');
newItem.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_N,
InputEvent.CTRL_MASK));
newItem.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
newFile();//如果被触发,则调用新建文件函数段
}
});
fileMenu.add(newItem);
//保存文件菜单项
JMenuItemsaveItem=newJMenuItem("保存-S");
saveItem.setMnemonic('S');
saveItem.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_S,
InputEvent.CTRL_MASK));
saveItem.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
saveFile();//如果被触发,则调用保存文件函数段
}
});
fileMenu.add(saveItem);
//打开文件菜单项
JMenuItemopenItem=newJMenuItem("打开-L");
openItem.setMnemonic('L');
openItem.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_L,
InputEvent.CTRL_MASK));
openItem.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
openFile();//如果被触发,则调用打开文件函数段
}
});
fileMenu.add(openItem);
fileMenu.addSeparator();
//退出菜单项
JMenuItemexitItem=newJMenuItem("退出-X");
exitItem.setMnemonic('X');
exitItem.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_X,
InputEvent.CTRL_MASK));
exitItem.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.exit(0);//如果被触发,则退出画图板程序
}
});
fileMenu.add(exitItem);
bar.add(fileMenu);
JMenusetMenu=newJMenu("设置(E)");
setMenu.setMnemonic('E');
JMenuItemcolorItem=newJMenuItem("颜色-O");
colorItem.setMnemonic('O');
colorItem.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
chooseColor();//如果被触发,则调用选择颜色函数段
}
});
setMenu.add(colorItem);
bar.add(setMenu);
JMenuItemstrokeItem=newJMenuItem("线条-K");
strokeItem.setMnemonic('K');
strokeItem.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
setStroke();
}
});
setMenu.add(strokeItem);
bar.add(setMenu);
JMenuhelpMenu=newJMenu("帮助(H)");
helpMenu.setMnemonic('H');
JMenuItemaboutItem=newJMenuItem("请联系我");
aboutItem.setMnemonic('A');
aboutItem.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
JOptionPane.showMessageDialog(null,
"西安石油大学信息与计算科学\n郄利飞\n\n\n",
"帮助",
JOptionPane.INFORMATION_MESSAGE);
}
});
helpMenu.add(aboutItem);
bar.add(helpMenu);
items=newImageIcon[names.length];
//创建各种基本图形的按钮
drawingArea=newDrawPanel();
choices=newJButton[names.length];
buttonPanel=newJToolBar(JToolBar.VERTICAL);
buttonPanel=newJToolBar(JToolBar.HORIZONTAL);
ButtonHandlerhandler=newButtonHandler();
ButtonHandler1handler1=newButtonHandler1();
for(inti=0;iitems[i]=newImageIcon(names[i]+".gif");
choices[i]=newJButton("",items[i]);
choices[i].setToolTipText(tipText[i]);
choices[i].setPreferredSize(newDimension(33,33));
buttonPanel.add(choices[i]);
}
//将动作侦听器加入按钮里面
for(inti=3;ichoices[i].addActionListener(handler);
}
choices[0].addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
newFile();
}
});
choices[1].addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
openFile();
}
});
choices[2].addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
saveFile();
}
});
choices[choices.length-3].addActionListener(handler1);
choices[choices.length-2].addActionListener(handler1);
choices[choices.length-1].addActionListener(handler1);
//字体风格选择
styles=newJComboBox(styleNames);
styles.setMaximumRowCount(8);
styles.addItemListener(
newItemListener(){
publicvoiditemStateChanged(ItemEvente){
style1=styleNames[styles.getSelectedIndex()];
}
});
//字体选择
bold=newJCheckBox("粗体");
italic=newJCheckBox("斜体");
checkBoxHandlercHandler=newcheckBoxHandler();
bold.addItemListener(cHandler);
italic.addItemListener(cHandler);
JPanelwordPanel=newJPanel();
buttonPanel.add(bold);
buttonPanel.add(italic);
buttonPanel.add(styles);
styles.setMinimumSize(newDimension(50,20));
styles.setMaximumSize(newDimension(100,20));
Containerc=getContentPane();
super.setJMenuBar(bar);
c.add(buttonPanel,BorderLayout.NORTH);
c.add(drawingArea,BorderLayout.CENTER);
statusBar=newJLabel();
c.add(statusBar,BorderLayout.SOUTH);
statusBar.setText("欢迎进入我的绘图板界面!
!
!
:
)");
createNewItem();
setSize(width,height);
show();
}
//按钮侦听器ButtonHanler类,内部类,用来侦听基本按钮的操作
publicclassButtonHandlerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
for(intj=3;j