手机版
你好,游客 登录 注册
背景:
阅读新闻

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

[日期:2011-11-05] 来源:Linux社区  作者:afgasdg [字体:大 中 小]
  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. }  
linux
相关资讯       Java教程  Java培训 
本文评论   查看全部评论 (2)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 2 楼
* 222 发表于 2012/3/4 17:46:15
加载图片的时候错误:java.io.StreamCorruptedException: invalid stream header: FFD8FFE0
求解答
第 1 楼
* 22 发表于 2012/3/4 9:41:36
windows系统用不了啊