Application drawing board

From , 5 Years ago, written in Java, viewed 161 times.
URL https://pastebin.vip/view/e077e1a5
  1. 1.主类
  2. package s1221画图板重绘撤销;
  3.  
  4. import java.awt.BorderLayout;
  5. import java.awt.Color;
  6. import java.awt.Dimension;
  7. import java.awt.Graphics;
  8. import java.util.ArrayList;
  9. import javax.swing.*;
  10.  
  11. public class Main1221 extends JFrame{
  12.         ImageIcon image[]={
  13.                     new ImageIcon("E:/Michael/javaImage/扫雷图片/2.jpg"),//曲线
  14.                     new ImageIcon("E:/Michael/javaImage/扫雷图片/2.jpg"),//直线
  15.                     new ImageIcon("E:/Michael/javaImage/扫雷图片/2.jpg"), //矩形
  16.                     new ImageIcon("E:/Michael/javaImage/扫雷图片/2.jpg"),//椭圆
  17.                     };
  18.        
  19.          String st[]={"曲线","直线","矩形","椭圆","橡皮","颜色笔","填色","放大镜","刷子","圆角矩形","涂鸦","文字","多边形","填充矩形","文字","多边形","填充矩形"};
  20.                
  21.          Color b[] ={ Color.blue,Color.green,Color.lightGray,Color.orange,Color.black,
  22.                                 Color.cyan,Color.darkGray,Color.gray,Color.magenta,Color.pink,Color.red,
  23.                                 Color.yellow,Color.white,new Color(0,100,200),new Color(0,200,200)};
  24.                 static ArrayList<Shape> list = new ArrayList<Shape>();
  25.        
  26.                  Mymouselisten l1=new Mymouselisten();    //创建监听对象
  27.                  
  28.         public  Main1221()
  29.         {      
  30.                    //设置窗体属性*********************************************
  31.                         this.setDefaultCloseOperation(3);//设置关闭的方式
  32.                         this.setLayout( new BorderLayout());//设置窗体为边框布局
  33.                         this.setSize(800, 600);//设置窗体的大小
  34.                         this.setTitle("画板");  //设置窗体的标题      
  35.                         this.setResizable(true);//设置窗体不可改变大小
  36.                         this.setLocationRelativeTo(null);//设置窗体出现在屏幕中间
  37.                         //设置窗体属性*********************************************
  38.  
  39.                         //创建按钮**************************************
  40.  
  41.                         JMenuBar jbar=new JMenuBar();
  42.                         JMenuItem t1=new JMenuItem("撤销");
  43.                         JMenu m1=new JMenu("选项");
  44.                
  45.                         t1.addActionListener(l1);
  46.                         m1.add(t1);            
  47.                         jbar.add(m1);
  48.                 this.setJMenuBar(jbar);
  49.                 //创建按钮***************************************
  50.            
  51.                 //左边的面板************************************
  52.                         JPanel west=new JPanel();
  53.                         west.setPreferredSize(new Dimension(80,0));
  54.                         west.setBackground(Color.lightGray);
  55.                         this.add(west,BorderLayout.WEST);
  56.                         //左边的面板************************************
  57.                
  58.                         //下方的面板********************************************
  59.                         JPanel south =new JPanel();
  60.                         south.setPreferredSize(new Dimension(0,100));
  61.                         south.setBackground(Color.pink);
  62.                         this.add(south,BorderLayout.SOUTH);
  63.                         //下方的面板*********************************************
  64.                        
  65.                        
  66.                         //绘画的面板**************************************************************
  67.                          JPanel drawpanel =new JPanel()
  68.                           {
  69.                                                 public void paint(Graphics g)         //重写JPanel中的方法
  70.                                                 {
  71.                                                         super.paint(g);                                                        
  72.                                                         list.forEach((shape)->shape.draw(g));  //lambda表达式                       
  73.                                                 }      
  74.                          };
  75.                         //绘画的面板**************************************************************
  76.                
  77.                          
  78.                          //中间的面板********************************************
  79.                         JPanel center =new JPanel();
  80.                         center.setBackground(Color.GRAY);
  81.                         center.setLayout(new BorderLayout());
  82.                         center.add(drawpanel);
  83.                         this.add(center,BorderLayout.CENTER);
  84.                         //中间的面板*********************************************
  85.                        
  86.                         drawpanel.setBackground(Color.WHITE);          
  87.                     drawpanel.addMouseListener(l1);                  //给监听方法传递监听的对象
  88.                     drawpanel.addMouseMotionListener(l1);        //虽然l1实现了多个接口,但是面板要监听鼠标拖动事件,要分别添加方法
  89.                    
  90.                    
  91.                         //**********************************窗体左边的选项栏***************************************//
  92.                         for(int i=0;i<image.length;i++)
  93.                         {
  94.                         JButton jb=new JButton(image[i]);
  95.                         jb.setActionCommand(st[i]);                      //给这个图标添加action command 方便在事件中获取
  96.                         jb.setPreferredSize(new Dimension(image[i].getIconWidth(),image[i].getIconHeight()));
  97.                         jb.setToolTipText(st[i]);
  98.                         jb.addActionListener(l1);
  99.                         west.add(jb);
  100.                         }                        
  101.                         //*****************************************************************************************//
  102.                        
  103.                         //**********************************窗体下方颜色选择框***************************************//       
  104.                         for(Color i:b)
  105.                         {
  106.                          JButton jb1=new JButton("");  
  107.                          jb1.setBackground(i);
  108.                          jb1.setPreferredSize(new Dimension(30,30));
  109.                          jb1.addActionListener(l1);
  110.                          south.add(jb1);
  111.                         }
  112.                         //*****************************************************************************************//
  113.                        
  114.                         this.setVisible(true);                           //要先设置窗体可见,然后再传画笔       
  115.                         Graphics g=drawpanel.getGraphics();                      //传递画笔 ,我一开始放在最后,结果不能显示!!!传画笔之前一定要先设置可见
  116.                         l1.setGraphics(g,list,drawpanel);                  
  117.         }
  118.        
  119.         public static void main(String[] args)
  120.         { Main1221 k=new Main1221();}
  121. }
  122.        
  123.  
  124.  

Reply to "Application drawing board"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website