画图板小程序

画图板小程序

上课老师让我们写的画板程序,写的实在是太难看了,功能很简单,保存着以后看看。

//测试类
public class Test {
     public static void main(String[] args) {
           //创建一个画图板对象
          DrawPanelDemo d = new DrawPanelDemo();
           //调用DrawPanelDemo类中的监听器方法
          d.Listener();
           //调用DrawPanelDemo类中的画图方法
          d.drawMetoh();
           //调用DrawPanelDemo类中的鼠标拖动监听方法
          d.myMouseMotionListener();
     }
}

画图板小程序
画图板小程序

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;

//画板类
public class DrawPanelDemo extends JFrame{
     
     //坐标
     private int x1;
     private int x;
     private int x2;
     private int y1;
     private int y;
     private int y2;
     private Color color = Color. black;
     private  int taye;
     //创建按钮
     JButton b1 = new JButton( "直线");
     JButton b2 = new JButton( "矩形");
     JButton b3 = new JButton( "圆");
     JButton b4 = new JButton();        //橙色按钮
     JButton b5 = new JButton();        //蓝色按钮
     JButton b6 = new JButton();        //黑色按钮
     JButton b7 = new JButton();        //红色按钮
     //创建Graphics对象
     Graphics g;
     //存取图像信息
     ArrayList<Line> line = new ArrayList<Line>();
     ArrayList<Rectangle> rect = new ArrayList<Rectangle>();
     ArrayList<Round> round = new ArrayList<Round>();
     //重写paint方法,图像重绘
     @Override
     public void paint(Graphics g) {
           super.paint(g);
           for(Line l : line){
              g = DrawPanelDemo. this.getGraphics();
              g.setColor(l.getColor());
              g.drawLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
          }
           for(Rectangle r: rect){
              g = DrawPanelDemo. this.getGraphics();
              g.setColor(r.getColor());
              g.drawRect(r.getX(), r.getY(), Math.abs(r.getX2()-r.getX1()), Math.abs(r.getY2()-r.getY1()));
          }
           for(Round ro : round){
              g = DrawPanelDemo. this.getGraphics();
              g.setColor(ro.getColor());
              g.drawOval(ro.getX(), ro.getY(), Math.abs(ro.getX2()-ro.getX1()), Math.abs(ro.getY2()-ro.getY1()));
          }
     }
     
     public DrawPanelDemo(){
           //设置布局
           super( "画图板");
           this.setSize(800, 490);
           this.setLayout( null);
           b1.setBounds(0, 0, 65, 65);
           b2.setBounds(0, 65, 65, 65);
           b3.setBounds(0, 130, 65, 65);
           b4.setBounds(0, 195, 65, 65);
           b5.setBounds(0, 260, 65, 65);
           b6.setBounds(0, 325, 65, 65);
           b7.setBounds(0, 390, 65, 65);
           b4.setBackground(Color. orange);
           b5.setBackground(Color. blue);
           b6.setBackground(Color. black);
           b7.setBackground(Color. red);
          add( b1);
          add( b2);
          add( b3);
          add( b4);
          add( b5);
          add( b6);
          add( b7);
           this.setVisible( true);
           this.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
     }
     
     //按钮监听
     public void Listener(){
           b1.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    taye = 1;  //画直线
              }
          });
           b2.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    taye = 2;  //画矩形
              }
          });
           b3.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    taye = 3;  //画圆
              }
          });
           b4.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    color = Color. orange;
              }
          });
           b5.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    color = Color. blue;
              }
          });
           b6.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    color = Color. black;
              }
          });
           b7.addActionListener( new ActionListener() {
              
               @Override
               public void actionPerformed(ActionEvent e) {
                    color = Color. red;
              }
          });
     }
     //实现画图功能
     public void drawMetoh(){
          addMouseListener( new MouseListener() {
              
               @Override
               public void mouseReleased(MouseEvent e) {
                    x2 = e.getX();
                    y2 = e.getY();
                    //g.drawLine(x1, y1, x2, y2);
                    g = DrawPanelDemo. this.getGraphics();
                    if( taye == 1){
                         g.setColor( color);
                         g.drawLine( x1, y1, x2, y2);
                         line.add( new Line( x1, x2, y1, y2, color));
                   } else if( taye == 2){
                         if( x1< x2 && y1< y2){
                              x = x1;
                              y = y1;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              rect.add( new Rectangle(x,y ,x1 , x2 , y1 , y2 , color ));
                        } else if( x1> x2 && y1> y2){
                              x = x2;
                              y = y2;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              rect.add( new Rectangle(x,y ,x1 , x2 , y1 , y2 , color ));
                        } else if( x1< x2 && y1> y2){
                              x = x1;
                              y = y2;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              rect.add( new Rectangle(x,y ,x1 , x2 , y1 , y2 , color ));
                        } else if( x1> x2 && y2> y1){
                              x = x2;
                              y = y1;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              rect.add( new Rectangle(x,y ,x1 , x2 , y1 , y2 , color ));
                        }
                   } else if( taye == 3){
                         if( x1< x2 && y1< y2){
                              x = x1;
                              y = y1;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              round.add( new Round( x, y, x1, x2, y1, y2, color));
                        } else if( x1> x2 && y1> y2){
                              x = x2;
                              y = y2;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              round.add( new Round( x, y, x1, x2, y1, y2, color));
                        } else if( x1< x2 && y1> y2){
                              x = x1;
                              y = y2;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              round.add( new Round( x, y, x1, x2, y1, y2, color));
                        } else if( x1> x2 && y2> y1){
                              x = x2;
                              y = y1;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                              round.add( new Round( x, y, x1, x2, y1, y2, color));
                        }
                   }
              }
              
               @Override
               public void mousePressed(MouseEvent e) {
                    x1 = e.getX();
                    y1 = e.getY();
              }
              
               @Override
               public void mouseExited(MouseEvent e) {}
              
               @Override
               public void mouseEntered(MouseEvent e) {}
              
               @Override
               public void mouseClicked(MouseEvent e) {}
          });
     }
     //拖动效果,监听器
     public void myMouseMotionListener(){
          addMouseMotionListener( new MouseMotionListener() {
              
               @Override
               public void mouseMoved(MouseEvent e) {
                    // TODO Auto-generated method stub
                   
              }
              
               @Override
               public void mouseDragged(MouseEvent e) {
                    x2 = e.getX();
                    y2 = e.getY();
                    g = DrawPanelDemo. this.getGraphics();
                    if( taye == 1){
                         g.setColor( color);
                         g.drawLine( x1, y1, x2, y2);
                        repaint();
                   } else if( taye == 2){
                         if( x1< x2 && y1< y2){
                              x = x1;
                              y = y1;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        } else if( x1> x2 && y1> y2){
                              x = x2;
                              y = y2;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        } else if( x1< x2 && y1> y2){
                              x = x1;
                              y = y2;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        } else if( x1> x2 && y2> y1){
                              x = x2;
                              y = y1;
                              g.setColor( color);
                              g.drawRect( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        }
                   } else if( taye == 3){
                         if( x1< x2 && y1< y2){
                              x = x1;
                              y = y1;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        } else if( x1> x2 && y1> y2){
                              x = x2;
                              y = y2;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        } else if( x1< x2 && y1> y2){
                              x = x1;
                              y = y2;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        } else if( x1> x2 && y2> y1){
                              x = x2;
                              y = y1;
                              g.setColor( color);
                              g.drawOval( x, y, Math. abs(x2 - x1), Math. abs(y2 - y1));
                             repaint();
                        }
                   }
              }
          });
     }

}

画板类

画图板小程序
画图板小程序

import java.awt.Color;
//直线类
public class Line {
     private int x1;
     private int x2;
     private int y1;
     private int y2;
     Color color;
     public Line( int x1, int x2, int y1, int y2, Color color) {
           super();
           this. x1 = x1;
           this. x2 = x2;
           this. y1 = y1;
           this. y2 = y2;
           this. color = color;
     }
     public int getX1() {
           return x1;
     }
     public void setX1( int x1) {
           this. x1 = x1;
     }
     public int getX2() {
           return x2;
     }
     public void setX2( int x2) {
           this. x2 = x2;
     }
     public int getY1() {
           return y1;
     }
     public void setY1( int y1) {
           this. y1 = y1;
     }
     public int getY2() {
           return y2;
     }
     public void setY2( int y2) {
           this. y2 = y2;
     }
     public Color getColor() {
           return color;
     }
     public void setColor(Color color) {
           this. color = color;
     }
     
}

直线类

画图板小程序
画图板小程序

import java.awt.Color;
//矩形类
public class Rectangle {
     private int x;
     private int y;
     private int x1;
     private int x2;
     private int y1;
     private int y2;
     Color color;
     public Rectangle( int x, int y, int x1, int x2, int y1, int y2, Color color) {
           super();
           this. x = x;
           this. y = y;
           this. x1 = x1;
           this. x2 = x2;
           this. y1 = y1;
           this. y2 = y2;
           this. color = color;
     }
     
     public int getX() {
           return x;
     }

     public void setX(int x) {
           this. x = x;
     }

     public int getY() {
           return y;
     }

     public void setY(int y) {
           this. y = y;
     }

