?? minidrawpad.java
字號(hào):
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class MiniDrawPad extends JFrame //主類,擴(kuò)展了JFrame類,用來(lái)生成主界面
{
private ObjectInputStream input;
private ObjectOutputStream output; //定義輸入輸出流,用來(lái)調(diào)用和保存圖像文件
private JButton choices[]; //按鈕數(shù)組,存放以下名稱的功能按鈕
private String names[]={
"New", "Open", "Save", //這三個(gè)是基本操作按鈕,包括"新建"、"打開"、"保存"
/*接下來(lái)是我們的畫圖板上面有的基本的幾個(gè)繪圖單元按鈕*/
"Pencil", //鉛筆畫,也就是用鼠標(biāo)拖動(dòng)著隨意繪圖
"Line", //繪制直線
"Rect", //繪制空心矩形
"fRect", //繪制以指定顏色填充的實(shí)心矩形
"Oval", //繪制空心橢圓
"fOval", //繪制以指定顏色填充的實(shí)心橢圓
"Circle", //繪制圓形
"fCircle", //繪制以指定顏色填充的實(shí)心圓形
"RoundRect", //繪制空心圓角矩形
"frRect", //繪制以指定顏色填充的實(shí)心圓角矩形
"Rubber", //橡皮擦,可用來(lái)擦去已經(jīng)繪制好的圖案
"Color", //選擇顏色按鈕,可用來(lái)選擇需要的顏色
"Stroke", //選擇線條粗細(xì)的按鈕,輸入需要的數(shù)值可以實(shí)現(xiàn)繪圖線條粗細(xì)的變化
"Word" //輸入文字按鈕,可以在繪圖板上實(shí)現(xiàn)文字輸入
};
private String styleNames[]={
" 宋體 " , " 隸書 " , " 華文彩云 " , " 仿宋_GB2312 " , " 華文行楷 " ,
" 方正舒體 " , " Times New Roman " , " Serif " , " Monospaced " ,
" SonsSerif " , " Garamond "
}; //可供選擇的字體項(xiàng)
//當(dāng)然這里的靈活的結(jié)構(gòu)可以讓讀者自己隨意添加系統(tǒng)支持的字體
private Icon items[];
private String tipText[]={
//這里是鼠標(biāo)移動(dòng)到相應(yīng)按鈕上面上停留時(shí)給出的提示說(shuō)明條
//讀者可以參照上面的按鈕定義對(duì)照著理解
"Draw a new picture",
"Open a saved picture",
"Save current drawing",
"Draw at will",
"Draw a straight line",
"Draw a rectangle",
"Fill a ractangle",
"Draw an oval",
"Fill an oval",
"Draw a circle",
"Fill a circle",
"Draw a round rectangle",
"Fill a round rectangle",
"Erase at will",
"Choose current drawing color",
"Set current drawing stroke",
"Write down what u want"
};
JToolBar buttonPanel ; //定義按鈕面板
private JLabel statusBar; //顯示鼠標(biāo)狀態(tài)的提示條
private DrawPanel drawingArea; //畫圖區(qū)域
private int width=800,height=550; //定義畫圖區(qū)域初始大小
drawings[] itemList=new drawings[5000]; //用來(lái)存放基本圖形的數(shù)組
private int currentChoice=3; //設(shè)置默認(rèn)畫圖狀態(tài)為隨筆畫
int index=0; //當(dāng)前已經(jīng)繪制的圖形數(shù)目
private Color color=Color.black; //當(dāng)前畫筆顏色
int R,G,B; //用來(lái)存放當(dāng)前色彩值
int f1,f2; //用來(lái)存放當(dāng)前字體風(fēng)格
String style1; //用來(lái)存放當(dāng)前字體
private float stroke=1.0f; //設(shè)置畫筆粗細(xì),默認(rèn)值為1.0f
JCheckBox bold,italic; //定義字體風(fēng)格選擇框
//bold為粗體,italic為斜體,二者可以同時(shí)使用
JComboBox styles;
public MiniDrawPad() //構(gòu)造函數(shù)
{
super("Drawing Pad");
JMenuBar bar=new JMenuBar(); //定義菜單條
JMenu fileMenu=new JMenu("File");
fileMenu.setMnemonic('F');
//新建文件菜單條
JMenuItem newItem=new JMenuItem("New");
newItem.setMnemonic('N');
newItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
newFile(); //如果被觸發(fā),則調(diào)用新建文件函數(shù)段
}
}
);
fileMenu.add(newItem);
//保存文件菜單項(xiàng)
JMenuItem saveItem=new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
saveFile(); //如果被觸發(fā),則調(diào)用保存文件函數(shù)段
}
}
);
fileMenu.add(saveItem);
//打開文件菜單項(xiàng)
JMenuItem loadItem=new JMenuItem("Load");
loadItem.setMnemonic('L');
loadItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
loadFile(); //如果被觸發(fā),則調(diào)用打開文件函數(shù)段
}
}
);
fileMenu.add(loadItem);
fileMenu.addSeparator();
//退出菜單項(xiàng)
JMenuItem exitItem=new JMenuItem("Exit");
exitItem.setMnemonic('X');
exitItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0); //如果被觸發(fā),則退出畫圖板程序
}
}
);
fileMenu.add(exitItem);
bar.add(fileMenu);
//設(shè)置顏色菜單條
JMenu colorMenu=new JMenu("Color");
colorMenu.setMnemonic('C');
//選擇顏色菜單項(xiàng)
JMenuItem colorItem=new JMenuItem("Choose Color");
colorItem.setMnemonic('O');
colorItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
chooseColor(); //如果被觸發(fā),則調(diào)用選擇顏色函數(shù)段
}
}
);
colorMenu.add(colorItem);
bar.add(colorMenu);
//設(shè)置線條粗細(xì)菜單條
JMenu strokeMenu=new JMenu("Stroke");
strokeMenu.setMnemonic('S');
//設(shè)置線條粗細(xì)菜單項(xiàng)
JMenuItem strokeItem=new JMenuItem("Set Stroke");
strokeItem.setMnemonic('K');
strokeItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
setStroke();
}
}
);
strokeMenu.add(strokeItem);
bar.add(strokeMenu);
//設(shè)置提示菜單條
JMenu helpMenu=new JMenu("Help");
helpMenu.setMnemonic('H');
//設(shè)置提示菜單項(xiàng)
JMenuItem aboutItem=new JMenuItem("About this Drawing Pad!");
aboutItem.setMnemonic('A');
aboutItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,
"This is a mini drawing pad!\nCopyright (c) 2002 Tsinghua University ",
" 畫圖板程序說(shuō)明 ",
JOptionPane.INFORMATION_MESSAGE );
}
}
);
helpMenu.add(aboutItem);
bar.add(helpMenu);
items=new ImageIcon[names.length];
//創(chuàng)建各種基本圖形的按鈕
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();
//導(dǎo)入我們需要的圖形圖標(biāo),這些圖標(biāo)都存放在與源文件相同的目錄下面
for(int i=0;i<choices.length;i++)
{//items[i]=new ImageIcon( MiniDrawPad.class.getResource(names[i] +".gif"));
//如果在jbuilder下運(yùn)行本程序,則應(yīng)該用這條語(yǔ)句導(dǎo)入圖片
items[i]=new ImageIcon(names[i] + ".gif");
//默認(rèn)的在jdk或者jcreator下運(yùn)行,用此語(yǔ)句導(dǎo)入圖片
choices[i]=new JButton("",items[i]);
choices[i].setToolTipText(tipText[i]);
buttonPanel.add(choices[i]);
}
//將動(dòng)作偵聽器加入按鈕里面
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);
//字體風(fēng)格選擇
styles=new JComboBox(styleNames);
styles.setMaximumRowCount(8);
styles.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e)
{
style1=styleNames[styles.getSelectedIndex()];
}
}
);
//字體選擇
bold=new JCheckBox("BOLD");
italic=new JCheckBox("ITALIC");
checkBoxHandler cHandler=new checkBoxHandler();
bold.addItemListener(cHandler);
italic.addItemListener(cHandler);
JPanel wordPanel=new JPanel();
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( 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);
show();
}
//按鈕偵聽器ButtonHanler類,內(nèi)部類,用來(lái)偵聽基本按鈕的操作
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();}
}
}
}
//按鈕偵聽器ButtonHanler1類,用來(lái)偵聽顏色選擇、畫筆粗細(xì)設(shè)置、文字輸入按鈕的操作
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,
"Please hit the drawing pad to choose the word input position",
"Hint",JOptionPane.INFORMATION_MESSAGE );
currentChoice=14;
createNewItem();
repaint();
}
}
}
//鼠標(biāo)事件mouseA類,繼承了MouseAdapter,用來(lái)完成鼠標(biāo)相應(yīng)事件操作
class mouseA extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{statusBar.setText(" Mouse Pressed @:[" + e.getX() +
", " + e.getY() + "]");//設(shè)置狀態(tài)提示
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
//如果當(dāng)前選擇的圖形是隨筆畫或者橡皮擦,則進(jìn)行下面的操作
if(currentChoice==3||currentChoice==13)
{
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
index++;
createNewItem();
}
//如果當(dāng)前選擇的圖形式文字輸入,則進(jìn)行下面操作
if(currentChoice==14)
{
itemList[index].x1=e.getX();
itemList[index].y1=e.getY();
String input;
input=JOptionPane.showInputDialog(
"Please input the text you want!");
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++;
createNewItem();
}
public void mouseEntered(MouseEvent e)
{
statusBar.setText(" Mouse Entered @:[" + e.getX() +
", " + e.getY() + "]");
}
public void mouseExited(MouseEvent e)
{
statusBar.setText(" Mouse Exited @:[" + e.getX() +
", " + e.getY() + "]");
}
}
//鼠標(biāo)事件mouseB類繼承了MouseMotionAdapter,用來(lái)完成鼠標(biāo)拖動(dòng)和鼠標(biāo)移動(dòng)時(shí)的相應(yīng)操作
class mouseB extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{statusBar.setText(" Mouse Dragged @:[" + e.getX() +
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -