非常值得学习的java 绘图板源代码

非常值得学习的java 绘图板源代码下载地址:http://download.csdn.net/source/2371150

[java]  view plain copy
  1. package minidrawpad;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. import java.io.InputStreamReader;  
  5. import java.io.Reader;  
  6. import javax.swing.*;  
  7. // 主界面类  
  8. public class DrawPad extends JFrame implements ActionListener {  
  9.     /** 
  10.      * @param FileName DrawPad 
  11.      * @author Liu Jun Guang s 
  12.      * @param V 1.0.0  
  13.      */  
  14.     private static final long serialVersionUID = -2551980583852173918L;  
  15.     private JToolBar buttonpanel;//定义按钮面板  
  16.     private JMenuBar bar ;//定义菜单条  
  17.     private JMenu file,color,stroke,help;//定义菜单  
  18.     private JMenuItem newfile,openfile,savefile,exit;//file 菜单中的菜单项  
  19.     private JMenuItem helpin,helpmain,colorchoice,strokeitem;//help 菜单中的菜单项  
  20.     private Icon nf,sf,of;//文件菜单项的图标对象  
  21.     private JLabel startbar;//状态栏  
  22.     private DrawArea drawarea;//画布类的定义  
  23.     private Help  helpobject; //定义一个帮助类对象  
  24.     private FileClass fileclass ;//文件对象  
  25.     String[] fontName;   
  26.     //定义工具栏图标的名称  
  27.     private String names[] = {"newfile","openfile","savefile","pen","line"  
  28.             ,"rect","frect","oval","foval","circle","fcircle"  
  29.             ,"roundrect","froundrect","rubber","color"  
  30.             ,"stroke","word"};//定义工具栏图标的名称  
  31.     private Icon icons[];//定义图象数组  
  32.       
  33.     private String tiptext[] = {//这里是鼠标移到相应的按钮上给出相应的提示  
  34.             "新建一个图片","打开图片","保存图片","随笔画","画直线"  
  35.             ,"画空心的矩形","填充矩形","画空心的椭圆","填充椭圆"  
  36.             ,"画空心的圆","填充圆","画圆角矩形","填充圆角矩形"  
  37.             ,"橡皮擦","颜色","选择线条的粗细","文字的输入"};  
  38.      JButton button[];//定义工具条中的按钮组  
  39.     private JCheckBox bold,italic;//工具条字体的风格(复选框)  
  40.     private JComboBox stytles ;//工具条中的字体的样式(下拉列表)  
  41.     public DrawPad(String string) {  
  42.         // TODO 主界面的构造函数  
  43.         super(string);  
  44.         //菜单的初始化  
  45.         file = new JMenu("文件");  
  46.         color = new JMenu("颜色");  
  47.         stroke = new JMenu("画笔");  
  48.         help = new JMenu("帮助");  
  49.         bar = new JMenuBar();//菜单条的初始化  
  50.           
  51.         //菜单条添加菜单  
  52.         bar.add(file);  
  53.         bar.add(color);  
  54.         bar.add(stroke);  
  55.         bar.add(help);  
  56.           
  57.         //界面中添加菜单条  
  58.         setJMenuBar(bar);  
  59.           
  60.         //菜单中添加快捷键  
  61.         file.setMnemonic('F');//既是ALT+“F”  
  62.         color.setMnemonic('C');//既是ALT+“C”  
  63.         stroke.setMnemonic('S');//既是ALT+“S”  
  64.         help.setMnemonic('H');//既是ALT+“H”  
  65.          
  66.         //File 菜单项的初始化  
  67.         try {  
  68.             Reader reader = new InputStreamReader(getClass().getResourceAsStream("/icon"));//读取文件以类路径为基准  
  69.         } catch (Exception e) {  
  70.             // TODO 文件读取错误  
  71.             JOptionPane.showMessageDialog(this,"图片读取错误!","错误",JOptionPane.ERROR_MESSAGE);  
  72.         }  
  73.         nf = new ImageIcon(getClass().getResource("/icon/newfile.jpg"));//创建图表  
  74.         sf = new ImageIcon(getClass().getResource("/icon/savefile.jpg"));  
  75.         of = new ImageIcon(getClass().getResource("/icon/openfile.jpg"));  
  76.         newfile = new JMenuItem("新建",nf);  
  77.         openfile = new JMenuItem("打开",of );  
  78.         savefile = new JMenuItem("保存",sf);  
  79.         exit = new JMenuItem("退出");  
  80.           
  81.         //File 菜单中添加菜单项  
  82.         file.add(newfile);  
  83.         file.add(openfile);  
  84.         file.add(savefile);  
  85.         file.add(exit);  
  86.           
  87.         //File 菜单项添加快捷键  
  88.         newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));  
  89.         openfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));  
  90.         savefile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));  
  91.         exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));  
  92.          
  93.         //File 菜单项的注册监听  
  94.         newfile.addActionListener(this);  
  95.         openfile.addActionListener(this);  
  96.         savefile.addActionListener(this);  
  97.         exit.addActionListener(this);  
  98.           
  99.         //Color 菜单项的初始化  
  100.         colorchoice = new JMenuItem("调色板");  
  101.         colorchoice.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));  
  102.         colorchoice.addActionListener(this);  
  103.         color.add(colorchoice);  
  104.         //Help 菜单项的初始化  
  105.         helpmain = new JMenuItem("帮助主题");  
  106.         helpin = new JMenuItem("关于小小绘图板");  
  107.          
  108.         //Help 菜单中添加菜单项  
  109.         help.add(helpmain);  
  110.         help.addSeparator();//添加分割线  
  111.         help.add(helpin);  
  112.           
  113.         //Help 菜单项的注册监听  
  114.         helpin.addActionListener(this);  
  115.         helpmain.addActionListener(this);  
  116.           
  117.         //Stroke 菜单项的初始化  
  118.         strokeitem = new JMenuItem("设置画笔");  
  119.         strokeitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));  
  120.         stroke.add(strokeitem);  
  121.         strokeitem.addActionListener(this);  
  122.           
  123.         //工具栏的初始化  
  124.         buttonpanel = new JToolBar( JToolBar.HORIZONTAL);  
  125.         icons = new ImageIcon[names.length];  
  126.         button = new JButton[names.length];  
  127.         for(int i = 0 ;i
  128.         {  
  129.             icons[i] = new ImageIcon(getClass().getResource("/icon/"+names[i]+".jpg"));//获得图片(以类路径为基准)  
  130.             button[i] = new JButton("",icons[i]);//创建工具条中的按钮  
  131.             button[i].setToolTipText(tiptext[i]);//这里是鼠标移到相应的按钮上给出相应的提示  
  132.             buttonpanel.add(button[i]);  
  133.             button[i].setBackground(Color.red);  
  134.             if(i<3)button[i].addActionListener(this);  
  135.             else if(i<=16) button[i].addActionListener(this);  
  136.         }  
  137.        CheckBoxHandler CHandler = new CheckBoxHandler();//字体样式处理类  
  138.        bold = new  JCheckBox("粗体");   
  139.        bold.setFont(new Font(Font.DIALOG,Font.BOLD,30));//设置字体  
  140.        bold.addItemListener(CHandler);//bold注册监听  
  141.        italic = new  JCheckBox("斜体");  
  142.        italic.addItemListener(CHandler);//italic注册监听  
  143.        italic.setFont(new Font(Font.DIALOG,Font.ITALIC,30));//设置字体  
  144.        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();//计算机上字体可用的名称  
  145.        fontName = ge.getAvailableFontFamilyNames();  
  146.        stytles = new JComboBox(fontName);//下拉列表的初始化  
  147.        stytles.addItemListener(CHandler);//stytles注册监听  
  148.        stytles.setMaximumSize(new Dimension(400,50));//设置下拉列表的最大尺寸  
  149.        stytles.setMinimumSize(new  Dimension(250,40));  
  150.        stytles.setFont(new Font(Font.DIALOG,Font.BOLD,20));//设置字体  
  151.         
  152.       //工具栏中添加字体式样  
  153.        buttonpanel.add(bold);  
  154.        buttonpanel.add(italic);  
  155.        buttonpanel.add(stytles);  
  156.          
  157.        //状态栏的初始化  
  158.         startbar = new JLabel("我的小小绘图板");  
  159.          
  160.           
  161.         //绘画区的初始化  
  162.         drawarea = new DrawArea(this);  
  163.         helpobject = new Help(this);  
  164.         fileclass = new FileClass(this,drawarea);  
  165.          
  166.           
  167.         Container con = getContentPane();//得到内容面板  
  168.         con.add(buttonpanel, BorderLayout.NORTH);  
  169.         con.add(drawarea,BorderLayout.CENTER);  
  170.         con.add(startbar,BorderLayout.SOUTH);  
  171.         Toolkit tool = getToolkit();//得到一个Tolkit类的对象(主要用于得到屏幕的大小)  
  172.         Dimension dim = tool.getScreenSize();//得到屏幕的大小 (返回Dimension对象)  
  173.         setBounds(40,40,dim.width-70,dim.height-100);  
  174.         setVisible(true);  
  175.         validate();  
  176.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  177.     }  
  178.     //设置状态栏显示的字符  
  179.     public void setStratBar(String s) {  
  180.         startbar.setText(s);  
  181.     }  
  182.     public void actionPerformed(ActionEvent e) {  
  183.         // TODO 事件的处理  
  184.         for(int i = 3; i<=13;i++)  
  185.         {  
  186.             if(e.getSource() ==button[i])  
  187.             {  
  188.                 drawarea.setCurrentChoice(i);  
  189.                 drawarea.createNewitem();  
  190.                 drawarea.repaint();  
  191.             }  
  192.               
  193.         }  
  194.         if(e.getSource() == newfile||e.getSource() == button[0])//新建  
  195.         {fileclass.newFile();}  
  196.         else if(e.getSource() == openfile||e.getSource() == button[1])//打开  
  197.         {fileclass.openFile();}  
  198.         else if(e.getSource() == savefile||e.getSource() == button[2])//保存  
  199.         {fileclass.saveFile();}  
  200.         else if(e.getSource() == exit)//退出程序  
  201.         {System.exit(0);}  
  202.         else if(e.getSource() == colorchoice||e.getSource() == button[14])//弹出颜色对话框  
  203.         {  
  204.             drawarea.chooseColor();//颜色的选择  
  205.         }  
  206.         else if(e.getSource() == button[15]||e.getSource()==strokeitem)//画笔粗细  
  207.         {  
  208.             drawarea.setStroke();//画笔粗细的调整  
  209.         }  
  210.         else if(e.getSource() == button[16])//添加文字  
  211.         {   JOptionPane.showMessageDialog(null"请单击画板以确定输入文字的位置!","提示"  
  212.                 ,JOptionPane.INFORMATION_MESSAGE);   
  213.             drawarea.setCurrentChoice(14);  
  214.             drawarea.createNewitem();  
  215.             drawarea.repaint();  
  216.         }  
  217.           
  218.         else if(e.getSource() == helpin)//帮助信息  
  219.         {helpobject.AboutBook();}  
  220.         else if(e.getSource() == helpmain)//帮助主题  
  221.         {helpobject.MainHeip();}  
  222.           
  223.           
  224.     }  
  225.       
  226.     //字体样式处理类(粗体、斜体、字体名称)  
  227.     public  class CheckBoxHandler implements ItemListener  
  228.     {  
  229.           
  230.         public void itemStateChanged(ItemEvent ie) {  
  231.             // TODO 字体样式处理类(粗体、斜体、字体名称)  
  232.             if(ie.getSource() == bold)//字体粗体  
  233.             {  
  234.                 if(ie.getStateChange() == ItemEvent.SELECTED)  
  235.                 drawarea.setFont(1, Font.BOLD);  
  236.                 else   
  237.                     drawarea.setFont(1, Font.PLAIN);  
  238.             }  
  239.             else if(ie.getSource() == italic)//字体斜体  
  240.             {  
  241.                 if(ie.getStateChange() == ItemEvent.SELECTED)  
  242.                     drawarea.setFont(2, Font.ITALIC);  
  243.                 else drawarea.setFont(2, Font.PLAIN);  
  244.                   
  245.             }  
  246.             else if(ie.getSource() == stytles)//字体的名称  
  247.             {     
  248.                 drawarea.stytle = fontName[stytles.getSelectedIndex()];  
  249.             }  
  250.         }  
  251.           
  252.     }  
  253. }  

java 绘图板源代码下载地址:http://download.csdn.net/source/2371150 

[java]  view plain copy
  1. package minidrawpad;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. import javax.swing.*;  
  5. import java.awt.event.MouseMotionAdapter;  
  6. //绘图区类(各种图形的绘制和鼠标事件)  
  7. public class DrawArea extends JPanel{  
  8.     DrawPad drawpad =null;  
  9.     Drawing[] itemList =new Drawing[5000];; //绘制图形类  
  10.       
  11.     private int currentChoice = 3;//设置默认基本图形状态为随笔画  
  12.     int index = 0;//当前已经绘制的图形数目  
  13.     private Color color = Color.black;//当前画笔的颜色  
  14.     int R,G,B;//用来存放当前颜色的彩值  
  15.     int f1,f2;//用来存放当前字体的风格  
  16.     String stytle ;//存放当前字体  
  17.     float stroke = 1.0f;//设置画笔的粗细 ,默认的是 1.0  
  18.     DrawArea(DrawPad dp) {  
  19.         drawpad = dp;  
  20.         // 把鼠标设置成十字形  
  21.         setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));  
  22.         // setCursor 设置鼠标的形状 ,getPredefinedCursor()返回一个具有指定类型的光标的对象  
  23.         setBackground(Color.white);// 设置绘制区的背景是白色  
  24.         addMouseListener(new MouseA());// 添加鼠标事件  
  25.         addMouseMotionListener(new MouseB());  
  26.          createNewitem();  
  27.           
  28.     }  
  29.       
  30.     public void paintComponent(Graphics g){  
  31.         super.paintComponent(g);  
  32.         Graphics2D g2d = (Graphics2D)g;//定义随笔画  
  33.         int  j = 0;  
  34.         while(j<=index)  
  35.         {  
  36.             draw(g2d,itemList[j]);  
  37.             j++;  
  38.         }  
  39.           
  40.     }  
  41.     void draw(Graphics2D g2d , Drawing i)  
  42.     {  
  43.         i.draw(g2d);//将画笔传到个各类的子类中,用来完成各自的绘图  
  44.     }  
  45.       
  46.     //新建一个图形的基本单元对象的程序段  
  47.     void createNewitem(){  
  48.         if(currentChoice == 14)//字体的输入光标相应的设置为文本输入格式  
  49.             setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));  
  50.         else    setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));  
  51.             switch(currentChoice){  
  52.             case 3: itemList[index] = new Pencil();break;  
  53.             case 4: itemList[index] = new Line();break;  
  54.             case 5: itemList[index] = new Rect();break;  
  55.             case 6: itemList[index] = new fillRect();break;  
  56.             case 7: itemList[index] = new Oval();break;  
  57.             case 8: itemList[index] = new fillOval();break;  
  58.             case 9: itemList[index] = new Circle();break;  
  59.             case 10: itemList[index] = new fillCircle();break;  
  60.             case 11: itemList[index] = new RoundRect();break;  
  61.             case 12: itemList[index] = new fillRoundRect();break;  
  62.             case 13: itemList[index] = new Rubber();break;  
  63.             case 14: itemList[index] = new Word();break;  
  64.         }  
  65.       itemList[index].type = currentChoice;  
  66.       itemList[index].R = R;  
  67.       itemList[index].G = G;  
  68.       itemList[index].B = B;  
  69.       itemList[index].stroke = stroke ;  
  70.        
  71.     }  
  72.      
  73.     public void setIndex(int x){//设置index的接口  
  74.         index = x;  
  75.     }  
  76.     public int getIndex(){//设置index的接口  
  77.         return index ;  
  78.     }  
  79.     public void setColor(Color color)//设置颜色的值  
  80.     {  
  81.         this.color = color;   
  82.     }  
  83.     public void setStroke(float f)//设置画笔粗细的接口  
  84.     {  
  85.         stroke = f;  
  86.     }  
  87.     public void chooseColor()//选择当前颜色  
  88.     {  
  89.         color = JColorChooser.showDialog(drawpad, "请选择颜色", color);  
  90.         try {  
  91.             R = color.getRed();  
  92.             G = color.getGreen();  
  93.             B = color.getBlue();  
  94.         } catch (Exception e) {  
  95.             R = 0;  
  96.             G = 0;  
  97.             B = 0;  
  98.         }  
  99.         itemList[index].R = R;  
  100.         itemList[index].G = G;  
  101.         itemList[index].B = B;  
  102.     }  
  103.     public void setStroke()//画笔粗细的调整  
  104.     {  
  105.         String input ;  
  106.         input = JOptionPane.showInputDialog("请输入画笔的粗细( >0 )");  
  107.         try {  
  108.             stroke = Float.parseFloat(input);  
  109.               
  110.         } catch (Exception e) {  
  111.             stroke = 1.0f;  
  112.               
  113.         }itemList[index].stroke = stroke;  
  114.           
  115.     }  
  116.     public void setCurrentChoice(int i )//文字的输入  
  117.     {  
  118.         currentChoice = i;  
  119.     }  
  120.       
  121.     public void setFont(int  i,int font)//设置字体  
  122.     {  
  123.         if(i == 1)  
  124.         {  
  125.             f1 = font;   
  126.         }  
  127.         else   
  128.             f2 = font;  
  129.     }  
  130. // TODO 鼠标事件MouseA类继承了MouseAdapter   
  131. //用来完成鼠标的响应事件的操作(鼠标的按下、释放、单击、移动、拖动、何时进入一个组件、何时退出、何时滚动鼠标滚轮 )  
  132. class MouseA extends MouseAdapter  
  133. {  
  134.       
  135.     @Override  
  136.     public void mouseEntered(MouseEvent me) {  
  137.         // TODO 鼠标进入  
  138.         drawpad.setStratBar("鼠标进入在:["+me.getX()+" ,"+me.getY()+"]");  
  139.     }  
  140.     @Override  
  141.     public void mouseExited(MouseEvent me) {  
  142.         // TODO 鼠标退出  
  143.         drawpad.setStratBar("鼠标退出在:["+me.getX()+" ,"+me.getY()+"]");  
  144.     }  
  145.     @Override  
  146.     public void mousePressed(MouseEvent me) {  
  147.         // TODO 鼠标按下  
  148.         drawpad.setStratBar("鼠标按下在:["+me.getX()+" ,"+me.getY()+"]");//设置状态栏提示  
  149.           
  150.         itemList[index].x1 = itemList[index].x2 = me.getX();  
  151.         itemList[index].y1 = itemList[index].y2 = me.getY();  
  152.           
  153.         //如果当前选择为随笔画或橡皮擦 ,则进行下面的操作  
  154.         if(currentChoice == 3||currentChoice ==13){  
  155.             itemList[index].x1 = itemList[index].x2 = me.getX();  
  156.             itemList[index].y1 = itemList[index].y2 = me.getY();  
  157.             index++;  
  158.             createNewitem();//创建新的图形的基本单元对象  
  159.         }  
  160.         //如果选择图形的文字输入,则进行下面的操作  
  161.         if(currentChoice == 14){  
  162.             itemList[index].x1 = me.getX();  
  163.             itemList[index].y1 = me.getY();  
  164.             String input ;  
  165.             input = JOptionPane.showInputDialog("请输入你要写入的文字!");  
  166.             itemList[index].s1 = input;  
  167.             itemList[index].x2 = f1;  
  168.             itemList[index].y2 = f2;  
  169.             itemList[index].s2 = stytle;  
  170.               
  171.             index++;  
  172.             currentChoice = 14;  
  173.             createNewitem();//创建新的图形的基本单元对象  
  174.             repaint();  
  175.         }  
  176.               
  177.     }  
  178.     @Override  
  179.     public void mouseReleased(MouseEvent me) {  
  180.         // TODO 鼠标松开  
  181.         drawpad.setStratBar("鼠标松开在:["+me.getX()+" ,"+me.getY()+"]");  
  182.         if(currentChoice == 3||currentChoice ==13){  
  183.             itemList[index].x1 = me.getX();  
  184.             itemList[index].y1 = me.getY();  
  185.         }  
  186.         itemList[index].x2 = me.getX();  
  187.         itemList[index].y2 = me.getY();  
  188.         repaint();  
  189.         index++;  
  190.         createNewitem();//创建新的图形的基本单元对象  
  191.     }  
  192. }  
  193.     // 鼠标事件MouseB继承了MouseMotionAdapter  
  194.     // 用来处理鼠标的滚动与拖动  
  195.     class MouseB extends MouseMotionAdapter {  
  196.       public void mouseDragged(MouseEvent me)//鼠标的拖动  
  197.       {  
  198.           drawpad.setStratBar("鼠标拖动在:["+me.getX()+" ,"+me.getY()+"]");  
  199.           if(currentChoice == 3||currentChoice ==13){  
  200.               itemList[index-1].x1 = itemList[index].x2 = itemList[index].x1 =me.getX();  
  201.               itemList[index-1].y1 = itemList[index].y2 = itemList[index].y1 = me.getY();  
  202.               index++;  
  203.               createNewitem();//创建新的图形的基本单元对象  
  204.           }  
  205.           else   
  206.           {  
  207.               itemList[index].x2 = me.getX();  
  208.               itemList[index].y2 = me.getY();  
  209.           }  
  210.           repaint();  
  211.       }  
  212.       public void mouseMoved(MouseEvent me)//鼠标的移动  
  213.       {  
  214.           drawpad.setStratBar("鼠标移动在:["+me.getX()+" ,"+me.getY()+"]");  
  215.       }  
  216.     }  
  217. }  
 

[java]  view plain copy
  1. package minidrawpad;  
  2. import javax.swing.UIManager;  
  3. //主函数类  
  4. public class MiniDrawPad {  
  5.     /** 
  6.      * @param FileName DrawPad 
  7.      * @author Liu Jun Guang  
  8.      * @param V 1.0.0  
  9.      */  
  10.     public static void main(String[] args) {  
  11.         // TODO 主函数  
  12.         /*try {//将界面设置为当前windows界面风格 
  13.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
  14.         } catch (Exception e) {}*/  
  15.         DrawPad drawpad = new DrawPad("小小绘图板");  
  16.         
  17.     }  
  18. }  
 

java 绘图板源代码下载地址:http://download.csdn.net/source/2371150

[java]  view plain copy
  1. package minidrawpad;  
  2. import java.awt.Color;  
  3. import java.io.*;  
  4. import javax.swing.*;  
  5. import javax.swing.filechooser.*;  
  6. //文件类 (文件的打开、新建、保存)  
  7. public class FileClass {  
  8.    private DrawPad drawpad;  
  9.    DrawArea drawarea = null;  
  10.     FileClass(DrawPad dp,DrawArea da) {  
  11.         drawpad = dp;  
  12.         drawarea = da;  
  13.     }  
  14.       
  15.     public void newFile() {  
  16.         // TODO 新建图像  
  17.         drawarea.setIndex(0);  
  18.         drawarea.setCurrentChoice(3);//设置默认为随笔画  
  19.         drawarea.setColor(Color.black);//设置颜色  
  20.         drawarea.setStroke(1.0f);//设置画笔的粗细  
  21.         drawarea.createNewitem();  
  22.         drawarea.repaint();  
  23.     }  
  24.     public void openFile() {  
  25.         // TODO 打开图像  
  26.           
  27.          //JFileChooser 为用户选择文件提供了一种简单的机制  
  28.          JFileChooser filechooser = new JFileChooser();  
  29.          filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
  30.           /* FileNameExtensionFilter filter = new FileNameExtensionFilter( 
  31.                     "JPG & GIF Images", "jpg", "gif");//其中只显示 .jpg 和 .gif 图像 
  32.            filechooser.setFileFilter(filter);*/  
  33.             int returnVal = filechooser.showOpenDialog(drawpad);  
  34.               
  35.             if(returnVal == JFileChooser.CANCEL_OPTION) {//如果单击确定按钮就执行下面得程序  
  36.                return;  
  37.             }  
  38.             File fileName = filechooser.getSelectedFile();//getSelectedFile()返回选中的文件  
  39.             fileName.canRead();  
  40.             if(fileName == null || fileName.getName().equals(""))//文件名不存在时  
  41.             {  
  42.                 JOptionPane.showMessageDialog(filechooser,"文件名","请输入文件名!",JOptionPane.ERROR_MESSAGE);  
  43.             }  
  44.               
  45.             else {  
  46.                   
  47.                     try {  
  48.                         FileInputStream ifs = new FileInputStream(fileName);  
  49.                         ObjectInputStream input = new ObjectInputStream(ifs);  
  50.                           
  51.                         int countNumber = 0;  
  52.                         Drawing inputRecord;  
  53.                         countNumber = input.readInt();  
  54.                         for(int i =0;i<=countNumber;i++)  
  55.                         {  
  56.                             drawarea.setIndex(i);  
  57.                             inputRecord = (Drawing)input.readObject();  
  58.                             drawarea.itemList[i] = inputRecord;  
  59.                         }  
  60.                         drawarea.createNewitem();  
  61.                         input.close();  
  62.                         drawarea.repaint();  
  63.                     } catch (FileNotFoundException e) {  
  64.                         JOptionPane.showMessageDialog(drawpad,"没有找到源文件!","没有找到源文件",JOptionPane.ERROR_MESSAGE);  
  65.                     } catch (IOException e) {  
  66.                         JOptionPane.showMessageDialog(drawpad,"读文件是发生错误!","读取错误",JOptionPane.ERROR_MESSAGE);  
  67.                     } catch (ClassNotFoundException e) {  
  68.                         JOptionPane.showMessageDialog(drawpad,"不能创建对象!","已到文件末尾",JOptionPane.ERROR_MESSAGE);  
  69.                     }  
  70.                   
  71.             }  
  72.     }  
  73.       
  74.     //保存图像文件程序段,用到文件对(FileOupputSream)象流  
  75.     public void saveFile() {  
  76.         // TODO 保存图像  
  77.           
  78.          //JFileChooser 为用户选择文件提供了一种简单的机制  
  79.         JFileChooser filechooser = new JFileChooser();  
  80.         filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
  81.         //setFileSelectionMode()设置 JFileChooser,以允许用户只选择文件、只选择目录,或者可选择文件和目录。  
  82.         int result = filechooser.showSaveDialog(drawpad);  
  83.         if(result == JFileChooser.CANCEL_OPTION){  
  84.             return ;  
  85.         }  
  86.           
  87.         File fileName = filechooser.getSelectedFile();//getSelectedFile()返回选中的文件  
  88.         fileName.canWrite();//测试应用程序是否可以修改此抽象路径名表示的文件  
  89.         if(fileName == null || fileName.getName().equals(""))//文件名不存在时  
  90.         {  
  91.             JOptionPane.showMessageDialog(filechooser,"文件名","请输入文件名!",JOptionPane.ERROR_MESSAGE);  
  92.         }  
  93.         else {  
  94.             try {  
  95.                 fileName.delete();//删除此抽象路径名表示的文件或目录  
  96.                 FileOutputStream fos = new FileOutputStream(fileName+".xxh");//文件输出流以字节的方式输出  
  97.                 //对象输出流  
  98.                 ObjectOutputStream output = new ObjectOutputStream(fos);  
  99.                 //Drawing record;  
  100.                   
  101.                 output.writeInt(drawarea.getIndex());  
  102.                   
  103.                 for(int i = 0;i<=drawarea.getIndex() ;i++)  
  104.                 {  
  105.                     Drawing p = drawarea.itemList[i];  
  106.                     output.writeObject(p);  
  107.                     output.flush();//刷新该流的缓冲。此操作将写入所有已缓冲的输出字节,并将它们刷新到底层流中。  
  108.                                    //将所有的图形信息强制的转换成父类线性化存储到文件中      
  109.                 }  
  110.                 output.close();  
  111.                 fos.close();  
  112.             } catch (Exception e) {  
  113.                 e.printStackTrace();  
  114.             }  
  115.         }  
  116.     }  
  117. }  
 

[java]  view plain copy
  1. package minidrawpad;  
  2. import java.awt.BasicStroke;  
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics2D;  
  6. import java.io.Serializable;  
  7. //图形绘制类 用于绘制各种图形  
  8. //父类,基本图形单元,用到串行的接口,保存使用到  
  9. //公共的属性放到超类中,子类可以避免重复定义  
  10. /*类通过实现 java.io.Serializable 接口以启用其序列化功能。 
  11. 未实现此接口的类将无法使其任何状态序列化或反序列化。 
  12. 可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段, 
  13. 仅用于标识可序列化的语义。*/  
  14. public class Drawing implements Serializable {  
  15. int x1,x2,y1,y2;        //定义坐标属性  
  16. int  R,G,B;             //定义色彩属性  
  17. float stroke ;          //定义线条粗细的属性  
  18. int type;               //定义字体属性  
  19. String s1;              //定义字体的风格  
  20. String s2;              //定义字体的风格  
  21. void draw(Graphics2D g2d ){}//定义绘图函数  
  22. }  
  23. class Line extends Drawing//直线类  
  24. {  
  25.     void draw(Graphics2D g2d) {  
  26.         g2d.setPaint(new Color(R, G, B));// 为 Graphics2D 上下文设置 Paint 属性。  
  27.         // 使用为 null 的 Paint 对象调用此方法对此 Graphics2D 的当前 Paint 属性没有任何影响。  
  28.         g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,  
  29.                 BasicStroke.JOIN_BEVEL));  
  30.         // setStroke(Stroke s)为 Graphics2D 上下文设置 Stroke  
  31.         // BasicStroke 类定义针对图形图元轮廓呈现属性的一个基本集合  
  32.         // BasicStroke.CAP_ROUND使用半径等于画笔宽度一半的圆形装饰结束未封闭的子路径和虚线线段  
  33.         // BasicStroke.JOIN_BEVEL通过直线连接宽体轮廓的外角,将路径线段连接在一起。  
  34.         g2d.drawLine(x1, y1, x2, y2);// 画直线  
  35.           
  36.     }  
  37. }  
  38. class Rect extends Drawing{//矩形类  
  39.     void draw(Graphics2D g2d ){  
  40.         g2d.setPaint(new Color(R,G,B));  
  41.         g2d.setStroke(new BasicStroke(stroke));  
  42.         g2d.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));  
  43.     }  
  44. }  
  45. class fillRect extends Drawing{//实心矩形类  
  46.    void draw(Graphics2D g2d ){  
  47.         g2d.setPaint(new Color(R,G,B));  
  48.         g2d.setStroke(new BasicStroke(stroke));  
  49.         g2d.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));  
  50.     }  
  51. }  
  52. class Oval extends Drawing{//椭圆类  
  53.     void draw(Graphics2D g2d ){  
  54.         g2d.setPaint(new Color(R,G,B));  
  55.         g2d.setStroke(new BasicStroke(stroke));  
  56.         g2d.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));  
  57.     }  
  58. }  
  59. class fillOval extends Drawing{//实心椭圆类  
  60.     void draw(Graphics2D g2d ){  
  61.         g2d.setPaint(new Color(R,G,B));  
  62.         g2d.setStroke(new BasicStroke(stroke));  
  63.         g2d.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));  
  64.     }  
  65. }  
  66. class Circle extends Drawing{//圆类  
  67.     void draw(Graphics2D g2d ){  
  68.         g2d.setPaint(new Color(R,G,B));  
  69.         g2d.setStroke(new BasicStroke(stroke));  
  70.         g2d.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.max(Math.abs(x1-x2),   
  71.                 Math.abs(y1-y2)), Math.max(Math.abs(x1-x2), Math.abs(y1-y2)));  
  72.     }  
  73. }  
  74. class fillCircle extends Drawing{//实心圆类  
  75.     void draw(Graphics2D g2d ){  
  76.         g2d.setPaint(new Color(R,G,B));  
  77.         g2d.setStroke(new BasicStroke(stroke));  
  78.         g2d.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.max(Math.abs(x1-x2),  
  79.                 Math.abs(y1-y2)), Math.max(Math.abs(x1-x2), Math.abs(y1-y2)));  
  80.     }  
  81. }  
  82. class RoundRect extends Drawing{//圆角矩形类  
  83.     void draw(Graphics2D g2d ){  
  84.         g2d.setPaint(new Color(R,G,B));  
  85.         g2d.setStroke(new BasicStroke(stroke));  
  86.         g2d.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2),Math.abs(x1-x2), Math.abs(y1-y2),50,35);  
  87.     }  
  88. }  
  89. class fillRoundRect extends Drawing{//实心圆角矩形类  
  90.     void draw(Graphics2D g2d ){  
  91.         g2d.setPaint(new Color(R,G,B));  
  92.         g2d.setStroke(new BasicStroke(stroke));  
  93.         g2d.fillRoundRect(Math.min(x1, x2), Math.min(y1, y2),Math.abs(x1-x2), Math.abs(y1-y2),50,35);  
  94.     }  
  95. }  
  96. class Pencil extends Drawing{//随笔画类  
  97.     void draw(Graphics2D g2d ){  
  98.         g2d.setPaint(new Color(R,G,B));  
  99.         g2d.setStroke(new BasicStroke(stroke,BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));  
  100.         g2d.drawLine(x1, y1,x2, y2);  
  101.     }  
  102. }  
  103. class Rubber extends Drawing{//橡皮擦类  
  104.     void draw(Graphics2D g2d ){  
  105.         g2d.setPaint(new Color(255,255,255));//白色  
  106.         g2d.setStroke(new BasicStroke(stroke+4,BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));  
  107.         g2d.drawLine(x1, y1,x2, y2);  
  108.     }  
  109. }  
  110. class Word extends Drawing{//输入文字类  
  111.     void draw(Graphics2D g2d ){  
  112.         g2d.setPaint(new Color(R,G,B));  
  113.         g2d.setFont(new Font(s2,x2+y2,((int)stroke)*18));//设置字体  
  114.         if(s1 != null)  
  115.         g2d.drawString( s1, x1,y1);  
  116.     }  
  117. }  
 

