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

📄 b0f5b0916aa6001c12d3a6ace67d4745

📁 用JAVA语言编制一个画图板
💻
📖 第 1 页 / 共 2 页
字号:
   public void mouseExited(MouseEvent e)   {statusBar.setText("     鼠标离开 @:[" + e.getX() +                              ", " + e.getY() + "]"); } }//鼠标事件mouseB类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的相应操作 class mouseB extends MouseMotionAdapter {  public void mouseDragged(MouseEvent e)  {statusBar.setText("     拖拉鼠标 @:[" + e.getX() +                              ", " + e.getY() + "]");   if(currentChoice==3||currentChoice==13)   {    itemList[index-1].x1=itemList[index].x2=itemList[index].x1=e.getX();    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("    移动鼠标 @:[" + 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, "选择颜色",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("请输入线条粗细值! ( >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,"输入文件名字",       "输入文件名字", 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,"输入文件名字",            "输入文件名字", 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,"在文件中没有更多记录",                           "类不能发现",JOptionPane.ERROR_MESSAGE );          }          catch(ClassNotFoundException classNotFoundException){            JOptionPane.showMessageDialog(this,"不能创建新物体",                           "文件结束",JOptionPane.ERROR_MESSAGE );          }          catch (IOException ioException){            JOptionPane.showMessageDialog(this,"在读取文件过程出现错误",                           "读取错误",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 + -