MiniDrawPad.java
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:23k
源码类别:

Java编程

开发平台:

Java

  1. package drawingpanel;
  2. import java.awt.*;
  3. import java.awt.geom.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import java.io.*;
  7. import java.util.*;
  8. public class MiniDrawPad extends JFrame     //主类,扩展了JFrame类,用来生成主界面
  9.  {
  10.   private ObjectInputStream  input;
  11.   private ObjectOutputStream output; //定义输入输出流,用来调用和保存图像文件
  12.   private JButton choices[];         //按钮数组,存放以下名称的功能按钮
  13.   private String names[]={
  14.           "New",
  15.           "Open",
  16.           "Save",    //这三个是基本操作按钮,包括"新建"、"打开"、"保存"
  17.         /*接下来是我们的画图板上面有的基本的几个绘图单元按钮*/
  18.           "Pencil", //铅笔画,也就是用鼠标拖动着随意绘图
  19.           "Line", //绘制直线
  20.           "Rect", //绘制空心矩形
  21.           "fRect", //绘制以指定颜色填充的实心矩形
  22.           "Oval", //绘制空心椭圆
  23.           "fOval", //绘制以指定颜色填充的实心椭圆
  24.           "Circle", //绘制圆形
  25.           "fCircle", //绘制以指定颜色填充的实心圆形
  26.           "RoundRect", //绘制空心圆角矩形
  27.           "frRect", //绘制以指定颜色填充的实心圆角矩形
  28.           "Rubber", //橡皮擦,可用来擦去已经绘制好的图案
  29.           "Color", //选择颜色按钮,可用来选择需要的颜色
  30.           "Stroke", //选择线条粗细的按钮,输入需要的数值可以实现绘图线条粗细的变化
  31.           "Word" //输入文字按钮,可以在绘图板上实现文字输入
  32.           };
  33.   private String styleNames[]={
  34.             " 宋体 " , " 隶书 " , " 华文彩云 " , " 仿宋_GB2312 " , " 华文行楷 " ,
  35.             " 方正舒体 " , " Times New Roman " , " Serif " , " Monospaced " ,
  36.             " SonsSerif " , " Garamond "
  37.             };            //可供选择的字体项
  38.                           //当然这里的灵活的结构可以让读者自己随意添加系统支持的字体
  39.   private Icon items[];
  40.   private String tipText[]={
  41.                   //这里是鼠标移动到相应按钮上面上停留时给出的提示说明条
  42.                   //读者可以参照上面的按钮定义对照着理解
  43.                 "Draw a new picture",
  44.                 "Open a saved picture",
  45.                 "Save current drawing",
  46.                 "Draw at will",
  47.                 "Draw a straight line",
  48.                 "Draw a rectangle",
  49.                 "Fill a ractangle",
  50.                 "Draw an oval",
  51.                 "Fill an oval",
  52.                 "Draw a circle",
  53.                 "Fill a circle",
  54.                 "Draw a round rectangle",
  55.                 "Fill a round rectangle",
  56.                 "Erase at will",
  57.                 "Choose current drawing color",
  58.                 "Set current drawing stroke",
  59.                 "Write down what u want"
  60.               };
  61.   JToolBar buttonPanel ;        //定义按钮面板
  62.   private JLabel statusBar;            //显示鼠标状态的提示条
  63.   private DrawPanel drawingArea;       //画图区域
  64.   private int width=800,height=550;    //定义画图区域初始大小
  65.   drawings[] itemList=new drawings[5000]; //用来存放基本图形的数组
  66.   private int currentChoice=3;            //设置默认画图状态为随笔画
  67.   int index=0;                         //当前已经绘制的图形数目
  68.   private Color color=Color.black;     //当前画笔颜色
  69.   int R,G,B;                           //用来存放当前色彩值
  70.   int f1,f2;                  //用来存放当前字体风格
  71.   String style1;              //用来存放当前字体
  72.   private float stroke=1.0f;  //设置画笔粗细,默认值为1.0f
  73.   JCheckBox bold,italic;      //定义字体风格选择框
  74.                               //bold为粗体,italic为斜体,二者可以同时使用
  75.   JComboBox styles;
  76.   public MiniDrawPad()        //构造函数
  77.   {
  78.    super("Drawing Pad");
  79.    JMenuBar bar=new JMenuBar(); //定义菜单条
  80.    JMenu fileMenu=new JMenu("File");
  81.    fileMenu.setMnemonic('F');
  82. //新建文件菜单条
  83.    JMenuItem newItem=new JMenuItem("New");
  84.    newItem.setMnemonic('N');
  85.    newItem.addActionListener(
  86.           new ActionListener(){
  87.                   public void actionPerformed(ActionEvent e)
  88.                   {
  89.                    newFile(); //如果被触发,则调用新建文件函数段
  90.                   }
  91.           }
  92.    );
  93.    fileMenu.add(newItem);
  94. //保存文件菜单项
  95.    JMenuItem saveItem=new JMenuItem("Save");
  96.    saveItem.setMnemonic('S');
  97.    saveItem.addActionListener(
  98.           new ActionListener(){
  99.                   public void actionPerformed(ActionEvent e)
  100.                   {
  101.                    saveFile(); //如果被触发,则调用保存文件函数段
  102.                   }
  103.           }
  104.    );
  105.    fileMenu.add(saveItem);
  106. //打开文件菜单项
  107.    JMenuItem loadItem=new JMenuItem("Load");
  108.    loadItem.setMnemonic('L');
  109.    loadItem.addActionListener(
  110.           new ActionListener(){
  111.                   public void actionPerformed(ActionEvent e)
  112.                   {
  113.                    loadFile(); //如果被触发,则调用打开文件函数段
  114.                   }
  115.           }
  116.    );
  117.    fileMenu.add(loadItem);
  118.    fileMenu.addSeparator();
  119. //退出菜单项
  120.    JMenuItem exitItem=new JMenuItem("Exit");
  121.    exitItem.setMnemonic('X');
  122.    exitItem.addActionListener(
  123.           new ActionListener(){
  124.                   public void actionPerformed(ActionEvent e)
  125.                   {
  126.                    System.exit(0); //如果被触发,则退出画图板程序
  127.                   }
  128.           }
  129.    );
  130.    fileMenu.add(exitItem);
  131.    bar.add(fileMenu);
  132. //设置颜色菜单条
  133.    JMenu colorMenu=new JMenu("Color");
  134.    colorMenu.setMnemonic('C');
  135. //选择颜色菜单项
  136.    JMenuItem colorItem=new JMenuItem("Choose Color");
  137.    colorItem.setMnemonic('O');
  138.    colorItem.addActionListener(
  139.            new ActionListener(){
  140.                    public void actionPerformed(ActionEvent e)
  141.                    {
  142.                     chooseColor(); //如果被触发,则调用选择颜色函数段
  143.                    }
  144.        }
  145.       );
  146.    colorMenu.add(colorItem);
  147.    bar.add(colorMenu);
  148. //设置线条粗细菜单条
  149.     JMenu strokeMenu=new JMenu("Stroke");
  150.     strokeMenu.setMnemonic('S');
  151. //设置线条粗细菜单项
  152.     JMenuItem strokeItem=new JMenuItem("Set Stroke");
  153.     strokeItem.setMnemonic('K');
  154.     strokeItem.addActionListener(
  155.            new ActionListener(){
  156.                    public void actionPerformed(ActionEvent e)
  157.                     {
  158.                      setStroke();
  159.                      }
  160.                    }
  161.               );
  162.            strokeMenu.add(strokeItem);
  163.            bar.add(strokeMenu);
  164. //设置提示菜单条
  165.     JMenu helpMenu=new JMenu("Help");
  166.     helpMenu.setMnemonic('H');
  167. //设置提示菜单项
  168.     JMenuItem aboutItem=new JMenuItem("About this Drawing Pad!");
  169.     aboutItem.setMnemonic('A');
  170.     aboutItem.addActionListener(
  171.            new ActionListener(){
  172.                    public void actionPerformed(ActionEvent e)
  173.                     {
  174.                      JOptionPane.showMessageDialog(null,
  175.                         "This is a mini drawing pad!nCopyright (c) 2002 Tsinghua University ",
  176.                         " 画图板程序说明 ",
  177.                          JOptionPane.INFORMATION_MESSAGE );
  178.                      }
  179.                    }
  180.               );
  181.     helpMenu.add(aboutItem);
  182.     bar.add(helpMenu);
  183.     items=new ImageIcon[names.length];
  184. //创建各种基本图形的按钮
  185.     drawingArea=new DrawPanel();
  186.     choices=new JButton[names.length];
  187.     buttonPanel = new JToolBar( JToolBar.VERTICAL ) ;
  188.     buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ;
  189.     ButtonHandler handler=new ButtonHandler();
  190.     ButtonHandler1 handler1=new ButtonHandler1();
  191. //导入我们需要的图形图标,这些图标都存放在与源文件相同的目录下面
  192.     for(int i=0;i<choices.length;i++)
  193.     {//items[i]=new ImageIcon( MiniDrawPad.class.getResource(names[i] +".gif"));
  194.                        //如果在jbuilder下运行本程序,则应该用这条语句导入图片
  195.      items[i]=new ImageIcon(names[i] + ".gif");
  196.                    //默认的在jdk或者jcreator下运行,用此语句导入图片
  197.      choices[i]=new JButton("",items[i]);
  198.      choices[i].setToolTipText(tipText[i]);
  199.      buttonPanel.add(choices[i]);
  200.     }
  201. //将动作侦听器加入按钮里面
  202.     for(int i=3;i<choices.length-3;i++)
  203.     {
  204.      choices[i].addActionListener(handler);
  205.     }
  206.     choices[0].addActionListener(
  207.           new ActionListener(){
  208.                   public void actionPerformed(ActionEvent e)
  209.                   {
  210.                    newFile();
  211.                   }
  212.           }
  213.      );
  214.     choices[1].addActionListener(
  215.           new ActionListener(){
  216.                   public void actionPerformed(ActionEvent e)
  217.                   {
  218.                    loadFile();
  219.                   }
  220.           }
  221.      );
  222.     choices[2].addActionListener(
  223.           new ActionListener(){
  224.                   public void actionPerformed(ActionEvent e)
  225.                   {
  226.                    saveFile();
  227.                   }
  228.           }
  229.      );
  230.     choices[choices.length-3].addActionListener(handler1);
  231.     choices[choices.length-2].addActionListener(handler1);
  232.     choices[choices.length-1].addActionListener(handler1);
  233. //字体风格选择
  234.     styles=new JComboBox(styleNames);
  235.     styles.setMaximumRowCount(8);
  236.     styles.addItemListener(
  237.             new ItemListener(){
  238.                     public void itemStateChanged(ItemEvent e)
  239.                     {
  240.                       style1=styleNames[styles.getSelectedIndex()];
  241.                     }
  242.                }
  243.             );
  244. //字体选择
  245.     bold=new JCheckBox("BOLD");
  246.     italic=new JCheckBox("ITALIC");
  247.     checkBoxHandler cHandler=new checkBoxHandler();
  248.     bold.addItemListener(cHandler);
  249.     italic.addItemListener(cHandler);
  250.     JPanel wordPanel=new JPanel();
  251.     buttonPanel.add(bold);
  252.     buttonPanel.add(italic);
  253.     buttonPanel.add(styles);
  254.     styles.setMinimumSize(  new Dimension ( 50, 20 ) );
  255.     styles.setMaximumSize(new Dimension ( 100, 20 ) );
  256.     Container c=getContentPane();
  257.     super.setJMenuBar( bar );
  258.     c.add(buttonPanel,BorderLayout.NORTH);
  259.     c.add(drawingArea,BorderLayout.CENTER);
  260.     statusBar=new JLabel();
  261.     c.add(statusBar,BorderLayout.SOUTH);
  262.     statusBar.setText("     Welcome To The Little Drawing Pad!!!  :)");
  263.     createNewItem();
  264.     setSize(width,height);
  265.     show();
  266.   }
  267. //按钮侦听器ButtonHanler类,内部类,用来侦听基本按钮的操作
  268. public class ButtonHandler implements ActionListener
  269.  {
  270.   public void actionPerformed(ActionEvent e)
  271.   {
  272.    for(int j=3;j<choices.length-3;j++)
  273.    {
  274.       if(e.getSource()==choices[j])
  275.          {currentChoice=j;
  276.           createNewItem();
  277.           repaint();}
  278.         }
  279.     }
  280.  }
  281. //按钮侦听器ButtonHanler1类,用来侦听颜色选择、画笔粗细设置、文字输入按钮的操作
  282. public class ButtonHandler1 implements ActionListener
  283.  {
  284.   public void actionPerformed(ActionEvent e)
  285.   {
  286.     if(e.getSource()==choices[choices.length-3])
  287.          {chooseColor();}
  288.     if(e.getSource()==choices[choices.length-2])
  289.          {setStroke();}
  290.     if(e.getSource()==choices[choices.length-1])
  291.          {JOptionPane.showMessageDialog(null,
  292.              "Please hit the drawing pad to choose the word input position",
  293.              "Hint",JOptionPane.INFORMATION_MESSAGE );
  294.           currentChoice=14;
  295.           createNewItem();
  296.           repaint();
  297.           }
  298.     }
  299.  }
  300. //鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作
  301.  class mouseA extends MouseAdapter
  302.  {
  303.    public void mousePressed(MouseEvent e)
  304.     {statusBar.setText("     Mouse Pressed @:[" + e.getX() +
  305.                               ", " + e.getY() + "]");//设置状态提示
  306.      itemList[index].x1=itemList[index].x2=e.getX();
  307.      itemList[index].y1=itemList[index].y2=e.getY();
  308.     //如果当前选择的图形是随笔画或者橡皮擦,则进行下面的操作
  309.     if(currentChoice==3||currentChoice==13)
  310.     {
  311.      itemList[index].x1=itemList[index].x2=e.getX();
  312.      itemList[index].y1=itemList[index].y2=e.getY();
  313.      index++;
  314.      createNewItem();
  315.      }
  316.     //如果当前选择的图形式文字输入,则进行下面操作
  317.      if(currentChoice==14)
  318.      {
  319.       itemList[index].x1=e.getX();
  320.       itemList[index].y1=e.getY();
  321.       String input;
  322.       input=JOptionPane.showInputDialog(
  323.           "Please input the text you want!");
  324.       itemList[index].s1=input;
  325.       itemList[index].x2=f1;
  326.       itemList[index].y2=f2;
  327.       itemList[index].s2=style1;
  328.       index++;
  329.       currentChoice=14;
  330.       createNewItem();
  331.       drawingArea.repaint();
  332.       }
  333.     }
  334.    public void mouseReleased(MouseEvent e)
  335.     {statusBar.setText("     Mouse Released @:[" + e.getX() +
  336.                               ", " + e.getY() + "]");
  337.     if(currentChoice==3||currentChoice==13)
  338.     {
  339.      itemList[index].x1=e.getX();
  340.      itemList[index].y1=e.getY();
  341.     }
  342.      itemList[index].x2=e.getX();
  343.      itemList[index].y2=e.getY();
  344.      repaint();
  345.      index++;
  346.      createNewItem();
  347.     }
  348.    public void mouseEntered(MouseEvent e)
  349.    {
  350.            statusBar.setText("     Mouse Entered @:[" + e.getX() +
  351.                               ", " + e.getY() + "]");
  352.            }
  353.    public void mouseExited(MouseEvent e)
  354.    {
  355.            statusBar.setText("     Mouse Exited @:[" + e.getX() +
  356.                               ", " + e.getY() + "]");
  357.            }
  358.   }
  359. //鼠标事件mouseB类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的相应操作
  360.  class mouseB extends MouseMotionAdapter
  361.  {
  362.   public void mouseDragged(MouseEvent e)
  363.   {statusBar.setText("     Mouse Dragged @:[" + e.getX() +
  364.                               ", " + e.getY() + "]");
  365.    if(currentChoice==3||currentChoice==13)
  366.    {
  367.     itemList[index-1].x1=itemList[index].x2=itemList[index].x1=e.getX();
  368.     itemList[index-1].y1=itemList[index].y2=itemList[index].y1=e.getY();
  369.     index++;
  370.     createNewItem();
  371.    }
  372.    else
  373.     {
  374.      itemList[index].x2=e.getX();
  375.      itemList[index].y2=e.getY();
  376.     }
  377.    repaint();
  378.    }
  379.   public void mouseMoved(MouseEvent e)
  380.    {statusBar.setText("     Mouse Moved @:[" + e.getX() +
  381.                               ", " + e.getY() + "]");}
  382.   }
  383. //选择字体风格时候用到的事件侦听器类,加入到字体风格的选择框中
  384. private class checkBoxHandler implements ItemListener
  385.  {
  386.   public void itemStateChanged(ItemEvent e)
  387.   {
  388.    if(e.getSource()==bold)
  389.      if(e.getStateChange()==ItemEvent.SELECTED)
  390.         f1=Font.BOLD;
  391.       else
  392.         f1=Font.PLAIN;
  393.    if(e.getSource()==italic)
  394.      if(e.getStateChange()==ItemEvent.SELECTED)
  395.         f2=Font.ITALIC;
  396.       else
  397.         f2=Font.PLAIN;
  398.           }
  399.  }
  400. //画图面板类,用来画图
  401.  class DrawPanel extends JPanel
  402.  {
  403.    public DrawPanel()
  404.   {
  405.    setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  406.    setBackground(Color.white);
  407.    addMouseListener(new mouseA());
  408.    addMouseMotionListener(new mouseB());
  409.   }
  410.     public void paintComponent(Graphics g)
  411.     {
  412.       super.paintComponent(g);
  413.       Graphics2D g2d=(Graphics2D)g; //定义画笔
  414.       int j=0;
  415.       while (j<=index)
  416.       {
  417.         draw(g2d,itemList[j]);
  418.         j++;
  419.       }
  420.     }
  421.     void draw(Graphics2D g2d,drawings i)
  422.     {
  423.       i.draw(g2d);//将画笔传入到各个子类中,用来完成各自的绘图
  424.     }
  425.  }
  426. //新建一个画图基本单元对象的程序段
  427.  void createNewItem()
  428.   { if(currentChoice==14)//进行相应的游标设置
  429.           drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
  430.           else
  431.           drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  432.     switch (currentChoice)
  433.     {
  434.       case 3:
  435.         itemList[index]=new Pencil();
  436.         break;
  437.       case 4:
  438.         itemList[index]=new Line();
  439.         break;
  440.       case 5:
  441.         itemList[index]=new Rect();
  442.         break;
  443.       case 6:
  444.         itemList[index]=new fillRect();
  445.         break;
  446.       case 7:
  447.         itemList[index]=new Oval();
  448.         break;
  449.       case 8:
  450.         itemList[index]=new fillOval();
  451.         break;
  452.       case 9:
  453.         itemList[index]=new Circle();
  454.         break;
  455.       case 10:
  456.         itemList[index]=new fillCircle();
  457.         break;
  458.       case 11:
  459.         itemList[index]=new RoundRect();
  460.         break;
  461.       case 12:
  462.         itemList[index]=new fillRoundRect();
  463.         break;
  464.       case 13:
  465.         itemList[index]=new Rubber();
  466.         break;
  467.       case 14:
  468.         itemList[index]=new Word();
  469.         break;
  470.     }
  471.     itemList[index].type=currentChoice;
  472.     itemList[index].R=R;
  473.     itemList[index].G=G;
  474.     itemList[index].B=B;
  475.     itemList[index].stroke=stroke;
  476.   }
  477. //选择当前颜色程序段
  478. public void chooseColor()
  479.  {
  480.     color=JColorChooser.showDialog(MiniDrawPad.this,
  481.                           "Choose a color",color);
  482.     R=color.getRed();
  483.     G=color.getGreen();
  484.     B=color.getBlue();
  485.     itemList[index].R=R;
  486.     itemList[index].G=G;
  487.     itemList[index].B=B;
  488.   }
  489. //选择当前线条粗细程序段
  490. public void setStroke()
  491.  {
  492.   String input;
  493.   input=JOptionPane.showInputDialog(
  494.           "Please input a float stroke value! ( >0 )");
  495.   stroke=Float.parseFloat(input);
  496.   itemList[index].stroke=stroke;
  497.   }
  498. //保存图形文件程序段
  499.  public void saveFile()
  500.  {
  501.     JFileChooser fileChooser=new JFileChooser();
  502.     fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  503.     int result =fileChooser.showSaveDialog(this);
  504.     if(result==JFileChooser.CANCEL_OPTION)
  505.              return ;
  506.     File fileName=fileChooser.getSelectedFile();
  507.     fileName.canWrite();
  508.     if (fileName==null||fileName.getName().equals(""))
  509.     JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
  510.             "Invalid File Name", JOptionPane.ERROR_MESSAGE);
  511.     else{
  512.       try {
  513.        fileName.delete();
  514.        FileOutputStream fos=new FileOutputStream(fileName);
  515.        output=new ObjectOutputStream(fos);
  516.        drawings record;
  517.        output.writeInt( index );
  518.        for(int i=0;i< index ;i++)
  519.        {
  520.                drawings p= itemList[ i ] ;
  521.         output.writeObject(p);
  522.         output.flush();    //将所有图形信息强制转换成父类线性化存储到文件中
  523.                }
  524.       output.close();
  525.       fos.close();
  526.       }
  527.        catch(IOException ioe)
  528.        {
  529.          ioe.printStackTrace();
  530.        }
  531.       }
  532.    }
  533. //打开一个图形文件程序段
  534.  public void loadFile()
  535.  {
  536.     JFileChooser fileChooser=new JFileChooser();
  537.     fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  538.     int result =fileChooser.showOpenDialog(this);
  539.     if(result==JFileChooser.CANCEL_OPTION)
  540.           return ;
  541.     File fileName=fileChooser.getSelectedFile();
  542.     fileName.canRead();
  543.     if (fileName==null||fileName.getName().equals(""))
  544.        JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
  545.             "Invalid File Name", JOptionPane.ERROR_MESSAGE);
  546.     else {
  547.       try {
  548.           FileInputStream fis=new FileInputStream(fileName);
  549.           input=new ObjectInputStream(fis);
  550.           drawings inputRecord;
  551.           int countNumber=0;
  552.           countNumber=input.readInt();
  553.           for(index=0;index< countNumber ;index++)
  554.           {
  555.             inputRecord=(drawings)input.readObject();
  556.             itemList[ index ] = inputRecord ;
  557.           }
  558.           createNewItem();
  559.           input.close();
  560.           repaint();
  561.           }
  562.            catch(EOFException endofFileException){
  563.             JOptionPane.showMessageDialog(this,"no more record in file",
  564.                            "class not found",JOptionPane.ERROR_MESSAGE );
  565.           }
  566.           catch(ClassNotFoundException classNotFoundException){
  567.             JOptionPane.showMessageDialog(this,"Unable to Create Object",
  568.                            "end of file",JOptionPane.ERROR_MESSAGE );
  569.           }
  570.           catch (IOException ioException){
  571.             JOptionPane.showMessageDialog(this,"error during read from file",
  572.                            "read Error",JOptionPane.ERROR_MESSAGE );
  573.             }
  574.           }
  575.        }
  576. //新建一个文件程序段
  577.  public void newFile()
  578.  {
  579.   index=0;
  580.   currentChoice=3;
  581.   color=Color.black;
  582.   stroke=1.0f;
  583.   createNewItem();
  584.   repaint();//将有关值设置为初始状态,并且重画
  585.  }
  586. //主函数段
  587.  public static void main(String args[])
  588.   {try {
  589.         UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
  590.         }
  591.      catch ( Exception e ) {}//将界面设置为当前windows风格
  592.    MiniDrawPad newPad=new MiniDrawPad();
  593.    newPad.addWindowListener(
  594.         new WindowAdapter(){
  595.            public void windowClosing(WindowEvent e)
  596.            {System.exit(0);}});
  597.   }
  598. }
  599. //定义画图的基本图形单元
  600. class drawings implements Serializable//父类,基本图形单元,用到串行化接口,保存时所用
  601.  {
  602.   int x1,y1,x2,y2; //定义坐标属性
  603.   int R,G,B; //定义色彩属性
  604.   float stroke; //定义线条粗细属性
  605.   int type; //定义字体属性
  606.   String s1;
  607.   String s2; //定义字体风格属性
  608.   void draw(Graphics2D g2d){};//定义绘图函数
  609.  }
  610. /*******************************************************************************
  611.   下面是各种基本图形单元的子类,都继承自父类drawings,请仔细理解继承的概念
  612. ********************************************************************************/
  613.  class Line extends drawings //直线类
  614.  {
  615.  void draw(Graphics2D g2d)
  616.   {g2d.setPaint(new Color(R,G,B));
  617.    g2d.setStroke(new BasicStroke(stroke,
  618.                 BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));
  619.    g2d.drawLine(x1,y1,x2,y2);
  620.    }
  621.  }
  622.  class Rect extends drawings//矩形类
  623.  {
  624.  void draw(Graphics2D g2d)
  625.   {g2d.setPaint(new Color(R,G,B));
  626.    g2d.setStroke(new BasicStroke(stroke));
  627.    g2d.drawRect(Math.min(x1,x2),Math.min(y1,y2),
  628.               Math.abs(x1-x2),Math.abs(y1-y2));
  629.   }
  630.  }
  631.  class fillRect extends drawings//实心矩形类
  632.  {
  633.  void draw(Graphics2D g2d)
  634.   {g2d.setPaint(new Color(R,G,B));
  635.    g2d.setStroke(new BasicStroke(stroke));
  636.    g2d.fillRect(Math.min(x1,x2),Math.min(y1,y2),
  637.               Math.abs(x1-x2),Math.abs(y1-y2));
  638.   }
  639.  }
  640.  class Oval extends drawings//椭圆类
  641.   {
  642.     void draw(Graphics2D g2d)
  643.     {g2d.setPaint(new Color(R,G,B));
  644.      g2d.setStroke(new BasicStroke(stroke));
  645.      g2d.drawOval(Math.min(x1,x2),Math.min(y1,y2),
  646.                   Math.abs(x1-x2),Math.abs(y1-y2));
  647.     }
  648.   }
  649.  class fillOval extends drawings//实心椭圆
  650.  {
  651.   void draw(Graphics2D g2d)
  652.   {g2d.setPaint(new Color(R,G,B));
  653.    g2d.setStroke(new BasicStroke(stroke));
  654.    g2d.fillOval(Math.min(x1,x2),Math.min(y1,y2),
  655.                 Math.abs(x1-x2),Math.abs(y1-y2));
  656.          }
  657.  }
  658.  class Circle extends drawings//圆类
  659.  {
  660.    void draw(Graphics2D g2d)
  661.    {g2d.setPaint(new Color(R,G,B));
  662.     g2d.setStroke(new BasicStroke(stroke));
  663.     g2d.drawOval(Math.min(x1,x2),Math.min(y1,y2),
  664.                Math.max(Math.abs(x1-x2),Math.abs(y1-y2)),
  665.                Math.max(Math.abs(x1-x2),Math.abs(y1-y2))
  666.                );
  667.     }
  668.  }
  669.  class fillCircle extends drawings//实心圆
  670.  {
  671.   void draw(Graphics2D g2d)
  672.   {g2d.setPaint(new Color(R,G,B));
  673.    g2d.setStroke(new BasicStroke(stroke));
  674.    g2d.fillOval(Math.min(x1,x2),Math.min(y1,y2),
  675.                Math.max(Math.abs(x1-x2),Math.abs(y1-y2)),
  676.                Math.max(Math.abs(x1-x2),Math.abs(y1-y2))
  677.                );
  678.          }
  679.  }
  680.  class RoundRect extends drawings//圆角矩形类
  681.  {
  682.   void draw(Graphics2D g2d)
  683.   {g2d.setPaint(new Color(R,G,B));
  684.    g2d.setStroke(new BasicStroke(stroke));
  685.    g2d.drawRoundRect(Math.min(x1,x2),Math.min(y1,y2),
  686.                    Math.abs(x1-x2),Math.abs(y1-y2),
  687.                    50,35);
  688.          }
  689.  }
  690.  class fillRoundRect extends drawings//实心圆角矩形类
  691.  {
  692.   void draw(Graphics2D g2d)
  693.   {g2d.setPaint(new Color(R,G,B));
  694.    g2d.setStroke(new BasicStroke(stroke));
  695.    g2d.fillRoundRect(Math.min(x1,x2),Math.min(y1,y2),
  696.                    Math.abs(x1-x2),Math.abs(y1-y2),
  697.                    50,35);
  698.          }
  699.  }
  700.  class Pencil extends drawings//随笔画类
  701.  {
  702.   void draw(Graphics2D g2d)
  703.   {g2d.setPaint(new Color(R,G,B));
  704.    g2d.setStroke(new BasicStroke(stroke,
  705.                 BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));
  706.    g2d.drawLine(x1,y1,x2,y2);
  707.          }
  708.  }
  709.  class Rubber extends drawings//橡皮擦类
  710.  {
  711.   void draw(Graphics2D g2d)
  712.   {g2d.setPaint(new Color(255,255,255));
  713.    g2d.setStroke(new BasicStroke(stroke+4,
  714.                 BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));
  715.    g2d.drawLine(x1,y1,x2,y2);
  716.          }
  717.  }
  718.  class Word extends drawings//输入文字类
  719.  {
  720.   void draw(Graphics2D g2d)
  721.   {
  722.           g2d.setPaint(new Color(R,G,B));
  723.           g2d.setFont(new Font(s2,x2+y2,((int)stroke)*18));
  724.           if (s1!= null )
  725.           g2d.drawString(s1,x1,y1);
  726.          }
  727.  }