Java实现的简单画图板示例
程序员文章站
2024-02-25 20:13:39
本文实例讲述了java实现的简单画图板。分享给大家供大家参考,具体如下:
这个画图板是我好久之前做的,之后浙大的同学需要做课设然后就花了一点时间将它改了一下,变得简单些能...
本文实例讲述了java实现的简单画图板。分享给大家供大家参考,具体如下:
这个画图板是我好久之前做的,之后浙大的同学需要做课设然后就花了一点时间将它改了一下,变得简单些能够方便扩充功能,同时学习java基础
先截图一下吧,就可以知道有哪些功能了~
三个分区,上面选择图形,下面选择颜色,立体圆就是一个分形,也先放着不需要的同学可以注释了它
代码很简单,就是jpanel进行分区,得到画笔,同时使用画图的函数就可以做到了
贴代码应该很快就会了~
主类
package awtdemo; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.flowlayout; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; @suppresswarnings("serial") public class drawmain extends jpanel { public static void main(string[] args) { // todo auto-generated method stub drawmain draw = new drawmain(); draw.initui(); } public void initui() { jframe jf = new jframe(); jf.setsize(1000, 780); jf.settitle("简单画板"); jf.setdefaultcloseoperation(3); jf.setlocationrelativeto(null); jf.setlayout(new borderlayout()); // 实例化事件监听类 drawlistener dl = new drawlistener(this); // 实现中间面板 this.setbackground(color.white); jf.add(this, borderlayout.center); // 实现性状面板 jpanel shapepanel = new jpanel(); shapepanel.setbackground(color.black); shapepanel.setlayout(new flowlayout(flowlayout.center)); shapepanel.setbackground(color.gray); ; string[] shape = { "直线", "曲线", "圆", "喷枪", "橡皮擦", "矩形", "椭圆", "圆角矩形", "弧线", "多边形", "图形", "三角形", "立体圆", }; for (int i = 0; i < shape.length; i++) { jbutton button = new jbutton(shape[i]); button.setbackground(color.white); button.addactionlistener(dl); // 添加事件监听机制 shapepanel.add(button); } jf.add(shapepanel, borderlayout.north); // 实现颜色面板 jpanel colorpanel = new jpanel(); colorpanel.setbackground(color.black); colorpanel.setlayout(new flowlayout(flowlayout.center)); colorpanel.setbackground(color.gray); ; color[] color = { color.black, color.blue, color.white, color.gray, color.red, color.cyan, color.green, color.darkgray, color.pink }; for (int i = 0; i < color.length; i++) { jbutton button = new jbutton(); button.addactionlistener(dl); // 添加事件监听机制 button.setpreferredsize(new dimension(30, 30)); button.setbackground(color[i]); colorpanel.add(button); } jf.add(colorpanel, borderlayout.south); jf.setvisible(true); this.addmouselistener(dl); this.addmousemotionlistener(dl); } }
监听辅助类
package awtdemo; import java.awt.basicstroke; import java.awt.color; import java.awt.graphics2d; import java.awt.renderinghints; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.util.random; import javax.swing.jbutton; public class drawlistener extends mouseadapter implements actionlistener { private int x1, y1, x2, y2; private int newx1, newy1, newx2, newy2; private graphics2d g; private drawmain df; private boolean flag = false; string shape = "直线"; color color; private int[] arrx = new int[4]; private int[] arry = new int[4]; private int temp = 0; drawlistener(drawmain d) { df = d; } // 获取形状和颜色 public void actionperformed(actionevent e) { if (e.getactioncommand().equals("")) { jbutton button = (jbutton) e.getsource(); color = button.getbackground(); system.out.println("color = " + color); } else { jbutton button = (jbutton) e.getsource(); shape = button.getactioncommand(); system.out.println("string = " + shape); } } // 实现画笔 public void mousepressed(mouseevent e) { g = (graphics2d) df.getgraphics(); g.setcolor(color); x1 = e.getx(); y1 = e.gety(); } public void mousereleased(mouseevent e) { x2 = e.getx(); y2 = e.gety(); if (shape.equals("直线")) { g.drawline(x1, y1, x2, y2); } else if (shape.equals("弧线")) { g.drawarc(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1), 0, 180); } else if (shape.equals("多边形") && !flag) { g.drawline(x1, y1, x2, y2); newx1 = x1; newy1 = y1; newx2 = x2; newy2 = y2; flag = true; } else if (shape.equals("圆")) { g.drawoval(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1)); } else if (shape.equals("矩形")) { g.drawrect(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1)); } else if (shape.equals("圆角矩形")) { g.drawroundrect(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1), 2, 10); } else if (shape.equals("椭圆")) { g.drawoval(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1)); } } public void mouseclicked(mouseevent e) { if (shape.equals("多边形") && flag) { x2 = e.getx(); y2 = e.gety(); if (e.getclickcount() == 2) { g.drawline(newx1, newy1, newx2, newy2); flag = false; } g.drawline(newx2, newy2, x2, y2); newx2 = x2; newy2 = y2; } else if (shape.equals("图形")) { arrx[temp] = e.getx(); arry[temp] = e.gety(); temp++; if (temp == 4) { int x = arrx[3]; int y = arry[3]; for (int i = 0; i <= 10000; i++) { random ran = new random(); int k = ran.nextint(3); x = (x + arrx[k]) / 2; y = (y + arry[k]) / 2; g.drawline(x, y, x, y); } temp = 0; } } else if (shape.equals("立体圆")) { // double a=-2,b=-2,c=-1.2,d=2; double a = 1.40, b = 1.56, c = 1.40, d = -6.56; double x = 0, xo = 0; double y = 0, yo = 0; color[] col = { color.blue, color.cyan, color.green, color.magenta, color.red, color.yellow }; for (int i = 0; i <= 90000; i++) { random r = new random(); // 增加颜色 int r = r.nextint(col.length); g.setcolor(col[r]); // x=math.sin(a*yo)-math.cos(b*xo); // y=math.sin(c*xo)-math.cos(d*yo); x = d * math.sin(a * xo) - math.sin(b * yo); y = c * math.cos(a * xo) + math.cos(b * yo); int temp_x = (int) (x * 50); int temp_y = (int) (y * 50); g.drawline(temp_x + 500, temp_y + 300, temp_x + 500, temp_y + 300); xo = x; yo = y; } } else if (shape.equals("三角形")) { double a = -2, b = -2, c = -1.2, d = 2; double x = 0, xo = 0; double y = 0, yo = 0; color[] col = { color.blue, color.cyan, color.green, color.magenta, color.red, color.yellow }; for (int i = 0; i <= 90000; i++) { random r = new random(); // 增加颜色 int r = r.nextint(col.length); g.setcolor(col[r]); x = math.sin(a * yo) - math.cos(b * xo); y = math.sin(c * xo) - math.cos(d * yo); int temp_x = (int) (x * 50); int temp_y = (int) (y * 50); g.drawline(temp_x + 500, temp_y + 300, temp_x + 500, temp_y + 300); xo = x; yo = y; } } } public void mousedragged(mouseevent e) { x2 = e.getx(); y2 = e.gety(); if (shape.equals("曲线")) { // g.setstroke(new basicstroke(10)); // g.setrenderinghint(renderinghints.key_antialiasing, // renderinghints.value_antialias_on); g.drawline(x1, y1, x2, y2); x1 = x2; y1 = y2; } else if (shape.equals("橡皮擦")) { g.setstroke(new basicstroke(80)); g.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g.setcolor(color.white); g.drawline(x1, y1, x2, y2); x1 = x2; y1 = y2; } else if (shape.equals("喷枪")) { // g.setstroke(new basicstroke(2)); //不用加粗 // g.setrenderinghint(renderinghints.key_antialiasing, // renderinghints.value_antialias_on); for (int k = 0; k < 20; k++) { random i = new random(); int a = i.nextint(8); int b = i.nextint(10); g.drawline(x2 + a, y2 + b, x2 + a, y2 + b); } } } }
代码量也还是挺小的,因为是简单画板嘛~~
更多关于java相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java字符与字符串操作技巧总结》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
上一篇: Android图片加载缓存框架Glide
下一篇: Java内部类原理、概述与用法实例详解