Java 小小繪圖板,各種圖形的繪制和文字的寫入,也可以調整文字畫筆的粗細 。還可以保存圖像,非常值得學習的java 繪圖板源代碼下載地址:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /pub/2011/11/05/java 繪圖板源代碼/
用到的各種圖片 請將圖片放在icon文件夾下
circle.jpg color.jpgfcircle.jpgfoval.jpgfrect.jpgfroundrect.jpgline.jpgnewfile.jpgopenfile.jpgoval.jpgpen.jpgrect.jpgroundrect.jpgrubber.jpgsavefile.jpgstroke.jpgword.jpg
- package minidrawpad;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.InputStreamReader;
- import java.io.Reader;
- import javax.swing.*;
- // 主界面類
- public class DrawPad extends JFrame implements ActionListener {
- /**
- * @param FileName DrawPad
- * @author Liu Jun Guang s
- * @param V 1.0.0
- */
- private static final long serialVersionUID = -2551980583852173918L;
- private JToolBar buttonpanel;//定義按鈕面板
- private JMenuBar bar ;//定義菜單條
- private JMenu file,color,stroke,help;//定義菜單
- private JMenuItem newfile,openfile,savefile,exit;//file 菜單中的菜單項
- private JMenuItem helpin,helpmain,colorchoice,strokeitem;//help 菜單中的菜單項
- private Icon nf,sf,of;//文件菜單項的圖標對象
- private JLabel startbar;//狀態欄
- private DrawArea drawarea;//畫布類的定義
- private Help helpobject; //定義一個幫助類對象
- private FileClass fileclass ;//文件對象
- String[] fontName;
- //定義工具欄圖標的名稱
- private String names[] = {"newfile","openfile","savefile","pen","line"
- ,"rect","frect","oval","foval","circle","fcircle"
- ,"roundrect","froundrect","rubber","color"
- ,"stroke","word"};//定義工具欄圖標的名稱
- private Icon icons[];//定義圖象數組
-
- private String tiptext[] = {//這裡是鼠標移到相應的按鈕上給出相應的提示
- "新建一個圖片","打開圖片","保存圖片","隨筆畫","畫直線"
- ,"畫空心的矩形","填充矩形","畫空心的橢圓","填充橢圓"
- ,"畫空心的圓","填充圓","畫圓角矩形","填充圓角矩形"
- ,"橡皮擦","顏色","選擇線條的粗細","文字的輸入"};
- JButton button[];//定義工具條中的按鈕組
- private JCheckBox bold,italic;//工具條字體的風格(復選框)
- private JComboBox stytles ;//工具條中的字體的樣式(下拉列表)
- public DrawPad(String string) {
- // TODO 主界面的構造函數
- super(string);
- //菜單的初始化
- file = new JMenu("文件");
- color = new JMenu("顏色");
- stroke = new JMenu("畫筆");
- help = new JMenu("幫助");
- bar = new JMenuBar();//菜單條的初始化
-
- //菜單條添加菜單
- bar.add(file);
- bar.add(color);
- bar.add(stroke);
- bar.add(help);
-
- //界面中添加菜單條
- setJMenuBar(bar);
-
- //菜單中添加快捷鍵
- file.setMnemonic('F');//既是ALT+“F”
- color.setMnemonic('C');//既是ALT+“C”
- stroke.setMnemonic('S');//既是ALT+“S”
- help.setMnemonic('H');//既是ALT+“H”
-
- //File 菜單項的初始化
- try {
- Reader reader = new InputStreamReader(getClass().getResourceAsStream("/icon"));//讀取文件以類路徑為基准
- } catch (Exception e) {
- // TODO 文件讀取錯誤
- JOptionPane.showMessageDialog(this,"圖片讀取錯誤!","錯誤",JOptionPane.ERROR_MESSAGE);
- }
- nf = new ImageIcon(getClass().getResource("/icon/newfile.jpg"));//創建圖表
- sf = new ImageIcon(getClass().getResource("/icon/savefile.jpg"));
- of = new ImageIcon(getClass().getResource("/icon/openfile.jpg"));
- newfile = new JMenuItem("新建",nf);
- openfile = new JMenuItem("打開",of );
- savefile = new JMenuItem("保存",sf);
- exit = new JMenuItem("退出");
-
- //File 菜單中添加菜單項
- file.add(newfile);
- file.add(openfile);
- file.add(savefile);
- file.add(exit);
-
- //File 菜單項添加快捷鍵
- newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
- openfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
- savefile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
- exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
-
- //File 菜單項的注冊監聽
- newfile.addActionListener(this);
- openfile.addActionListener(this);
- savefile.addActionListener(this);
- exit.addActionListener(this);
-
- //Color 菜單項的初始化
- colorchoice = new JMenuItem("調色板");
- colorchoice.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
- colorchoice.addActionListener(this);
- color.add(colorchoice);
- //Help 菜單項的初始化
- helpmain = new JMenuItem("幫助主題");
- helpin = new JMenuItem("關於小小繪圖板");
-
- //Help 菜單中添加菜單項
- help.add(helpmain);
- help.addSeparator();//添加分割線
- help.add(helpin);
-
- //Help 菜單項的注冊監聽
- helpin.addActionListener(this);
- helpmain.addActionListener(this);
-
- //Stroke 菜單項的初始化
- strokeitem = new JMenuItem("設置畫筆");
- strokeitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
- stroke.add(strokeitem);
- strokeitem.addActionListener(this);
-
- //工具欄的初始化
- buttonpanel = new JToolBar( JToolBar.HORIZONTAL);
- icons = new ImageIcon[names.length];
- button = new JButton[names.length];
- for(int i = 0 ;i<names.length;i++)
- {
- icons[i] = new ImageIcon(getClass().getResource("/icon/"+names[i]+".jpg"));//獲得圖片(以類路徑為基准)
- button[i] = new JButton("",icons[i]);//創建工具條中的按鈕
- button[i].setToolTipText(tiptext[i]);//這裡是鼠標移到相應的按鈕上給出相應的提示
- buttonpanel.add(button[i]);
- button[i].setBackground(Color.red);
- if(i<3)button[i].addActionListener(this);
- else if(i<=16) button[i].addActionListener(this);
- }
- CheckBoxHandler CHandler = new CheckBoxHandler();//字體樣式處理類
- bold = new JCheckBox("粗體");
- bold.setFont(new Font(Font.DIALOG,Font.BOLD,30));//設置字體
- bold.addItemListener(CHandler);//bold注冊監聽
- italic = new JCheckBox("斜體");
- italic.addItemListener(CHandler);//italic注冊監聽
- italic.setFont(new Font(Font.DIALOG,Font.ITALIC,30));//設置字體
- GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();//計算機上字體可用的名稱
- fontName = ge.getAvailableFontFamilyNames();
- stytles = new JComboBox(fontName);//下拉列表的初始化
- stytles.addItemListener(CHandler);//stytles注冊監聽
- stytles.setMaximumSize(new Dimension(400,50));//設置下拉列表的最大尺寸
- stytles.setMinimumSize(new Dimension(250,40));
- stytles.setFont(new Font(Font.DIALOG,Font.BOLD,20));//設置字體
-
- //工具欄中添加字體式樣
- buttonpanel.add(bold);
- buttonpanel.add(italic);
- buttonpanel.add(stytles);
-
- //狀態欄的初始化
- startbar = new JLabel("我的小小繪圖板");
-
-
- //繪畫區的初始化
- drawarea = new DrawArea(this);
- helpobject = new Help(this);
- fileclass = new FileClass(this,drawarea);
-
-
- Container con = getContentPane();//得到內容面板
- con.add(buttonpanel, BorderLayout.NORTH);
- con.add(drawarea,BorderLayout.CENTER);
- con.add(startbar,BorderLayout.SOUTH);
- Toolkit tool = getToolkit();//得到一個Tolkit類的對象(主要用於得到屏幕的大小)
- Dimension dim = tool.getScreenSize();//得到屏幕的大小 (返回Dimension對象)
- setBounds(40,40,dim.width-70,dim.height-100);
- setVisible(true);
- validate();
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- //設置狀態欄顯示的字符
- public void setStratBar(String s) {
- startbar.setText(s);
- }
- public void actionPerformed(ActionEvent e) {
- // TODO 事件的處理
- for(int i = 3; i<=13;i++)
- {
- if(e.getSource() ==button[i])
- {
- drawarea.setCurrentChoice(i);
- drawarea.createNewitem();
- drawarea.repaint();
- }
-
- }
- if(e.getSource() == newfile||e.getSource() == button[0])//新建
- {fileclass.newFile();}
- else if(e.getSource() == openfile||e.getSource() == button[1])//打開
- {fileclass.openFile();}
- else if(e.getSource() == savefile||e.getSource() == button[2])//保存
- {fileclass.saveFile();}
- else if(e.getSource() == exit)//退出程序
- {System.exit(0);}
- else if(e.getSource() == colorchoice||e.getSource() == button[14])//彈出顏色對話框
- {
- drawarea.chooseColor();//顏色的選擇
- }
- else if(e.getSource() == button[15]||e.getSource()==strokeitem)//畫筆粗細
- {
- drawarea.setStroke();//畫筆粗細的調整
- }
- else if(e.getSource() == button[16])//添加文字
- { JOptionPane.showMessageDialog(null, "請單擊畫板以確定輸入文字的位置!","提示"
- ,JOptionPane.INFORMATION_MESSAGE);
- drawarea.setCurrentChoice(14);
- drawarea.createNewitem();
- drawarea.repaint();
- }
-
- else if(e.getSource() == helpin)//幫助信息
- {helpobject.AboutBook();}
- else if(e.getSource() == helpmain)//幫助主題
- {helpobject.MainHeip();}
-
-
- }
-
- //字體樣式處理類(粗體、斜體、字體名稱)
- public class CheckBoxHandler implements ItemListener
- {
-
- public void itemStateChanged(ItemEvent ie) {
- // TODO 字體樣式處理類(粗體、斜體、字體名稱)
- if(ie.getSource() == bold)//字體粗體
- {
- if(ie.getStateChange() == ItemEvent.SELECTED)
- drawarea.setFont(1, Font.BOLD);
- else
- drawarea.setFont(1, Font.PLAIN);
- }
- else if(ie.getSource() == italic)//字體斜體
- {
- if(ie.getStateChange() == ItemEvent.SELECTED)
- drawarea.setFont(2, Font.ITALIC);
- else drawarea.setFont(2, Font.PLAIN);
-
- }
- else if(ie.getSource() == stytles)//字體的名稱
- {
- drawarea.stytle = fontName[stytles.getSelectedIndex()];
- }
- }
-
- }
- }