⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 minidrawpad.java~1~

📁 这是一个实现画图的多功能的小软件,欢迎多多使用,很简单易用的.
💻 JAVA~1~
📖 第 1 页 / 共 2 页
字号:
    itemList[index-1].y1=itemList[index].y2=itemList[index].y1=e.getY();    index++;    createNewItem();   }   else    {     itemList[index].x2=e.getX();     itemList[index].y2=e.getY();    }   repaint();   }  public void mouseMoved(MouseEvent e)   {statusBar.setText("     Mouse Moved @:[" + e.getX() +                              ", " + e.getY() + "]");}  }//选择字体风格时候用到的事件侦听器类,加入到字体风格的选择框中private class checkBoxHandler implements ItemListener {  public void itemStateChanged(ItemEvent e)  {   if(e.getSource()==bold)     if(e.getStateChange()==ItemEvent.SELECTED)        f1=Font.BOLD;      else        f1=Font.PLAIN;   if(e.getSource()==italic)     if(e.getStateChange()==ItemEvent.SELECTED)        f2=Font.ITALIC;      else        f2=Font.PLAIN;          } }//画图面板类,用来画图 class DrawPanel extends JPanel {   public DrawPanel()  {   setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));   setBackground(Color.white);   addMouseListener(new mouseA());   addMouseMotionListener(new mouseB());  }    public void paintComponent(Graphics g)    {      super.paintComponent(g);      Graphics2D g2d=(Graphics2D)g;	//定义画笔      int j=0;      while (j<=index)      {        draw(g2d,itemList[j]);        j++;      }    }    void draw(Graphics2D g2d,drawings i)    {      i.draw(g2d);//将画笔传入到各个子类中,用来完成各自的绘图    } }//新建一个画图基本单元对象的程序段 void createNewItem()  { if(currentChoice==14)//进行相应的游标设置          drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));          else          drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));    switch (currentChoice)    {      case 3:        itemList[index]=new Pencil();        break;      case 4:        itemList[index]=new Line();        break;      case 5:        itemList[index]=new Rect();        break;      case 6:        itemList[index]=new fillRect();        break;      case 7:        itemList[index]=new Oval();        break;      case 8:        itemList[index]=new fillOval();        break;      case 9:        itemList[index]=new Circle();        break;      case 10:        itemList[index]=new fillCircle();        break;      case 11:        itemList[index]=new RoundRect();        break;      case 12:        itemList[index]=new fillRoundRect();        break;      case 13:        itemList[index]=new Rubber();        break;      case 14:        itemList[index]=new Word();        break;    }    itemList[index].type=currentChoice;    itemList[index].R=R;    itemList[index].G=G;    itemList[index].B=B;    itemList[index].stroke=stroke;  }//选择当前颜色程序段public void chooseColor() {    color=JColorChooser.showDialog(MiniDrawPad.this,                          "Choose a color",color);    R=color.getRed();    G=color.getGreen();    B=color.getBlue();    itemList[index].R=R;    itemList[index].G=G;    itemList[index].B=B;  }//选择当前线条粗细程序段public void setStroke() {  String input;  input=JOptionPane.showInputDialog(          "Please input a float stroke value! ( >0 )");  stroke=Float.parseFloat(input);  itemList[index].stroke=stroke;  }//保存图形文件程序段 public void saveFile() {    JFileChooser fileChooser=new JFileChooser();    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);    int result =fileChooser.showSaveDialog(this);    if(result==JFileChooser.CANCEL_OPTION)             return ;    File fileName=fileChooser.getSelectedFile();    fileName.canWrite();    if (fileName==null||fileName.getName().equals(""))    JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",            "Invalid File Name", JOptionPane.ERROR_MESSAGE);    else{      try {       fileName.delete();       FileOutputStream fos=new FileOutputStream(fileName);       output=new ObjectOutputStream(fos);       drawings record;       output.writeInt( index );       for(int i=0;i< index ;i++)       {               drawings p= itemList[ i ] ;        output.writeObject(p);        output.flush();    //将所有图形信息强制转换成父类线性化存储到文件中               }      output.close();      fos.close();      }       catch(IOException ioe)       {         ioe.printStackTrace();       }      }   }//打开一个图形文件程序段 public void loadFile() {    JFileChooser fileChooser=new JFileChooser();    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);    int result =fileChooser.showOpenDialog(this);    if(result==JFileChooser.CANCEL_OPTION)          return ;    File fileName=fileChooser.getSelectedFile();    fileName.canRead();    if (fileName==null||fileName.getName().equals(""))       JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",            "Invalid File Name", JOptionPane.ERROR_MESSAGE);    else {      try {          FileInputStream fis=new FileInputStream(fileName);          input=new ObjectInputStream(fis);          drawings inputRecord;          int countNumber=0;          countNumber=input.readInt();          for(index=0;index< countNumber ;index++)          {            inputRecord=(drawings)input.readObject();            itemList[ index ] = inputRecord ;          }          createNewItem();          input.close();          repaint();          }           catch(EOFException endofFileException){            JOptionPane.showMessageDialog(this,"no more record in file",                           "class not found",JOptionPane.ERROR_MESSAGE );          }          catch(ClassNotFoundException classNotFoundException){            JOptionPane.showMessageDialog(this,"Unable to Create Object",                           "end of file",JOptionPane.ERROR_MESSAGE );          }          catch (IOException ioException){            JOptionPane.showMessageDialog(this,"error during read from file",                           "read Error",JOptionPane.ERROR_MESSAGE );            }          }       }//新建一个文件程序段 public void newFile() {  index=0;  currentChoice=3;  color=Color.black;  stroke=1.0f;  createNewItem();  repaint();//将有关值设置为初始状态,并且重画 }//主函数段 public static void main(String args[])  {try {        UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());        }     catch ( Exception e ) {}//将界面设置为当前windows风格   MiniDrawPad newPad=new MiniDrawPad();   newPad.addWindowListener(        new WindowAdapter(){           public void windowClosing(WindowEvent e)           {System.exit(0);}});  }}//定义画图的基本图形单元class drawings implements Serializable//父类,基本图形单元,用到串行化接口,保存时所用 {  int x1,y1,x2,y2;	//定义坐标属性  int R,G,B;		//定义色彩属性  float stroke;		//定义线条粗细属性  int type;		//定义字体属性  String s1;  String s2;		//定义字体风格属性  void draw(Graphics2D g2d){};//定义绘图函数 }/*******************************************************************************  下面是各种基本图形单元的子类,都继承自父类drawings,请仔细理解继承的概念********************************************************************************/ class Line extends drawings //直线类 { void draw(Graphics2D g2d)  {g2d.setPaint(new Color(R,G,B));   g2d.setStroke(new BasicStroke(stroke,                BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));   g2d.drawLine(x1,y1,x2,y2);   } } class Rect extends drawings//矩形类 { void draw(Graphics2D g2d)  {g2d.setPaint(new Color(R,G,B));   g2d.setStroke(new BasicStroke(stroke));   g2d.drawRect(Math.min(x1,x2),Math.min(y1,y2),              Math.abs(x1-x2),Math.abs(y1-y2));  } } class fillRect extends drawings//实心矩形类 { void draw(Graphics2D g2d)  {g2d.setPaint(new Color(R,G,B));   g2d.setStroke(new BasicStroke(stroke));   g2d.fillRect(Math.min(x1,x2),Math.min(y1,y2),              Math.abs(x1-x2),Math.abs(y1-y2));  } } class Oval extends drawings//椭圆类  {    void draw(Graphics2D g2d)    {g2d.setPaint(new Color(R,G,B));     g2d.setStroke(new BasicStroke(stroke));     g2d.drawOval(Math.min(x1,x2),Math.min(y1,y2),                  Math.abs(x1-x2),Math.abs(y1-y2));    }  } class fillOval extends drawings//实心椭圆 {  void draw(Graphics2D g2d)  {g2d.setPaint(new Color(R,G,B));   g2d.setStroke(new BasicStroke(stroke));   g2d.fillOval(Math.min(x1,x2),Math.min(y1,y2),                Math.abs(x1-x2),Math.abs(y1-y2));         } } class Circle extends drawings//圆类 {   void draw(Graphics2D g2d)   {g2d.setPaint(new Color(R,G,B));    g2d.setStroke(new BasicStroke(stroke));    g2d.drawOval(Math.min(x1,x2),Math.min(y1,y2),               Math.max(Math.abs(x1-x2),Math.abs(y1-y2)),               Math.max(Math.abs(x1-x2),Math.abs(y1-y2))               );    } } class fillCircle extends drawings//实心圆 {  void draw(Graphics2D g2d)  {g2d.setPaint(new Color(R,G,B));   g2d.setStroke(new BasicStroke(stroke));   g2d.fillOval(Math.min(x1,x2),Math.min(y1,y2),               Math.max(Math.abs(x1-x2),Math.abs(y1-y2)),               Math.max(Math.abs(x1-x2),Math.abs(y1-y2))               );         } } class RoundRect extends drawings//圆角矩形类 {  void draw(Graphics2D g2d)  {g2d.setPaint(new Color(R,G,B));   g2d.setStroke(new BasicStroke(stroke));   g2d.drawRoundRect(Math.min(x1,x2),Math.min(y1,y2),                   Math.abs(x1-x2),Math.abs(y1-y2),                   50,35);         } } class fillRoundRect extends drawings//实心圆角矩形类 {  void draw(Graphics2D g2d)  {g2d.setPaint(new Color(R,G,B));   g2d.setStroke(new BasicStroke(stroke));   g2d.fillRoundRect(Math.min(x1,x2),Math.min(y1,y2),                   Math.abs(x1-x2),Math.abs(y1-y2),                   50,35);         } } class Pencil extends drawings//随笔画类 {  void draw(Graphics2D g2d)  {g2d.setPaint(new Color(R,G,B));   g2d.setStroke(new BasicStroke(stroke,                BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));   g2d.drawLine(x1,y1,x2,y2);         } } class Rubber extends drawings//橡皮擦类 {  void draw(Graphics2D g2d)  {g2d.setPaint(new Color(255,255,255));   g2d.setStroke(new BasicStroke(stroke+4,                BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));   g2d.drawLine(x1,y1,x2,y2);         } } class Word extends drawings//输入文字类 {  void draw(Graphics2D g2d)  {          g2d.setPaint(new Color(R,G,B));          g2d.setFont(new Font(s2,x2+y2,((int)stroke)*18));          if (s1!= null )          g2d.drawString(s1,x1,y1);         } }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -