鱼C论坛

 找回密码
 立即注册
查看: 2565|回复: 2

[已解决]JAVA swing 简易画板

[复制链接]
发表于 2021-12-14 22:19:55 | 显示全部楼层 |阅读模式
60鱼币
GitHub上抄了个简易画板的代码
源自:(https://github.com/Fattybenny/ja ... E%E5%B7%A5%E5%85%B7
一个简易的画板,copy到eclipse 后报错情况在图片里
就是提示Paint类里
  1. jb.addActionListener(lis);
复制代码
  1. jb.addActionListener(lis);
复制代码
这两行报错
大佬们帮忙看看啥问题,(当然能给个差不多的画板源代码也可以)谢谢

源码如下:

  1. package swing_01;

  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.event.MouseAdapter;
  5. import java.awt.event.MouseEvent;
  6. import javax.swing.*;
  7. import java.awt.event.ActionEvent;


  8. public class Listener extends MouseAdapter  {
  9.         //一定要将接口的所有方法重写
  10.         private int index=0;
  11.         private Graphics gr;
  12.         private String command="";       
  13.         private Paint p;
  14.         private Color color=Color.black;
  15.         int x0=0,y0=0,//曲线的起始坐标
  16.             x1=0,y1=0,x2=0,y2=0,//其他图形
  17.             x3=0,x4=0,y3=0,y4=0,
  18.             start_x=0,start_y=0;//多边形起点

  19.         //初始化数据
  20.         //基本数据类型向形参传递的是副本,引用数据类型(类,接口,数组)向形参传递的地址,如下传递jp       
  21.         public void set_gr(Graphics G) {
  22.                 this.gr = G;
  23.         }       

  24.         public void set_p(Paint p)
  25.         {
  26.                 this.p=p;
  27.         }
  28.                        
  29.         /*
  30.          * getSource得到的组件的名称,而getActionCommand得到的是标签。
  31.            如:Button bt=new Button("buttons");
  32.              用getSource得到的是bt 而用getActionCommand得到的是:buttons
  33.          */
  34.         /*
  35.          * 这里必须先点击被命名的按钮(即要画什么图形),将"zhiling"初始化,
  36.          * 否则后面的监听回应方法都不能执行,如果不点击颜色按钮(只设置了按钮的背景颜色,没有settext),
  37.          * 则不会为画笔setcolor
  38.          * 默认黑色
  39.          */
  40.         public void actionPerformed(ActionEvent e)
  41.         {
  42.                
  43.                 // TODO Auto-generated method stub
  44.                 //在画板上描绘图案的时候用的是同一个画笔,只不过每次使用之前都把画笔的颜色修改了               
  45.                                  if (e.getActionCommand()=="") {
  46.                           //j暂存颜色按钮  
  47.                         JButton j = (JButton)e.getSource();
  48.                         //将画笔设置为按钮的背景颜色
  49.                         color=j.getBackground();
  50.                 }
  51.                 else {
  52.                         command = e.getActionCommand();
  53.                         if("清除".equals(command))
  54.                         {
  55.                                 Graphics cleang=p.getGraphics();
  56.                                 //JFrame和JPanel的repaint方法是同一个,都是继承自JComponent的,对该方法的调用执行的都是同样的代码的。
  57.                                
  58.                                         p.paint1(cleang);
  59.                                 p.shape=new Shape[10000];
  60.                                 index=0;
  61.                         }
  62.                 }
  63.         }
  64.        
  65.         //鼠标按键在组件上按下(长按)并拖动时(在拖动的过程中)调用。   (处理鼠标拖动事件)
  66.                         public void mouseDragged(MouseEvent e) {
  67.                                 //System.out.println("Drag");                               
  68.                                 if("曲线".equals(command)) {
  69.                                         x1 = x0; y1 = y0; //鼠标按下的时候就获得了起始坐标
  70.                                         x0 = e.getX(); y0 = e.getY();
  71.                                         gr.drawLine(x1,y1,x0,y0);
  72.                                         p.shape[index++]= new Shape(x1,y1,x0,y0,command,gr.getColor());
  73.                                 }
  74.                         }
  75.                 
  76.                
  77.                         // Override鼠标监听//点击鼠标点击时间,用来绘画多边形
  78.                         public void mouseClicked(MouseEvent e) //是点击和松开两步完成后才被再调用(前提是点击和松开的位置是相同点) 在此之前,pressed 和released都已经被调用
  79.                                                                    
  80.                         {        
  81.                                 System.out.println("鼠标click事件发生");
  82.                                 // TODO Auto-generated method stub
  83.                                 if("多边形".equals(command))
  84.                                 {
  85.                                        
  86.                                         if(x4==0&&y4==0)
  87.                                             {
  88.                                                
  89.                                                 x4 = e.getX(); y4 = e.getY();
  90.                                                 start_x = x4; start_y = y4;
  91.                                                 //记录多边形的起点位置
  92.                                               }
  93.                                        
  94.                                         else {
  95.                                                 //System.out.println("Here is duobianxing ");
  96.                                                 x3 = x4; y3 = y4;//这里的x3,y3为上一条边的终点
  97.                                                 x4 = e.getX(); y4 = e.getY();
  98.                                                 gr.drawLine(x3, y3, x4, y4);//将这点与上一条边的终点连起来
  99.                                                 p.shape[index++]= new Shape(x3,y3,x4,y4,command,gr.getColor());
  100.                                              }
  101.                                         if(e.getClickCount()==2)
  102.                                         {
  103.                                         //System.out.println("双击");
  104.                                         x4 =0; y4=0;//这里设置为零是为了能都再次执行if语句
  105.                                         gr.drawLine(start_x, start_y, e.getX(), e.getY());//将双击的终点位置和开始记录的起点位置相连
  106.                                         p.shape[index++]= new Shape(start_x, start_y, e.getX(), e.getY(),command,gr.getColor());
  107.                                         }       
  108.                                 }
  109.                         }
  110.                                
  111.                                  //当鼠标按下的时候,获取起点的坐标
  112.                                  public void mousePressed(MouseEvent e)
  113.                                 {
  114.                                           gr=p.getGraphics();
  115.                                           gr.setColor(color);
  116.                                System.out.println("pressed事件发生");
  117.                                        
  118.                                         if("曲线".equals(command)){
  119.                                                 x0 = e.getX();
  120.                                                 y0 = e.getY();
  121.                                         }
  122.                                         // TODO Auto-generated method stub
  123.                                         x1 = e.getX();
  124.                                         y1 = e.getY();
  125.                                        
  126.                                 }
  127.                                
  128.                                
  129.                                 //当鼠标松开的时候,获取终点的坐标
  130.                                 public void mouseReleased(MouseEvent e)
  131.                                 {
  132.                                         System.out.println("released事件发生");
  133.                                         // TODO Auto-generated method stub
  134.                                         x2 = e.getX();
  135.                                         y2 = e.getY();
  136.                                
  137.                                         if("直线".equals(command)){
  138.                                                 gr.drawLine(x1, y1, x2, y2);
  139.                                                 p.shape[index++]= new Shape(x1,y1,x2,y2,command,gr.getColor());
  140.                                                
  141.                                         }
  142.                                         else if("矩形".equals(command)) {
  143.                                                 gr.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
  144.                                                 p.shape[index++]= new Shape(x1,y1,x2,y2,command,gr.getColor());
  145.                                         }
  146.                                         else if("圆形".equals(command)) {
  147.                                                 //Math.min(x1, x2), Math.min(y1, y2),
  148.                                                 //drawoval 和drawrect 都是以某个点 向着坐标点增大的方向画图
  149.                                                 //Math.abs(x1-x2)求绝对值
  150.                                                 gr.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
  151.                                                 p.shape[index++]= new Shape(x1,y1,x2,y2,command,gr.getColor());
  152.                                                                                         }
  153.                                 }
  154.                                 //鼠标进入窗口
  155.                                 public void mouseEntered(MouseEvent e)
  156.                                 {
  157.                                        
  158.                                 }
  159.                  //鼠标退出窗口
  160.                             public void mouseExited(MouseEvent e)
  161.                             {
  162.                                    
  163.                             }
  164.                                 //鼠标在移动的过程中调用(没有点击)
  165.                             public void mouseMoved(MouseEvent e)
  166.                          {
  167.                                  
  168.                          }
  169.                         
  170.                                
  171.                         }
复制代码


  1. package swing_01;
  2. import java.awt.*;

  3. import javax.swing.*;
  4. public class Paint extends JPanel{

  5.         //建立一个很大的数组,用来存放图形元素,
  6.         Shape[] shape=new Shape[10000];
  7.         public static void main(String[] args) {
  8.                  new Paint();
  9.         }
  10.           JFrame jf = new JFrame();          
  11.           
  12.         //实例化一个ButtonListener对象,实现了多种接口
  13.         Listener lis= new Listener();
  14.        
  15.         public Paint() {
  16.                   //实现一个窗体
  17.                   jf.setTitle("画图板");
  18.                   jf.setLocation(600,250);//窗口左上角起点
  19.                   jf.setSize(700, 600);
  20.                   jf.setLayout(new FlowLayout());       
  21.                   
  22.                    //1.setPreferredSize需要在使用布局管理器的时候使用,布局管理器会获取空间的preferredsize,因而可以生效。
  23.                   //例如borderlayout在north中放入一个panel,panel的高度可以通过这样实现:panel.setPreferredSize(new Dimension(0, 100));
  24.                   //这样就设置了一个高度为100的panel,宽度随窗口变化。
  25.                   //2.setSize,setLocation,setBounds方法需要在不使用布局管理器的时候使用,也就是setLayout(null)的时候可以使用这三个方法控制布局。
  26.                   //区分好这两个不同点之后,

  27.                   
  28.           //声明两个数组,包含各种指令
  29.                       String[] command = { "开始", "清除", "直线", "曲线", "多边形", "矩形","圆形" };
  30.                       Color[] color = { Color.BLACK, Color.BLUE, Color.YELLOW, Color.RED, Color.GREEN };
  31.    
  32.    
  33.                     /*在下面的两个循环中将各种按钮添加进入动作监听器中,其中addActionListener参数为btl,
  34.                     btl是一个Buttonlistener的对象
  35.                     */
  36.                      
  37.                    for (int i = 0; i < command.length; i++) {
  38.                            JButton jb = new JButton(command[i]);
  39.                            jb.addActionListener(lis);
  40.                            jf.add(jb);
  41.                    }
  42.                    for (int i = color.length - 1; i >= 0; i--) {
  43.                            JButton jb = new JButton();
  44.                            jb.setBackground(color[i]);//设置背景颜色
  45.                            Dimension dm = new Dimension(20, 20);//设置大小
  46.                            jb.setPreferredSize(dm);
  47.                            jb.addActionListener(lis);
  48.                            jf.add(jb);
  49.                    }
  50.                   
  51.                //将JPanel对象添加进入jf窗体对象中,让他生效
  52.             //jpanel画板的大小和颜色
  53.                   this.setPreferredSize(new Dimension(1600, 1400));
  54.           this.setBackground(Color.LIGHT_GRAY);
  55.          
  56.          
  57.           //这里注意流程  我们实在rootpane上进行流式布局 现将各个按钮添加完,在添加这个画板 P
  58.                         jf.add(this);
  59.                         jf.setDefaultCloseOperation(3);
  60.                         jf.setVisible(true);        //设置可见
  61.                            //Graphics gf = this.getGraphics();        //获取画笔,我们的this代表当前类的对象,正好是一个JPanel的对象
  62.                            this.addMouseListener(lis);                //添加鼠标监听器,用于画图
  63.                            this.addMouseMotionListener(lis);        //添加鼠标模式监听器,用于绘画曲线                        
  64.                            lis.set_p(this);//给监听类当中的Paint 对象赋初值,方便通过p调用paint1 方法
  65.         }
  66.        
  67.         //这里是重写jpanel中的paint   
  68.         //????这里的重绘画笔与上面的gf应该不相同
  69.        
  70.         //一下知识点经验:当我们重写父类中的方法后,如果想在其他类中通过对象调用父类的方法时
  71.         //应该再重新创建一个方法,名字与被重写的父类方法不同,这个方法不是重写,只是通过这个方法调用父类的方法
  72.        
  73.         public void paint(Graphics g)//前面有三角图案 代表是重写 (重写在当前的子类中)  
  74.         {
  75.                 super.paint(g);//这是原先的父类方法 本来只要绘制组件的功能
  76.                        //所以现在我们要加一个绘制图形的功能如下
  77.                 for(int i=0;i<=shape.length;i++)
  78.                 {
  79.                         if(shape[i]!=null)
  80.                         {  
  81.                              g.setColor(shape[i].getcolor());
  82.                                 shape[i].Repaint(g);
  83.                         }
  84.                         else
  85.                                 break;
  86.                 }
  87.                 System.out.println("paint");
  88.         }
  89.        
  90.         public void paint1(Graphics g)
  91.         {
  92.                 super.paint(g);
  93.         }
  94.        
  95. }
复制代码


  1. package swing_01;

  2. import java.awt.Color;
  3. import java.awt.Graphics;

  4. public class Shape{
  5. private int x1,y1,x2,y2;
  6. private String name;
  7. private Graphics g;
  8. private Color color;

  9. public Shape(int x1,int y1,int x2,int y2,String name,Color color)
  10. {
  11.         this.x1=x1;
  12.         this.x2=x2;
  13.         this.y1=y1;
  14.         this.y2=y2;
  15.         this.name=name;
  16.         this.color=color;
  17. }

  18. public Color getcolor()
  19. {
  20.         return color;
  21. }

  22. public void Repaint(Graphics g)
  23. {
  24.         switch(name)
  25.         {
  26.         case "直线":
  27.                 g.drawLine(x1, y1, x2, y2);
  28.                 break;
  29.         case "矩形":
  30.                 g.drawRect(Math.min(x1, x2), Math.min(y1 ,y2), Math.abs(x1-x2), Math.abs(y1-y2));
  31.                 break;
  32.         case "圆形":
  33.                 g.drawOval(Math.min(x1, x2), Math.min(y1 ,y2), Math.abs(x1-x2), Math.abs(y1-y2));
  34.                 break;
  35.         case "曲线":
  36.                 g.drawLine(x1, y1, x2, y2);
  37.                 break;
  38.         case "多边形":
  39.                 g.drawLine(x1, y1, x2, y2);
  40.                 break;
  41.         }
  42. }
  43. }
  44.        
复制代码

最佳答案
2021-12-14 22:19:56
应该是引用问题  给你修改下 你看看可以使不
  1. package iih.ci.test.swing;

  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.awt.event.MouseMotionListener;

  9. import javax.swing.JButton;


  10. public class Listener implements ActionListener,MouseListener,MouseMotionListener {
  11.         //一定要将接口的所有方法重写
  12.         private int index=0;
  13.         private Graphics gr;
  14.         private String command="";       
  15.         private Paint p;
  16.         private Color color=Color.black;
  17.         int x0=0,y0=0,//曲线的起始坐标
  18.             x1=0,y1=0,x2=0,y2=0,//其他图形
  19.             x3=0,x4=0,y3=0,y4=0,
  20.             start_x=0,start_y=0;//多边形起点

  21.         //初始化数据
  22.         //基本数据类型向形参传递的是副本,引用数据类型(类,接口,数组)向形参传递的地址,如下传递jp       
  23.         public void set_gr(Graphics G) {
  24.                 this.gr = G;
  25.         }       

  26.         public void set_p(Paint p)
  27.         {
  28.                 this.p=p;
  29.         }
  30.                        
  31.         /*
  32.          * getSource得到的组件的名称,而getActionCommand得到的是标签。
  33.            如:Button bt=new Button("buttons");
  34.              用getSource得到的是bt 而用getActionCommand得到的是:buttons
  35.          */
  36.         /*
  37.          * 这里必须先点击被命名的按钮(即要画什么图形),将"zhiling"初始化,
  38.          * 否则后面的监听回应方法都不能执行,如果不点击颜色按钮(只设置了按钮的背景颜色,没有settext),
  39.          * 则不会为画笔setcolor
  40.          * 默认黑色
  41.          */
  42.         public void actionPerformed(ActionEvent e)
  43.         {
  44.                
  45.                 // TODO Auto-generated method stub
  46.                 //在画板上描绘图案的时候用的是同一个画笔,只不过每次使用之前都把画笔的颜色修改了               
  47.                                  if (e.getActionCommand()=="") {
  48.                           //j暂存颜色按钮  
  49.                         JButton j = (JButton)e.getSource();
  50.                         //将画笔设置为按钮的背景颜色
  51.                         color=j.getBackground();
  52.                 }
  53.                 else {
  54.                         command = e.getActionCommand();
  55.                         if("清除".equals(command))
  56.                         {
  57.                                 Graphics cleang=p.getGraphics();
  58.                                 //JFrame和JPanel的repaint方法是同一个,都是继承自JComponent的,对该方法的调用执行的都是同样的代码的。
  59.                                
  60.                                         p.paint1(cleang);
  61.                                 p.shape=new Shape[10000];
  62.                                 index=0;
  63.                         }
  64.                 }
  65.         }
  66.        
  67.         //鼠标按键在组件上按下(长按)并拖动时(在拖动的过程中)调用。   (处理鼠标拖动事件)
  68.                         public void mouseDragged(MouseEvent e) {
  69.                                 //System.out.println("Drag");                               
  70.                                 if("曲线".equals(command)) {
  71.                                         x1 = x0; y1 = y0; //鼠标按下的时候就获得了起始坐标
  72.                                         x0 = e.getX(); y0 = e.getY();
  73.                                         gr.drawLine(x1,y1,x0,y0);
  74.                                         p.shape[index++]= new Shape(x1,y1,x0,y0,command,gr.getColor());
  75.                                 }
  76.                         }
  77.                 
  78.                
  79.                         // Override鼠标监听//点击鼠标点击时间,用来绘画多边形
  80.                         public void mouseClicked(MouseEvent e) //是点击和松开两步完成后才被再调用(前提是点击和松开的位置是相同点) 在此之前,pressed 和released都已经被调用
  81.                                                                    
  82.                         {        
  83.                                 System.out.println("鼠标click事件发生");
  84.                                 // TODO Auto-generated method stub
  85.                                 if("多边形".equals(command))
  86.                                 {
  87.                                        
  88.                                         if(x4==0&&y4==0)
  89.                                             {
  90.                                                
  91.                                                 x4 = e.getX(); y4 = e.getY();
  92.                                                 start_x = x4; start_y = y4;
  93.                                                 //记录多边形的起点位置
  94.                                               }
  95.                                        
  96.                                         else {
  97.                                                 //System.out.println("Here is duobianxing ");
  98.                                                 x3 = x4; y3 = y4;//这里的x3,y3为上一条边的终点
  99.                                                 x4 = e.getX(); y4 = e.getY();
  100.                                                 gr.drawLine(x3, y3, x4, y4);//将这点与上一条边的终点连起来
  101.                                                 p.shape[index++]= new Shape(x3,y3,x4,y4,command,gr.getColor());
  102.                                              }
  103.                                         if(e.getClickCount()==2)
  104.                                         {
  105.                                         //System.out.println("双击");
  106.                                         x4 =0; y4=0;//这里设置为零是为了能都再次执行if语句
  107.                                         gr.drawLine(start_x, start_y, e.getX(), e.getY());//将双击的终点位置和开始记录的起点位置相连
  108.                                         p.shape[index++]= new Shape(start_x, start_y, e.getX(), e.getY(),command,gr.getColor());
  109.                                         }       
  110.                                 }
  111.                         }
  112.                                
  113.                                  //当鼠标按下的时候,获取起点的坐标
  114.                                  public void mousePressed(MouseEvent e)
  115.                                 {
  116.                                           gr=p.getGraphics();
  117.                                           gr.setColor(color);
  118.                                System.out.println("pressed事件发生");
  119.                                        
  120.                                         if("曲线".equals(command)){
  121.                                                 x0 = e.getX();
  122.                                                 y0 = e.getY();
  123.                                         }
  124.                                         // TODO Auto-generated method stub
  125.                                         x1 = e.getX();
  126.                                         y1 = e.getY();
  127.                                        
  128.                                 }
  129.                                
  130.                                
  131.                                 //当鼠标松开的时候,获取终点的坐标
  132.                                 public void mouseReleased(MouseEvent e)
  133.                                 {
  134.                                         System.out.println("released事件发生");
  135.                                         // TODO Auto-generated method stub
  136.                                         x2 = e.getX();
  137.                                         y2 = e.getY();
  138.                                
  139.                                         if("直线".equals(command)){
  140.                                                 gr.drawLine(x1, y1, x2, y2);
  141.                                                 p.shape[index++]= new Shape(x1,y1,x2,y2,command,gr.getColor());
  142.                                                
  143.                                         }
  144.                                         else if("矩形".equals(command)) {
  145.                                                 gr.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
  146.                                                 p.shape[index++]= new Shape(x1,y1,x2,y2,command,gr.getColor());
  147.                                         }
  148.                                         else if("圆形".equals(command)) {
  149.                                                 //Math.min(x1, x2), Math.min(y1, y2),
  150.                                                 //drawoval 和drawrect 都是以某个点 向着坐标点增大的方向画图
  151.                                                 //Math.abs(x1-x2)求绝对值
  152.                                                 gr.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
  153.                                                 p.shape[index++]= new Shape(x1,y1,x2,y2,command,gr.getColor());
  154.                                                                                         }
  155.                                 }
  156.                                 //鼠标进入窗口
  157.                                 public void mouseEntered(MouseEvent e)
  158.                                 {
  159.                                        
  160.                                 }
  161.                  //鼠标退出窗口
  162.                             public void mouseExited(MouseEvent e)
  163.                             {
  164.                                    
  165.                             }
  166.                                 //鼠标在移动的过程中调用(没有点击)
  167.                             public void mouseMoved(MouseEvent e)
  168.                          {
  169.                                  
  170.                          }
  171.                         
  172.                                
  173.                         }

复制代码

报错情况

报错情况

最佳答案

查看完整内容

应该是引用问题 给你修改下 你看看可以使不
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-14 22:19:56 | 显示全部楼层    本楼为最佳答案   
应该是引用问题  给你修改下 你看看可以使不
  1. package iih.ci.test.swing;

  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.awt.event.MouseMotionListener;

  9. import javax.swing.JButton;


  10. public class Listener implements ActionListener,MouseListener,MouseMotionListener {
  11.         //一定要将接口的所有方法重写
  12.         private int index=0;
  13.         private Graphics gr;
  14.         private String command="";       
  15.         private Paint p;
  16.         private Color color=Color.black;
  17.         int x0=0,y0=0,//曲线的起始坐标
  18.             x1=0,y1=0,x2=0,y2=0,//其他图形
  19.             x3=0,x4=0,y3=0,y4=0,
  20.             start_x=0,start_y=0;//多边形起点

  21.         //初始化数据
  22.         //基本数据类型向形参传递的是副本,引用数据类型(类,接口,数组)向形参传递的地址,如下传递jp       
  23.         public void set_gr(Graphics G) {
  24.                 this.gr = G;
  25.         }       

  26.         public void set_p(Paint p)
  27.         {
  28.                 this.p=p;
  29.         }
  30.                        
  31.         /*
  32.          * getSource得到的组件的名称,而getActionCommand得到的是标签。
  33.            如:Button bt=new Button("buttons");
  34.              用getSource得到的是bt 而用getActionCommand得到的是:buttons
  35.          */
  36.         /*
  37.          * 这里必须先点击被命名的按钮(即要画什么图形),将"zhiling"初始化,
  38.          * 否则后面的监听回应方法都不能执行,如果不点击颜色按钮(只设置了按钮的背景颜色,没有settext),
  39.          * 则不会为画笔setcolor
  40.          * 默认黑色
  41.          */
  42.         public void actionPerformed(ActionEvent e)
  43.         {
  44.                
  45.                 // TODO Auto-generated method stub
  46.                 //在画板上描绘图案的时候用的是同一个画笔,只不过每次使用之前都把画笔的颜色修改了               
  47.                                  if (e.getActionCommand()=="") {
  48.                           //j暂存颜色按钮  
  49.                         JButton j = (JButton)e.getSource();
  50.                         //将画笔设置为按钮的背景颜色
  51.                         color=j.getBackground();
  52.                 }
  53.                 else {
  54.                         command = e.getActionCommand();
  55.                         if("清除".equals(command))
  56.                         {
  57.                                 Graphics cleang=p.getGraphics();
  58.                                 //JFrame和JPanel的repaint方法是同一个,都是继承自JComponent的,对该方法的调用执行的都是同样的代码的。
  59.                                
  60.                                         p.paint1(cleang);
  61.                                 p.shape=new Shape[10000];
  62.                                 index=0;
  63.                         }
  64.                 }
  65.         }
  66.        
  67.         //鼠标按键在组件上按下(长按)并拖动时(在拖动的过程中)调用。   (处理鼠标拖动事件)
  68.                         public void mouseDragged(MouseEvent e) {
  69.                                 //System.out.println("Drag");                               
  70.                                 if("曲线".equals(command)) {
  71.                                         x1 = x0; y1 = y0; //鼠标按下的时候就获得了起始坐标
  72.                                         x0 = e.getX(); y0 = e.getY();
  73.                                         gr.drawLine(x1,y1,x0,y0);
  74.                                         p.shape[index++]= new Shape(x1,y1,x0,y0,command,gr.getColor());
  75.                                 }
  76.                         }
  77.                 
  78.                
  79.                         // Override鼠标监听//点击鼠标点击时间,用来绘画多边形
  80.                         public void mouseClicked(MouseEvent e) //是点击和松开两步完成后才被再调用(前提是点击和松开的位置是相同点) 在此之前,pressed 和released都已经被调用
  81.                                                                    
  82.                         {        
  83.                                 System.out.println("鼠标click事件发生");
  84.                                 // TODO Auto-generated method stub
  85.                                 if("多边形".equals(command))
  86.                                 {
  87.                                        
  88.                                         if(x4==0&&y4==0)
  89.                                             {
  90.                                                
  91.                                                 x4 = e.getX(); y4 = e.getY();
  92.                                                 start_x = x4; start_y = y4;
  93.                                                 //记录多边形的起点位置
  94.                                               }
  95.                                        
  96.                                         else {
  97.                                                 //System.out.println("Here is duobianxing ");
  98.                                                 x3 = x4; y3 = y4;//这里的x3,y3为上一条边的终点
  99.                                                 x4 = e.getX(); y4 = e.getY();
  100.                                                 gr.drawLine(x3, y3, x4, y4);//将这点与上一条边的终点连起来
  101.                                                 p.shape[index++]= new Shape(x3,y3,x4,y4,command,gr.getColor());
  102.                                              }
  103.                                         if(e.getClickCount()==2)
  104.                                         {
  105.                                         //System.out.println("双击");
  106.                                         x4 =0; y4=0;//这里设置为零是为了能都再次执行if语句
  107.                                         gr.drawLine(start_x, start_y, e.getX(), e.getY());//将双击的终点位置和开始记录的起点位置相连
  108.                                         p.shape[index++]= new Shape(start_x, start_y, e.getX(), e.getY(),command,gr.getColor());
  109.                                         }       
  110.                                 }
  111.                         }
  112.                                
  113.                                  //当鼠标按下的时候,获取起点的坐标
  114.                                  public void mousePressed(MouseEvent e)
  115.                                 {
  116.                                           gr=p.getGraphics();
  117.                                           gr.setColor(color);
  118.                                System.out.println("pressed事件发生");
  119.                                        
  120.                                         if("曲线".equals(command)){
  121.                                                 x0 = e.getX();
  122.                                                 y0 = e.getY();
  123.                                         }
  124.                                         // TODO Auto-generated method stub
  125.                                         x1 = e.getX();
  126.                                         y1 = e.getY();
  127.                                        
  128.                                 }
  129.                                
  130.                                
  131.                                 //当鼠标松开的时候,获取终点的坐标
  132.                                 public void mouseReleased(MouseEvent e)
  133.                                 {
  134.                                         System.out.println("released事件发生");
  135.                                         // TODO Auto-generated method stub
  136.                                         x2 = e.getX();
  137.                                         y2 = e.getY();
  138.                                
  139.                                         if("直线".equals(command)){
  140.                                                 gr.drawLine(x1, y1, x2, y2);
  141.                                                 p.shape[index++]= new Shape(x1,y1,x2,y2,command,gr.getColor());
  142.                                                
  143.                                         }
  144.                                         else if("矩形".equals(command)) {
  145.                                                 gr.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
  146.                                                 p.shape[index++]= new Shape(x1,y1,x2,y2,command,gr.getColor());
  147.                                         }
  148.                                         else if("圆形".equals(command)) {
  149.                                                 //Math.min(x1, x2), Math.min(y1, y2),
  150.                                                 //drawoval 和drawrect 都是以某个点 向着坐标点增大的方向画图
  151.                                                 //Math.abs(x1-x2)求绝对值
  152.                                                 gr.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
  153.                                                 p.shape[index++]= new Shape(x1,y1,x2,y2,command,gr.getColor());
  154.                                                                                         }
  155.                                 }
  156.                                 //鼠标进入窗口
  157.                                 public void mouseEntered(MouseEvent e)
  158.                                 {
  159.                                        
  160.                                 }
  161.                  //鼠标退出窗口
  162.                             public void mouseExited(MouseEvent e)
  163.                             {
  164.                                    
  165.                             }
  166.                                 //鼠标在移动的过程中调用(没有点击)
  167.                             public void mouseMoved(MouseEvent e)
  168.                          {
  169.                                  
  170.                          }
  171.                         
  172.                                
  173.                         }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-12-15 19:29:40 | 显示全部楼层
412046761 发表于 2021-12-14 22:19
应该是引用问题  给你修改下 你看看可以使不

已经可以,谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-7 15:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表