基于java实现画图板功能
本文实例为大家分享了java实现画图板功能的具体代码,供大家参考,具体内容如下
一、介绍
这个画图板主要实现的功能是画矩形(矩形使用的是一个函数画图的方法,这样画出来的图形比较有特点)、椭圆、多变形(先画一条直线,鼠标每点击一个地方就会从上一个点连接到点击的点,当鼠标双击时,双击的点会和终点和起点相连)、画线、橡皮以及颜色选择器,效果图如下所示:
二、具体实现
本项目主要使用的是java.swing以及java.awt的画图工具来实现。首先显示窗口的建立,先让主类draw继承javax.swing.JFrame。draw.java的代码如下:
public class draw extends JFrame{ private Shape shape[]= new Shape[100000];//将所画的形状存储在shape数组中 public static void main(String[] args) { draw simpleDraw = new draw(); simpleDraw.showUI();//调用showUI()函数 } public void showUI() { drawlistener drawListener = new drawlistener(); java.awt.FlowLayout flowLayout = new FlowLayout(); JButton jb1=new JButton("矩形");//添加一个叫“矩形”的按钮 JButton jb2=new JButton("椭圆"); JButton jb3=new JButton("多边形"); JButton jb4=new JButton("三角形"); JButton jb5=new JButton("画线"); JButton jb6=new JButton("橡皮"); java.awt.Dimension dimension = new Dimension(100, 30); jb1.setPreferredSize(dimension);//设置按钮的位置 jb2.setPreferredSize(dimension); jb3.setPreferredSize(dimension); jb4.setPreferredSize(dimension); jb5.setPreferredSize(dimension); jb6.setPreferredSize(dimension); this.add(jb1);//在这个窗口上添加按钮 this.add(jb2); this.add(jb3); this.add(jb4); this.add(jb5); this.add(jb6); Color []colors= {Color.BLUE,Color.GRAY,Color.YELLOW,Color.BLACK};//提供四种颜色选择,存储在colors数组中 for(int i=0;i<4;i++) {//新建4个颜色选择的按钮 JButton jButton=new JButton(); jButton.setBackground(colors[i]); jButton.setPreferredSize(new Dimension(30, 30)); this.add(jButton);//在这个窗口上添加次按钮 jButton.addActionListener(drawListener);//设置按钮的位置 } this.setLayout(flowLayout);//设置窗口布局 this.setSize(800, 700);//设置窗口大小 this.setTitle("画板");//设置窗口名称 this.setLocationRelativeTo(null);//设置窗口位置 this.setDefaultCloseOperation(3); this.setVisible(true); this.getContentPane().setBackground(Color.white);//设置窗口背景颜色 this.addMouseMotionListener(drawListener);//窗口添加监听 jb1.addActionListener(drawListener);//按钮添加监听 jb2.addActionListener(drawListener); jb3.addActionListener(drawListener); jb4.addActionListener(drawListener); jb5.addActionListener(drawListener); jb6.addActionListener(drawListener); //----------------- java.awt.Graphics g = this.getGraphics();//在此窗口上获得画笔 drawListener.setGr(g);//将画笔传给监听器drawListener drawListener.setShape(shape);//将数组传给监听器drawListener this.addMouseListener(drawListener);//画布添加监听 } public void paint(Graphics g) {//重绘 super.paint(g); for(int i=0;i<shape.length;i++) {//重绘shape数组中的所有图形 Shape shape1=shape[i]; if(shape1!=null) { shape1.drawShape(g); }else break; } } }
上述代码中showUI()函数是画图板的界面,drawlistener drawListener = new drawlistener();是调用drawlistener.java新建一个drawlistener类。
在项目进行的过程中,将窗口最小化或者改变窗口大小时,我们先前画的东西就全部消失了。这是因为当窗体在屏幕上显示的时候,首先是将窗体对象的数据从内存中取出来放到缓存中,再在屏幕上进行绘制。当窗体发生改变的时候,程序就会重新从内存中获取更新后的数据绘制。**在系统中Jframe的父类中提供有一个paint(Graphics g)的方法来负责将窗口数据在屏幕上绘制出来。**所以我重写了paint(Graphics g)方法,先将画过的图形存储在一个shape数组中,然后在paint(Graphics g)方法中将所有图形重新画出来,代码:
public void paint(Graphics g) {//重绘 super.paint(g); for(int i=0;i<shape.length;i++) {//重绘shape数组中的所有图形 Shape shape1=shape[i]; if(shape1!=null) { shape1.drawShape(g); }else break; } }
drawlistener.java代码如下:
public class drawlistener implements MouseListener,ActionListener,MouseMotionListener{ private int x1,x2,y1,y2,x3,y3,a,b,x4,y4; private Graphics gr; private int flag=1; String name; Shape shapeLarry[]; int index; Color c=Color.BLACK; public void setShape(Shape shape[]) {//传入shape数组 this.shapeLarry=shape; } public void setGr(Graphics graphics) {//传入画笔 gr=graphics; } public void mouseClicked(MouseEvent e) {//重写鼠标点击函数mouseClicked(MouseEvent e) x3=e.getX();//获取鼠标x坐标 y3=e.getY();//获取鼠标y坐标 if("多边形".equals(name)) {//如果点击多边形按钮 if(flag == 2) {//如果是第一次画 //gr.drawLine(x3, y3, x1, y1); gr.drawLine(x3, y3, x2, y2); Shape shape=new Shape(x3, x2, y3, y2, name); shape.setColor(c); shapeLarry[index++]=shape; a=x3; b=y3; flag++; } if(flag==3) {//如果不是第一次画 gr.drawLine(x3, y3, a, b); Shape shape=new Shape(x3, a, y3, b, name); shape.setColor(c); shapeLarry[index++]=shape; a=x3; b=y3; } if(e.getClickCount()==2) {//如果双击,连接起点终点 gr.drawLine(x3, y3, x1, y1); Shape shape=new Shape(x3, x1, y3, y1, name); shape.setColor(c); shapeLarry[index++]=shape; gr.drawLine(x3, y3, a, b); flag-=2; } } if("三角形".equals(name)) {//如果点击三角形按钮 if(flag==2) {//如果不是第一次画,连接两端 gr.drawLine(x3, y3, x1, y1); Shape shape=new Shape(x1, x3, y1, y3, name); shape.setColor(c); shapeLarry[index++]=shape; gr.drawLine(x3, y3, x2, y2); Shape shape2=new Shape(x2, x3, y2, y3, name); shape2.setColor(c); shapeLarry[index++]=shape2; flag--; } } System.out.println("flag="+flag); } public void mousePressed(MouseEvent e) {//重写鼠标持续点击函数mousePressed(MouseEvent e) if(flag == 1) { x1=e.getX(); y1=e.getY(); } } public void mouseReleased(MouseEvent e){//重写鼠标释放函数mouseReleased(MouseEvent e) if(flag==1) { x2=e.getX(); y2=e.getY(); } if("矩形".equals(name)) {//使用函数画图 for(int i=0;i<25500;i++) { gr.setColor(new Color(i/100, (i+12)/100, 12)); if((i+12)/100>=225) { for(int i2=i;i2<30000;i2++) { gr.setColor(new Color(i/100, (i+12)/100, 12)); if((Math.abs(x2-x1)-i2/50)<=0||(Math.abs(y2-y1)-i2/50)<=0) break; gr.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1)-i2/50, Math.abs(y2-y1)-i2/50); } break; } gr.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1)-i/50, Math.abs(y2-y1)-i/50); } Shape shape= new Shape(x1, x2, y1, y2, name); shapeLarry[index++]=shape; //gr.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1)); } if("椭圆".equals(name)) {//当鼠标释放时画椭圆 gr.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1)); Shape shape=new Shape(x1, x2, y1, y2, name); shape.setColor(c); shapeLarry[index++]=shape; } if("多边形".equals(name) && flag==1) {//当鼠标释放时且不是最后一次画时画直线 gr.drawLine(x1, y1, x2, y2); Shape shape=new Shape(x1, x2, y1, y2, name); shape.setColor(c); shapeLarry[index++]=shape; flag++; } if("三角形".equals(name) && flag==1) { gr.drawLine(x1, y1, x2, y2); Shape shape=new Shape(x1, x2, y1, y2, name); shape.setColor(c); shapeLarry[index++]=shape; flag++; } if("橡皮".equals(name)) { Graphics2D graphics2d=(Graphics2D) gr; BasicStroke basicStroke=new BasicStroke(1f); graphics2d.setColor(c); graphics2d.setStroke(basicStroke); } System.out.println("flag="+flag); } public void actionPerformed(ActionEvent e) { if("".equals(e.getActionCommand())) { JButton jb=(JButton)e.getSource(); c=jb.getBackground(); gr.setColor(c); }else if("多边形".equals(e.getActionCommand())==false ||"三角形".equals(e.getActionCommand())==false){ flag=1; name=e.getActionCommand(); }else { name=e.getActionCommand(); } } public void mouseDragged(MouseEvent e) {//重写鼠标拖拽函数mouseDragged(MouseEvent e) x4=e.getX(); y4=e.getY(); if("画线".equals(name)) {//画线主要是下一个点和上一个点连线组成 gr.drawLine(x1, y1,x4, y4); Shape sh=new Shape(x4, y4, name, x1, y1); sh.setColor(c); shapeLarry[index++]=sh; x1=x4; y1=y4; } if("橡皮".equals(name)) { Graphics2D graphics2d=(Graphics2D) gr; BasicStroke basicStroke=new BasicStroke(10f); graphics2d.setColor(Color.WHITE); graphics2d.setStroke(basicStroke); gr.drawLine(x1, y1,x4, y4); Shape she=new Shape(x4, y4, name, x1, y1); she.setColor(Color.white); shapeLarry[index++]=she; x1=x4; y1=y4; } if("矩形".equals(name)) { gr.drawRect(Math.min(x1, x4), Math.min(y1, y4), Math.abs(x4-x1), Math.abs(y4-y1)); gr.setColor(Color.white); gr.drawRect(Math.min(x1, a), Math.min(y1, b), Math.abs(a-x1), Math.abs(b-y1)); gr.setColor(Color.black); a=x4; b=y4; } } }
Shape.java代码如下:
public class Shape { private int x1,x2,y1,y2,x3,y3,x4,y4,a,b; String name; Color c1; public Shape(int x1,int x2,int y1,int y2,String name) { this.x1=x1; this.x2=x2; this.y1=y1; this.y2=y2; this.name=name; } public Shape(int x4,int y4,String name,int x1,int y1) { this.x1=x1; this.y1=y1; this.x4=x4; this.y4=y4; this.name=name; } public void setColor(Color c) { this.c1=c; } public void drawShape(Graphics g){ g.setColor(c1); switch (name) { case "矩形": for(int i=0;i<25500;i++) { g.setColor(new Color(i/100, (i+12)/100, 12)); if((i+12)/100>=225) { for(int i2=i;i2<30000;i2++) { g.setColor(new Color(i/100, (i+12)/100, 12)); if((Math.abs(x2-x1)-i2/50)<=0||(Math.abs(y2-y1)-i2/50)<=0) break; g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1)-i2/50, Math.abs(y2-y1)-i2/50); } break; } g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1)-i/50, Math.abs(y2-y1)-i/50); } break; case "椭圆": Graphics2D graphics2d4=(Graphics2D) g; BasicStroke basicStroke4=new BasicStroke(1f); graphics2d4.setColor(c1); graphics2d4.setStroke(basicStroke4); g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1)); break; case "画线": Graphics2D graphics2d=(Graphics2D) g; BasicStroke basicStroke=new BasicStroke(1f); graphics2d.setColor(c1); graphics2d.setStroke(basicStroke); g.drawLine(x1, y1, x4, y4); break; case "橡皮": Graphics2D g2D=(Graphics2D)g; BasicStroke basicStroke2=new BasicStroke(10f); g2D.setColor(Color.WHITE); g2D.setStroke(basicStroke2); g2D.drawLine(x1, y1,x4, y4); break; case "三角形": Graphics2D graphics2d1=(Graphics2D) g; BasicStroke basicStroke1=new BasicStroke(1f); graphics2d1.setColor(c1); graphics2d1.setStroke(basicStroke1); g.drawLine(x1, y1, x2, y2); //g.drawLine(x3, y3, x1, y1); //g.drawLine(x3, y3, x2, y2); break; case "多边形": Graphics2D graphics2d2=(Graphics2D) g; BasicStroke basicStroke3=new BasicStroke(1f); graphics2d2.setColor(c1); graphics2d2.setStroke(basicStroke3); g.drawLine(x1, y1, x2, y2); break; default: break; } } }
shape类主要是用来存储画过的图形而定义的类型,将图形存储在shape类型的数组中就不会出现所画的图形消失的情况了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。