画图板小程序

画图板小程序

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

//测试类
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

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 织梦5.7DEDECMS标签大全

    织梦5.7DEDECMS标签大全

    2021年9月19日
    39
  • Java ListNode 链表

    JavaListNode链表基本结构基本初始化添加构造方法初始化范型写法创建与遍历链表插入节点替换节点删除节点补充说明基本结构链表是一种数据结构,由数据和指针构成,JavaListNode链表是一种由Java自定义实现的链表结构。基本初始化classListNode{//类名:Java类就是一种自定义的数据结构intval;//数据:节点数据ListNodenext;//对象:引用下一个节点对象。在Jav

    2022年4月8日
    250
  • netty pooled vs unpooled ByteBuf

    netty pooled vs unpooled ByteBufWhatsthedifferencebetweenPooledvsUnpooledandDirectvsHeapinByteBuf?Like,whatdoespooledmeansincontextofamessagereceived,becauseobjectlikeHttpRequestiscreatedfromBy…

    2025年6月7日
    0
  • HttpURLConnection.setRequestProperty设置请求头「建议收藏」

    HttpURLConnection.setRequestProperty设置请求头「建议收藏」HttpURLConnection.setRequestProperty(Stringkey,Stringvalue); 设置http请求头

    2022年9月10日
    0
  • plsql安装教程

    plsql安装教程先安装Oracle客户端下载地址:再安装plsqldev下载地址:如果需要汉化,可安装汉化补丁包下载地址:配置数据库找到安装路径,如C:\app\xxx\product\11.2.0\client_1\Network\Admin,将此目录下Sample文件夹里的tnsnames.ora文件移到上层Admin目录下。tnsnames.ora里添加数据库配置AUTOSPD1=(…

    2022年5月27日
    34
  • poe交换机的供电方式_普通交换机能与PoE连接吗

    poe交换机的供电方式_普通交换机能与PoE连接吗PoE交换机是指能够通过网线为远端受电终端提供网络供电的交换机,是PoE供电系统中比较常见的供电设备,可是,如果一个交换机没有POE功能,那么可不可以额外加一个poe供电模块经过交换机给ap供电呢?接下来就由飞畅科技的小编来为大家详细介绍下,一起来看看吧!  其实是可以的,只能算利用了百兆网络环境里网线中闲置的4578脚,来传输电源而已。那就是在交换机和AP中间跨接一个POE合路器(又称POE供电器),但前提要看你的AP是否支持接受POE供电。  POE交换机供电方式一  那么如果你的是吸顶

    2022年10月4日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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