?? minidrawpad.java
字號:
package drawingpanel;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 //主類,擴展了JFrame類,用來生成主界面 { private ObjectInputStream input; private ObjectOutputStream output; //定義輸入輸出流,用來調用和保存圖像文件 private JButton 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 " }; //可供選擇的字體項 //當然這里的靈活的結構可以讓讀者自己隨意添加系統支持的字體 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 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; //顯示鼠標狀態的提示條 private DrawPanel drawingArea; //畫圖區域 private int width=800,height=550; //定義畫圖區域初始大小 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; //設置畫筆粗細,默認值為1.0f JCheckBox bold,italic; //定義字體風格選擇框 //bold為粗體,italic為斜體,二者可以同時使用 JComboBox styles; public MiniDrawPad() //構造函數 { 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(); //如果被觸發,則調用新建文件函數段 } } ); fileMenu.add(newItem);//保存文件菜單項 JMenuItem saveItem=new JMenuItem("Save"); saveItem.setMnemonic('S'); saveItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { saveFile(); //如果被觸發,則調用保存文件函數段 } } ); fileMenu.add(saveItem);//打開文件菜單項 JMenuItem loadItem=new JMenuItem("Load"); loadItem.setMnemonic('L'); loadItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { loadFile(); //如果被觸發,則調用打開文件函數段 } } ); fileMenu.add(loadItem); fileMenu.addSeparator();//退出菜單項 JMenuItem exitItem=new JMenuItem("Exit"); exitItem.setMnemonic('X'); exitItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit(0); //如果被觸發,則退出畫圖板程序 } } ); fileMenu.add(exitItem); bar.add(fileMenu);//設置顏色菜單條 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(){ public void actionPerformed(ActionEvent e) { 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(){ public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "This is a mini drawing pad!\nCopyright (c) 2002 Tsinghua University ", " 畫圖板程序說明 ", JOptionPane.INFORMATION_MESSAGE ); } } ); helpMenu.add(aboutItem); 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=0;i<choices.length;i++) {//items[i]=new ImageIcon( MiniDrawPad.class.getResource(names[i] +".gif")); //如果在jbuilder下運行本程序,則應該用這條語句導入圖片 items[i]=new ImageIcon(names[i] + ".gif"); //默認的在jdk或者jcreator下運行,用此語句導入圖片 choices[i]=new JButton("",items[i]); choices[i].setToolTipText(tipText[i]); buttonPanel.add(choices[i]); }//將動作偵聽器加入按鈕里面 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("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類,內部類,用來偵聽基本按鈕的操作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類,用來偵聽顏色選擇、畫筆粗細設置、文字輸入按鈕的操作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(); } } }//鼠標事件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( "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() + "]"); } }//鼠標事件mouseB類繼承了MouseMotionAdapter,用來完成鼠標拖動和鼠標移動時的相應操作 class mouseB extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) {statusBar.setText(" Mouse Dragged @:[" + e.getX() + ", " + e.getY() + "]"); if(currentChoice==3||currentChoice==13) { itemList[index-1].x1=itemList[index].x2=itemList[index].x1=e.getX();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -