java小程序 小画板(不加重绘版)

编程入门 行业动态 更新时间:2024-10-28 11:31:33

java小程序    小<a href=https://www.elefans.com/category/jswz/34/1723245.html style=画板(不加重绘版)"/>

java小程序 小画板(不加重绘版)

小画板功能的实现

1点击颜色和图形按钮时,获取按钮上的颜色和图形信息存储起来

2在窗口上按下和松开鼠标时 记录坐标值

3 绘制对应颜色的图形

下面直接码代码吧  

1 画板主类

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;import javax.swing.JButton;
import javax.swing.JFrame;
public class DrawMain extends JFrame {public static void main(String[] args) {DrawMain dm = new DrawMain();dm.initUI();}/*** 自定义初始化界面的方法*/public void initUI() {this.setTitle("画图程序");this.setSize(700, 500);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(3);this.setLayout(new FlowLayout());//2.实例化DrawListener事件处理类的对象,对象名dl。DrawListener dl = new DrawListener(this);// 定义数组,用来存储按钮上要显示的文字信息String[] typeArray = { "直线", "矩形", "圆" ,"3D矩形","长方体","图片"};// 循环遍历typeArray数组,根据数组中的数据创建按钮对象,将按钮对象添加到窗体上for (int i = 0; i < typeArray.length; i++) {JButton button = new JButton(typeArray[i]);this.add(button);//3.给事件源对象按钮添加addActionListener()动作监听方法,指定事件处理类的对象dl。button.addActionListener(dl);}// 定义数组,用来存储按钮上要显示的颜色信息Color[] colorArray = { Color.black,Color.RED,Color.green,new Color(255,255,255) };//循环遍历colorArray数组,根据数组中的数据创建按钮对象,将按钮对象添加到窗体上for (int i = 0; i < colorArray.length; i++) {JButton button = new JButton();button.setBackground(colorArray[i]);button.setPreferredSize(new Dimension(30,30));this.add(button);//3.给事件源对象按钮添加addActionListener()动作监听方法,指定事件处理类的对象dl。button.addActionListener(dl);}this.setVisible(true);//3.给事件源对象窗体添加addMouseListener()鼠标动作监听方法,指定事件处理类的对象dl。this.addMouseListener(dl);}}
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 javax.swing.ImageIcon;
import javax.swing.JButton;public class DrawListener implements ActionListener, MouseListener {private String type = "直线";// 存储图形的属性private Color color = Color.BLACK;// 存储颜色的属性private int x1, y1, x2, y2;// 存储按下释放坐标值的属性private Graphics g;// 声明Graphics的画笔对象属性,你要在哪一个组件上画图形,那画笔就从这个组件上获取private DrawMain dm;// 声明画图窗体对象属性public DrawListener(DrawMain dm) {this.dm = dm;}/*** 重写接口的抽象方法,也是发生事件后的处理方法* * @param e对象中存储事件源对象相关信息和动作信息。*/public void actionPerformed(ActionEvent e) {// 获取事件源对象JButton button = (JButton) e.getSource();// 1.1.判断点击的是否是颜色按钮,如果是则获取按钮上的背景颜色存储起来。if (button.getText().equals("")) {color = button.getBackground();// 获取按钮上的背景颜色System.out.println(color);}// 1.2.判断点击的是否是图形按钮,如果是则获取按钮上的图形存储起来。else {type = button.getText();// 获取按钮上的文字System.out.println(type);}}//测试鼠标点击public void mouseClicked(MouseEvent e) {System.out.println("点击");}//记录鼠标按下时的坐标public void mousePressed(MouseEvent e) {System.out.println("按下");  //测试鼠标按下x1 = e.getX();y1 = e.getY();// 获取窗体上的画笔对象g = dm.getGraphics();// 注意:获取组件上的画笔时,一定要在窗体可见之后获取,否则获取的是null。g.setColor(color);// 设置画笔的颜色}//鼠标松开时记录坐标并画图public void mouseReleased(MouseEvent e) {System.out.println("释放");// 1.4.获取鼠标释放时的坐标值,要存储释放的坐标值x2 = e.getX();y2 = e.getY();// 1.5.使用画笔对象,根据按下和释放的坐标值来绘制图形if(type.equals("直线"))g.drawLine(x1, y1, x2, y2);if(type.equals("矩形"))g.drawRect(x1,y1,x2-x1,y2-y1);if(type.equals("圆"))g.drawOval(x1,y1,x2-x1,y2-y1);if(type.equals("3D矩形")){g.draw3DRect(x1,y1,x2-x1,y2-y1,true);	g.fill3DRect(x1,y1,x2-x1,y2-y1,true);}if(type.equals("长方体")){int a=(x2+y2-x1-y2)/2;g.drawRect(x1, y1, x2-x1,y2-y1);g.drawLine(x1, y1, x1+a, y1-a);g.drawLine(x1+a, y1-a,x2+a, y1-a);g.drawLine(x2, y1, x2+a,y1-a);g.drawLine(x2, y2, x2+a, y2-a);g.drawLine(x2+a,y1-a,x2+a,y2-a);}if(type.equals("图片")){ImageIcon icon5=new ImageIcon("D:\\work\\eclipse\\Qigame\\src\\cx920\\person1.png");g.drawImage(icon5.getImage(),x1,y1,null);}}//测试鼠标进入窗口public void mouseEntered(MouseEvent e) {System.out.println("进入");}//测试鼠标退出窗口public void mouseExited(MouseEvent e) {System.out.println("退出");}
}

下面是程序跑起来的图

 

这个程序中加入了一些测试鼠标监听的一些东东,在小画板中其实只需要监听鼠标的按下和释放。

但是这个程序在你改变窗口的大小或者把窗口最大化或者最小化的过程中,你所画的都会消失,那么如何让它们不消失呢?那就得加上重绘啦!至于为什么所画的东西会消失?什么是重绘?如何加上重绘呢? 

下回再见!

更多推荐

java小程序 小画板(不加重绘版)

本文发布于:2023-07-28 18:12:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:画板   程序   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!