画图板小程序

画图板小程序

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

//测试类
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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Altium Designer — 精心总结

    Altium Designer — 精心总结以前是使用DXP2004来画图的,后来转行。想来已经有一年半的时间没有画过了。突然转到AD,有些不适应。用了下发觉很多功能确实比DXP要来的强大。花了不少时间和精力,将之前的一些经验技巧,进行整理总结。希望这篇文章,可以让人少走线些弯路。让初学者可以快速入门。好了,言归正传,开始启程!首先下载AD16: AltiumDesigner16.0.6Build282安装教程:Alti

    2022年7月13日
    23
  • 干货丨初学者学Java应该安装什么软件?

    干货丨初学者学Java应该安装什么软件?初学者刚刚入门学习需要用到一些开发工具,初学Java一般从控制台应用程序开发开始的,在cmd下调试,为你的电脑搭建好开发环境,需要在网站上下载JDK,安装完成后调试成功就可以开始写你的J…

    2022年7月8日
    24
  • ioctl函数详解_函数concat的作用

    ioctl函数详解_函数concat的作用ioctl函数的作用特殊的read,write,当你用read,write不能完成某一功能时,就用ioctl我这里说的ioctl函数是在驱动程序里的,因为我不知道还有没有别的场合用到了ioctl,所以就规定了我们讨论的范围。为什么要写篇文章呢,是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑。什么是ioctl。ioctl是设备驱动程序中对设备…

    2022年10月18日
    3
  • Delphi 2007安装问题[通俗易懂]

    Delphi 2007安装问题[通俗易懂]
    安装前提是你已经下载了Delphi2007forWin32的ISO。
      Delphi2007安装程序根据不同的序列号(许可文件)来判断安装版本,一般ISO中自带的许可文件是专业版的。
      企业版和专业版的许可文件下载:delphi2007_slip.zip
      C++Builder2007的企业版许可文件(slipfileforC++Builder2007):cb2007_ent.zip
      新装方法:
      1、下载D2

    2025年6月7日
    3
  • 2022年idea激活码[最新免费获取]

    (2022年idea激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月22日
    191
  • pthreads php 安装全过程(二)

    pthreads php 安装全过程(二)

    2022年2月17日
    73

发表回复

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

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