画图板小程序

画图板小程序

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

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

相关推荐

  • centos7 本地yum源_centos6更换为阿里源

    centos7 本地yum源_centos6更换为阿里源一、centos7配置yum源yum源分为本地yum源和网络yum源1、配置本地yum源步骤一:在centos虚拟机中挂载光盘1.创建挂载点目录[root@localhost~]#mkdir/mnt/cdrom[root@localhost~]#df/mnt/cdrom文件系统1K-块已用可用已用%挂载点/dev/sda33951733677184163179892020%/2.挂载光盘[root@loc

  • 在Ubuntu中安装Pycharm轻松搞定[通俗易懂]

    在Ubuntu中安装Pycharm轻松搞定[通俗易懂]说到Python代码编辑器,那肯定是Pycharm最好用了,当然还有Vscode、Atom也是很不错的选择,下面请跟着我进行Pycharm的安装。下载安装包首先必须访问Jetbrains官方网站下载Linux的安装包Pycharm下载地址本文对应Pycharm版本为pycharm-community-2020.2.2点击Download后下载文件名为pycharm-community-2020.2.2.tar.gz解压安装快捷键Ctrl+Alt+T启动终端进入

  • deep learning with pytorch中文版_pytorch distributed

    deep learning with pytorch中文版_pytorch distributed憨批的语义分割重制版9——Pytorch搭建自己的DeeplabV3+语义分割平台注意事项学习前言什么是DeeplabV3+模型代码下载DeeplabV3+实现思路一、预测部分1、主干网络介绍2、加强特征提取结构3、利用特征获得预测结果二、训练部分1、训练文件详解2、LOSS解析训练自己的DeeplabV3+模型一、数据集的准备二、数据集的处理三、开始网络训练四、训练结果预测注意事项这是重新构建了的DeeplabV3+语义分割网络,主要是文件框架上的构建,还有代码的实现,和之前的语义分割网络相比,更加

  • 水仙花数

    水仙花数水仙花数

  • windows系统如何cmd查看端口被占用、杀进程「建议收藏」

    windows系统如何cmd查看端口被占用、杀进程「建议收藏」首先是启动windows的命令窗口,按键盘上的windows+R,然后在输入框中输入cmd,既可以启动命令窗口 进入windows命令窗口之后,输入命令,输入netstat-ano然后回车,就可以看到系统当前所有的端口使用情况。 通过命令查找某一特定端口,在命令窗口中输入命令中输入netstat-ano|findstr”端口号”,然后回车就可以看到这个端口被哪个应用占用。 查看到对应的进程id之后,就可以通过id查找对应的进程名称,使用命令tasklist|findstr”进程id..

  • spring循环依赖到底怎么解决的_恋爱循环难吗

    spring循环依赖到底怎么解决的_恋爱循环难吗4.AOP中的循环依赖在看自动代理源码的过程中,突然注意到SmartInstantiationAwareBeanPostProcessor接口中的getEarlyBeanReference方法,它是Spring处理循环依赖时返回**未创建完(只实例化未做依赖注入)**Bean的扩展。关于循环依赖可以去Bean的循环依赖一章去详细了解,这里只做简单的说明。有两个单例Bean,A和B,A中引用了B…

发表回复

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

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