     public int getX1() {
           return x1;
     }
     public void setX1( int x1) {
           this. x1 = x1;
     }
     public int getX2() {
           return x2;
     }
     public void setX2( int x2) {
           this. x2 = x2;
     }
     public int getY1() {
           return y1;
     }
     public void setY1( int y1) {
           this. y1 = y1;
     }
     public int getY2() {
           return y2;
     }
     public void setY2( int y2) {
           this. y2 = y2;
     }
     public Color getColor() {
           return color;
     }
     public void setColor(Color color) {
           this. color = color;
     }
     
}

矩形类

画图板小程序
画图板小程序

import java.awt.Color;
//椭圆类
public class Round {
     private int x;
     private int y;
     private int x1;
     private int x2;
     private int y1;
     private int y2;
     Color color;
     public Round( int x, int y, int x1, int x2, int y1, int y2, Color color) {
           super();
           this. x = x;
           this. y = y;
           this. x1 = x1;
           this. x2 = x2;
           this. y1 = y1;
           this. y2 = y2;
           this. color = color;
     }
     public int getX() {
           return x;
     }
     public void setX(int x) {
           this. x = x;
     }
     public int getY() {
           return y;
     }
     public void setY(int y) {
           this. y = y;
     }
     public int getX1() {
           return x1;
     }
     public void setX1( int x1) {
           this. x1 = x1;
     }
     public int getX2() {
           return x2;
     }
     public void setX2( int x2) {
           this. x2 = x2;
     }
     public int getY1() {
           return y1;
     }
     public void setY1( int y1) {
           this. y1 = y1;
     }
     public int getY2() {
           return y2;
     }
     public void setY2( int y2) {
           this. y2 = y2;
     }
     public Color getColor() {
           return color;
     }
     public void setColor(Color color) {
           this. color = color;
     }
     
}

椭圆类

界面效果:

画图板小程序

转载于:https://www.cnblogs.com/junzhao/p/4852444.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/109309.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 信道和带宽_信道带宽怎么计算

    信道和带宽_信道带宽怎么计算信道和带宽在用cmw500测试不同band下的throughput时,发现module在某几个band注册不上小区。后来经过同事顺滑的演示,得知是因为不同band支持不同的带宽,而我一直设置cmw500的Cellbandwidth=20MHZ,对于那些最大只支持10MHZ的band自然注册不上。关于不同Band支持的带宽可以参考下表(3GPPTS36.101V17.2.0(2021-06))Table5.6.1-1:E-UTRAchannelbandwidth结尾处分享一

  • matlab画图标签,Matlab绘图[通俗易懂]

    matlab画图标签,Matlab绘图[通俗易懂]要使用plot函数来绘制图形,需要执行以下步骤:通过指定要绘制函数的变量x的值的范围来定义x。定义函数,y=f(x)调用plot命令,如下:plot(x,y)以下示例将演示该概念。下面绘制x的值范围是从0到100,使用简单函数y=x,增量值为5。创建脚本文件并键入以下代码-x=[0:5:100];y=x;plot(x,y)执行上面示例代码,得到以下结果-下面再来一个例子来绘制…

  • CDH6.1.0环境搭建 完成后 登录admin账户失败

    CDH6.1.0环境搭建 完成后 登录admin账户失败CDH环境搭建完成,启动cloudera-manager-server成功,下面是启动日志[root@node-1~]#sudosystemctlstartcloudera-scm-server[root@node-1~]#sudotail-f/var/log/cloudera-scm-server/cloudera-scm-server.log2019-02-14…

  • ubuntu20.04清华源_ubuntu20.04更换国内源

    ubuntu20.04清华源_ubuntu20.04更换国内源Ubuntu22.04的稳定版计划于2022年4月21日发布。开发工作已经在紧锣密鼓地进行,它将遵循如下发布时间表:2022年2月24日:功能冻结2022年3月17日:用户界面冻结2022年3月31日:测试版发布2022年4月14日:候选版本2022年4月21日:最终稳定版本Ubuntu22.04仍在积极开发中。您不应该在生产机器或主系统上使用它。如果你想在备用机器或虚拟机上测试它,你可以从Ubuntu的网站下载每日

  • qlogic官网_zabbix和nagios

    qlogic官网_zabbix和nagios引用http://hi.baidu.com/zeorliu/blog/item/be188aca2ce3ab8fc9176858.html2009-06-1716:26http://asmboy001.blog.51cto.com/340398/111496CactiNagiosSquid三个工具的一些区别cacti是

  • spring boot 系列之七:SpringBoot整合Mybatis

    springboot已经很流行,但是它仍需要搭配一款ORM框架来实现数据的CRUD,之前已经分享过JdbcTemplete和JPA的整合,本次分享下Mybatis的整合。对于mybatis的使用,需

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号