用j2se写的简单的画图软件Draw0.6

 

  
  
  
  
  1. import java.awt.*; 
  2. import java.awt.event.ActionEvent; 
  3. import java.awt.event.ActionListener; 
  4. import java.awt.event.MouseAdapter; 
  5. import java.awt.event.MouseEvent; 
  6. import java.awt.event.WindowAdapter; 
  7. import java.awt.event.WindowEvent; 
  8.  
  9. import javax.swing.*; 
  10.  
  11. public class Painter extends JFrame implements ActionListener { 
  12.  
  13.     private static final long serialVersionUID = 1L; 
  14.     private static final int X = 100
  15.     private static final int Y = 100
  16.     private static final int WIDTH = 600
  17.     private static final int HEIGHT = 450
  18.     private int  judge = 0
  19.     private Drawing itemList[] = new Drawing[5000]; 
  20.     private int index = 0
  21.     private Paint drawingArea = new Paint(); 
  22.     private JToolBar buttonPanel; 
  23.     private JButton choices[]; 
  24.     private JLabel mouseAction ; 
  25.     private int R,G,B; 
  26.     private Color color = Color.black; 
  27.     private float stroke = 1.0f; 
  28.     private static String names[] = { 
  29.         "铅笔","橡皮擦","清除","直线","空心矩形","实心矩形"
  30.         "空心圆","实心圆","空心椭圆","实心椭圆","调色盒","线条粗细" 
  31.     }; 
  32.  
  33.     public Painter() { 
  34.         createNewItem(); 
  35.         this.setTitle("画图"); 
  36.         this.setBounds(X, Y, WIDTH, HEIGHT); 
  37.         choices = new JButton[names.length]; 
  38.         buttonPanel = new JToolBar(JToolBar.HORIZONTAL); 
  39.         buttonPanel.setFloatable(false); 
  40.         for(int i = 0;i<names.length;i++) { 
  41.             choices[i] = new JButton(names[i]); 
  42.             buttonPanel.add(choices[i]); 
  43.             choices[i].addActionListener(this); 
  44.         } 
  45.         this.setLayout(new BorderLayout()); 
  46.         mouseAction = new JLabel(); 
  47.         add(buttonPanel,BorderLayout.NORTH); 
  48.         add(drawingArea,BorderLayout.CENTER); 
  49.         add(mouseAction,BorderLayout.SOUTH); 
  50.         setVisible(true); 
  51.         this.addWindowListener(new WindowAdapter() { 
  52.  
  53.             public void windowClosing(WindowEvent e) { 
  54.                 System.exit(0); 
  55.             } 
  56.  
  57.         }); 
  58.     } 
  59.  
  60.     public static void main(String[] args) { 
  61.         try { 
  62.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
  63.         } catch (Exception e) { 
  64.         }// 将界面设置为当前windows风格 
  65.         new Painter(); 
  66.  
  67.     } 
  68.  
  69.     @Override 
  70.     public void actionPerformed(ActionEvent e) { 
  71.          
  72.         if ((e.getActionCommand()).equals("铅笔")) { 
  73.             judge = 0
  74.             createNewItem(); 
  75.             repaint(); 
  76.         } 
  77.         if ((e.getActionCommand()).equals("橡皮擦")) { 
  78.             judge = 1
  79.             createNewItem(); 
  80.             repaint(); 
  81.         } 
  82.         if ((e.getActionCommand()).equals("清除")) { 
  83.             index = 0
  84.             judge = 0
  85.             createNewItem(); 
  86.             repaint();// 将有关值设置为初始状态,并且重画 
  87.  
  88.         } 
  89.         if ((e.getActionCommand()).equals("直线")) { 
  90.             judge = 2
  91.             createNewItem(); 
  92.             repaint(); 
  93.         } 
  94.         if ((e.getActionCommand()).equals("空心矩形")) { 
  95.             judge = 3
  96.             createNewItem(); 
  97.             repaint(); 
  98.         } 
  99.         if ((e.getActionCommand()).equals("实心矩形")) { 
  100.             judge = 4
  101.             createNewItem(); 
  102.             repaint(); 
  103.         } 
  104.          
  105.         if ((e.getActionCommand()).equals("空心圆")) { 
  106.             judge = 5
  107.             createNewItem(); 
  108.             repaint(); 
  109.         } 
  110.         if ((e.getActionCommand()).equals("实心圆")) { 
  111.             judge = 6
  112.             createNewItem(); 
  113.             repaint(); 
  114.         } 
  115.         if ((e.getActionCommand()).equals("空心椭圆")) { 
  116.             judge = 7
  117.             createNewItem(); 
  118.             repaint(); 
  119.         } 
  120.         if ((e.getActionCommand()).equals("实心椭圆")) { 
  121.             judge = 8
  122.             createNewItem(); 
  123.             repaint(); 
  124.         } 
  125.         if ((e.getActionCommand()).equals("调色盒")) { 
  126.             this.colorChooser(); 
  127.         } 
  128.         if ((e.getActionCommand()).equals("线条粗细")) { 
  129.             this.setStroke(); 
  130.         } 
  131.          
  132.          
  133.  
  134.     } 
  135.      
  136.     public void colorChooser () { 
  137.         try { 
  138.             color = JColorChooser.showDialog(Painter.this"Choose a color"
  139.                     color); 
  140.             R = color.getRed(); 
  141.             G = color.getGreen(); 
  142.             B = color.getBlue(); 
  143.             itemList[index].R = R; 
  144.             itemList[index].G = G; 
  145.             itemList[index].B = B; 
  146.              
  147.         }catch (NullPointerException e) { 
  148.              
  149.         } 
  150.          
  151.     } 
  152.      
  153.     public void setStroke() { 
  154.         try { 
  155.             String input = null
  156.             input = JOptionPane 
  157.             .showInputDialog("Please input a float stroke value! ( >0 )"); 
  158.             stroke = Float.parseFloat(input); 
  159.             itemList[index].stroke = stroke; 
  160.         }catch (NullPointerException e) { 
  161.              
  162.         }catch(NumberFormatException e) { 
  163.             JOptionPane.showMessageDialog(null"输入值为空""提示", JOptionPane.ERROR_MESSAGE); 
  164.         } 
  165.     } 
  166.  
  167.     private void createNewItem() { 
  168.  
  169.         if (judge == 0) { 
  170.             itemList[index] = new Pen(); 
  171.         }  
  172.         else if (judge == 1) { 
  173.             itemList[index] = new Rubber(); 
  174.         } 
  175.         else if (judge == 2) { 
  176.             itemList[index] = new Line(); 
  177.         } 
  178.         else if (judge == 3) { 
  179.             itemList[index] = new Rect(); 
  180.         }  
  181.         else if (judge == 4) { 
  182.             itemList[index] = new fillRect(); 
  183.         }  
  184.         else if (judge == 5) { 
  185.             itemList[index] = new Circle(); 
  186.         }  
  187.         else if (judge == 6) { 
  188.             itemList[index] = new fillCircle(); 
  189.         } 
  190.         else if (judge == 7) { 
  191.             itemList[index] = new Oval(); 
  192.         } 
  193.         else if (judge == 8) { 
  194.             itemList[index] = new fillOval(); 
  195.         } 
  196.         else { 
  197.              
  198.         } 
  199.         itemList[index].R = R; 
  200.         itemList[index].G = G; 
  201.         itemList[index].B = B; 
  202.         itemList[index].stroke = stroke; 
  203.     } 
  204.  
  205.     private class Paint extends JPanel { 
  206.  
  207.         private static final long serialVersionUID = 1L; 
  208.  
  209.         public Paint() { 
  210.             this.setBackground(Color.WHITE); 
  211.             setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); 
  212.             addMouseMotionListener(new MouseAdapter() { 
  213.                  
  214.  
  215.                 @Override 
  216.                 public void mouseMoved(MouseEvent e) { 
  217.                     mouseAction.setText("MouseMoved@ [X:"+e.getX()+"Y:"+e.getY()+"]"); 
  218.                 } 
  219.  
  220.                 public void mouseDragged(MouseEvent e) { 
  221.                     mouseAction.setText("MouseDragged@ [X:"+e.getX()+"Y:"+e.getY()+"]"); 
  222.          
  223.                     if (judge == 0||judge == 1) { 
  224.                         itemList[index - 1].sx = itemList[index].ex = itemList[index].sx = e 
  225.                                 .getX(); 
  226.                         itemList[index - 1].sy = itemList[index].ey = itemList[index].sy = e 
  227.                                 .getY(); 
  228.                         index++; 
  229.                         createNewItem(); 
  230.                     } else { 
  231.                         itemList[index].ex = e.getX(); 
  232.                         itemList[index].ey = e.getY(); 
  233.                     } 
  234.                     repaint(); 
  235.                 } 
  236.             }); 
  237.             addMouseListener(new MouseAdapter() { 
  238.                  
  239.                 @Override 
  240.                 public void mouseExited(MouseEvent e) { 
  241.                     mouseAction.setText("MouseExited@ [X:"+e.getX()+"Y:"+e.getY()+"]"); 
  242.                 } 
  243.                 public void mouseClicked(MouseEvent e) { 
  244.                     mouseAction.setText("MouseClicked@ [X:"+e.getX()+"Y:"+e.getY()+"]"); 
  245.                 } 
  246.  
  247.                 public void mousePressed(MouseEvent e) { 
  248.                     mouseAction.setText("MousePressed@ [X:"+e.getX()+"Y:"+e.getY()+"]"); 
  249.                      
  250.                     //itemList[index].sx = itemList[index].ex = e.getX(); 
  251.                     //itemList[index].sy = itemList[index].ey = e.getY(); 
  252.  
  253.                     if (judge == 0||judge == 1) { 
  254.                         itemList[index].sx = itemList[index].ex = e.getX(); 
  255.                         itemList[index].sy = itemList[index].ey = e.getY(); 
  256.                         index++; 
  257.                         createNewItem(); 
  258.                     }else { 
  259.                         itemList[index].sx = itemList[index].ex = e.getX(); 
  260.                         itemList[index].sy = itemList[index].ey = e.getY(); 
  261.                     } 
  262.  
  263.                 } 
  264.  
  265.                 public void mouseReleased(MouseEvent e) { 
  266.                     mouseAction.setText("MouseReleased@ [X:"+e.getX()+"Y:"+e.getY()+"]"); 
  267.  
  268.                     if (judge == 0||judge == 1) { 
  269.                         itemList[index].sx = e.getX(); 
  270.                         itemList[index].sy = e.getY(); 
  271.                     } 
  272.  
  273.                     itemList[index].ex = e.getX(); 
  274.                     itemList[index].ey = e.getY(); 
  275.                     repaint(); 
  276.                     index++; 
  277.                     createNewItem(); 
  278.  
  279.                 } 
  280.             }); 
  281.  
  282.             setVisible(true); 
  283.  
  284.         } 
  285.          
  286.  
  287.         public void paintComponent(Graphics g) { 
  288.             super.paintComponent(g); 
  289.             Graphics2D g2d = (Graphics2D) g; 
  290.             for(int i=0;i<=index;i++) { 
  291.                 draw(g2d, itemList[i]); 
  292.             } 
  293.             /*int j = 0; 
  294.             while (j <= index) { 
  295.                 draw(g2d, itemList[j]); 
  296.                 j++; 
  297.             }*/ 
  298.         } 
  299.  
  300.         void draw(Graphics2D g2d, Drawing i) { 
  301.             i.draw(g2d);// 将画笔传入到各个子类中,用来完成各自的绘图 
  302.         } 
  303.  
  304.     } 
  305.  
  306.  
  307.  
  308. import java.awt.BasicStroke; 
  309. import java.awt.Color; 
  310. import java.awt.Graphics2D; 
  311.  
  312. public class Drawing { 
  313.     int sx, sy, ex, ey; 
  314.     int R,G,B; 
  315.     float stroke; 
  316.  
  317.     public void draw(Graphics2D g) { 
  318.  
  319.     } 
  320.  
  321.  
  322. class Rect extends Drawing { 
  323.  
  324.     public void draw(Graphics2D g) { 
  325.         g.setPaint(new Color(R,G,B)); 
  326.         g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, 
  327.                 BasicStroke.JOIN_BEVEL)); 
  328.         g.drawRect(Math.min(sx, ex), Math.min(sy, ey), Math.abs(sx - ex), 
  329.                 Math.abs(sy - ey)); 
  330.     } 
  331.  
  332. class fillRect extends Drawing { 
  333.  
  334.     public void draw(Graphics2D g) { 
  335.         g.setPaint(new Color(R,G,B)); 
  336.         g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, 
  337.                 BasicStroke.JOIN_BEVEL)); 
  338.         g.fillRect(Math.min(sx, ex), Math.min(sy, ey), Math.abs(sx - ex), 
  339.                 Math.abs(sy - ey)); 
  340.     } 
  341.  
  342. class Oval extends Drawing { 
  343.  
  344.     public void draw(Graphics2D g) { 
  345.         g.setPaint(new Color(R,G,B)); 
  346.         g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, 
  347.                 BasicStroke.JOIN_BEVEL)); 
  348.         g.drawOval(Math.min(sx, ex), Math.min(sy, ey), Math.abs(ex - sx), 
  349.                 Math.abs(ey - sy)); 
  350.     } 
  351.  
  352. class fillOval extends Drawing { 
  353.  
  354.     public void draw(Graphics2D g) { 
  355.         g.setPaint(new Color(R,G,B)); 
  356.         g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, 
  357.                 BasicStroke.JOIN_BEVEL)); 
  358.         g.fillOval(Math.min(sx, ex), Math.min(sy, ey), Math.abs(ex - sx), 
  359.                 Math.abs(ey - sy)); 
  360.     } 
  361.  
  362. class Circle extends Drawing { 
  363.  
  364.     public void draw(Graphics2D g) { 
  365.         g.setPaint(new Color(R,G,B)); 
  366.         g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, 
  367.                 BasicStroke.JOIN_BEVEL)); 
  368.         g.drawOval(Math.min(sx, ex), Math.min(sy, ey), Math.abs(ey - sy), 
  369.                 Math.abs(ey - sy)); 
  370.     } 
  371.  
  372. class fillCircle extends Drawing { 
  373.  
  374.     public void draw(Graphics2D g) { 
  375.         g.setPaint(new Color(R,G,B)); 
  376.         g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, 
  377.                 BasicStroke.JOIN_BEVEL)); 
  378.         g.fillOval(Math.min(sx, ex), Math.min(sy, ey), Math.abs(ex - sx), 
  379.                 Math.abs(ex - sx)); 
  380.     } 
  381.  
  382. class Pen extends Drawing { 
  383.  
  384.     public void draw(Graphics2D g) { 
  385.         g.setPaint(new Color(R,G,B)); 
  386.         g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, 
  387.                 BasicStroke.JOIN_BEVEL)); 
  388.         g.drawLine(sx, sy, ex, ey); 
  389.     } 
  390. class Line extends Drawing { 
  391.  
  392.     public void draw(Graphics2D g) { 
  393.         g.setPaint(new Color(R,G,B)); 
  394.         g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, 
  395.                 BasicStroke.JOIN_BEVEL)); 
  396.         g.drawLine(sx, sy, ex, ey); 
  397.     } 
  398. class Rubber extends Drawing { 
  399.     public void draw(Graphics2D g2d) { 
  400.         g2d.setPaint(new Color(255255255)); 
  401.         g2d.setStroke(new BasicStroke(stroke + 5, BasicStroke.CAP_ROUND, 
  402.                 BasicStroke.JOIN_BEVEL)); 
  403.         g2d.drawLine(sx, sy, ex, ey); 
  404.     } 

 

你可能感兴趣的:(职场,J2SE,画图,休闲)