java 绘图板源代码下载地址:http://download.csdn.net/source/2371150

[java]  view plain copy
  1. package minidrawpad;  
  2. import javax.swing.JFrame;  
  3. import javax.swing.JOptionPane;  
  4. //帮助菜单功能的事项类  
  5. public class Help extends JFrame {  
  6.     private DrawPad  drawpad = null;  
  7.     Help(DrawPad dp)  
  8.     {  
  9.         drawpad = dp;  
  10.     }  
  11.       
  12.     public void MainHeip()  
  13.       {  
  14.         JOptionPane.showMessageDialog(this,"小小绘图板帮助文档!","小小绘图板",JOptionPane.WARNING_MESSAGE);  
  15.       }   
  16.      public void AboutBook()  
  17.       {  
  18.         JOptionPane.showMessageDialog(drawpad,"小小绘图板"+"/n"+"    版本: 1.1.2"+"/n"  
  19.            +"    作者:  刘  军  光"+"/n"  
  20.            +"    时间:  2009/12/13","小小绘图板",JOptionPane.WARNING_MESSAGE);  
  21.       }  
  22. }  
 


用到的各种图片  请将图片放在icon文件夹下

java 绘图板源代码下载地址:http://download.csdn.net/source/2371150

circle.jpg color.jpgfcircle.jpgfoval.jpgfrect.jpgfroundrect.jpgline.jpgnewfile.jpgopenfile.jpgoval.jpgpen.jpgrect.jpgroundrect.jpgrubber.jpgsavefile.jpgstroke.jpgword.jpg

你可能感兴趣的:(JavaSE)