画图工具2.0
在上篇文章中,我们已经对简易画图工具有了一个初步了解,下面我们要对一些具体细节进行完善并加上一些新的功能,我们直接来看升级点:
1.界面类加上颜色按钮
Color[] colors = {Color.BLACK, Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.PINK, Color.ORANGE, Color.CYAN, Color.MAGENTA}; for(int i = 0 ; i<colors.length;i++){ JButton btn = new JButton("C"); btn.setBackground(colors[i]); jf.add(btn); btn.addActionListener(dl); }2.监听器类颜色实现
color为全局变量,并定义为黑色
Color color = Color.black;JButton btn = (JButton)e.getSource(); Color bg = btn.getBackground(); color = bg; g.setColor(color); //把画笔颜色变为之前的黑色我们来理解一下getSource:
getSource() = 找出 “谁被点了”
就是:这次点击,到底是哪个按钮触发的
再来注意:
getSource()返回的是 Object- Java 不知道它是按钮,只知道它是一个东西
- 必须强转成 JButton,才能使用按钮独有的方法
3.增加Math.min与Math.abs方法并简化图形的处理
if (type.equals("矩形")) { // 矩形左上角的坐标 宽度 高度 g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1)); } else if (type.equals("圆形")) { // 外切矩形的左上角坐标 宽度 高度 g.drawOval(x1, y1,Math.abs( x2 - x1),Math.abs( y2 - y1)); } else if (type.equals("实心矩形")) { g.fillRect(x1, y1, Math.abs(x2 - x1),Math.abs( y2 - y1)); } else if (type.equals("实心圆形")) { g.fillOval(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));}这样无论是从上往下还是从下往上都可实现画图
4.增加橡皮擦功能
if (type.equals("橡皮擦")) { g.setColor(new Color(238, 238, 238)); g.fillOval(x1, y1, 50, 50); g.setColor(color);}5.用渐变来画出球体
if (type.equals("球形")) { int x = Math.min(x1, x2); int y = Math.min(y1, y2); int w = Math.abs(x2 - x1); int h = Math.abs(y2 - y1); for (int i = 0; i < w; i++) { int cn = i; if(cn>255){ cn=255; } Color c = new Color(cn,cn/2,cn);// 0-255 g.setColor(c); g.fillOval(x+i/2,y+i/2,w-i,h-i);}6.用多边形Polygon类来进行顺时针连线
Polygon pyg = new Polygon(); pyg.addPoint(p1.x, p1.y); pyg.addPoint(p2.x, p2.y); pyg.addPoint(p3.x, p3.y); pyg.addPoint(p4.x, p4.y); Polygon pyg2 = new Polygon(); pyg2.addPoint(p1.x, p1.y); pyg2.addPoint(p5.x, p5.y); pyg2.addPoint(p6.x, p6.y); pyg2.addPoint(p2.x, p2.y); Polygon pyg3 = new Polygon(); pyg3.addPoint(p2.x, p2.y); pyg3.addPoint(p6.x, p6.y); pyg3.addPoint(p7.x, p7.y); pyg3.addPoint(p3.x, p3.y);7.用渐变色使图形变得立体,并调用Polygon类里的方法
Color c1 = new Color(50,50,50); g.setColor(c1); g.fillPolygon(pyg); Color c2 = new Color(100,100,100); g.setColor(c2); g.fillPolygon(pyg2); Color c3 = new Color(150,150,150); g.setColor(c3); g.fillPolygon(pyg3);根据6与7,我们就可以自己写出一个画3D长方体的Draw3Dbox方法
public void Draw3DBox(int x1, int y1, int w ,int h ,int dx, int dy){ Point p1 = new Point(x1, y1); Point p2 = new Point(x1 + w, y1); Point p3 = new Point(x1 + w, y1 + h); Point p4 = new Point(x1, y1 + h); Point p5 = new Point(p1.x + dx, p1.y - dy); Point p6 = new Point(p5.x + w, p5.y); Point p7 = new Point(p6.x, p6.y + h); Point p8 = new Point(p5.x, p5.y + h); //多边形类 Polygon pyg = new Polygon(); pyg.addPoint(p1.x, p1.y); pyg.addPoint(p2.x, p2.y); pyg.addPoint(p3.x, p3.y); pyg.addPoint(p4.x, p4.y); Polygon pyg2 = new Polygon(); pyg2.addPoint(p1.x, p1.y); pyg2.addPoint(p5.x, p5.y); pyg2.addPoint(p6.x, p6.y); pyg2.addPoint(p2.x, p2.y); Polygon pyg3 = new Polygon(); pyg3.addPoint(p2.x, p2.y); pyg3.addPoint(p6.x, p6.y); pyg3.addPoint(p7.x, p7.y); pyg3.addPoint(p3.x, p3.y); //加颜色更立体 Color c1 = new Color(50,50,50); g.setColor(c1); g.fillPolygon(pyg); Color c2 = new Color(100,100,100); g.setColor(c2); g.fillPolygon(pyg2); Color c3 = new Color(150,150,150); g.setColor(c3); g.fillPolygon(pyg3); }8.用双层for循环与二维数组实现砖块画
首先根据图形具体形状用0和1进行简单模拟
int[][] arr ={ {1,0,0,0,1,1,1,0,0,0,1}, {1,0,0,0,1,1,1,0,0,0,1}, {1,0,0,0,1,1,1,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0,1,0,0}, {0,0,1,0,0,0,0,0,1,0,0}, {0,0,0,0,0,0,0,0,0,0,0} };然后双层for循环加上判断并调用draw3Dbox方法
int len = arr.length; int len2 = arr[0].length; for (int i = 0;i<len;i++){ for (int j = 0;j<len2;j++){ if(arr[i][j]==0) { Draw3DBox(x1 + j * (w + 1), y1 - i * (h + 1), w, h, dx, dy); } } }我们再来看一些细节,对x与y加偏移量时,要注意w与h后加1或其他数字来控制间距,否则会出现重叠而导致看不到我们画了多个长方体,只能看到最后的一个大的长方体,减低立体效果。
那么为什么会出现重叠呢?因为我们在画长方体时,加了偏移量dx,dy,它不像矩形一样严丝合缝对接,而是会上翘或右翘一部分。
若想完全避免看到每一个完整的小长方体,可以进行如下代码:
Draw3DBox(x1 + j * (w + dx), y1 - i * (h + dy), w, h, dx, dy);我们来看一下画图效果:
最后把所有功能汇编在一起我们来看一下完整代码:
界面类:
import javax.swing.*; import java.awt.*; public class DrawUI { DrawListener dl = new DrawListener(); public void showUI() { JFrame jf = new JFrame(); jf.setTitle("画图板V2.0"); jf.setSize(800, 600); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FlowLayout flow = new FlowLayout(); jf.setLayout(flow); String[] btnTexts = {"直线", "矩形", "圆形", "实心矩形", "实心圆形", "正圆", "等腰三角形", "长方体", "橡皮擦", "球形","砖块画"}; for (int i = 0; i < btnTexts.length; i++) { JButton btn = new JButton(btnTexts[i]); jf.add(btn); btn.addActionListener(dl); } Color[] colors = {Color.BLACK, Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.PINK, Color.ORANGE, Color.CYAN, Color.MAGENTA}; for (int i = 0; i < colors.length; i++) { JButton btn = new JButton("C"); btn.setBackground(colors[i]); jf.add(btn); btn.addActionListener(dl); } jf.setVisible(true); jf.addMouseListener(dl); Graphics g = jf.getGraphics(); dl.g = g; } public static void main(String[] args) { DrawUI ui = new DrawUI(); ui.showUI(); } }监听器类:
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class DrawListener implements MouseListener, ActionListener { int x1, y1, x2, y2; Graphics g; String type = ""; Color color = Color.black; public void Draw3DBox(int x1, int y1, int w ,int h ,int dx, int dy){ Point p1 = new Point(x1, y1); Point p2 = new Point(x1 + w, y1); Point p3 = new Point(x1 + w, y1 + h); Point p4 = new Point(x1, y1 + h); Point p5 = new Point(p1.x + dx, p1.y - dy); Point p6 = new Point(p5.x + w, p5.y); Point p7 = new Point(p6.x, p6.y + h); Point p8 = new Point(p5.x, p5.y + h); //多边形类 Polygon pyg = new Polygon(); pyg.addPoint(p1.x, p1.y); pyg.addPoint(p2.x, p2.y); pyg.addPoint(p3.x, p3.y); pyg.addPoint(p4.x, p4.y); Polygon pyg2 = new Polygon(); pyg2.addPoint(p1.x, p1.y); pyg2.addPoint(p5.x, p5.y); pyg2.addPoint(p6.x, p6.y); pyg2.addPoint(p2.x, p2.y); Polygon pyg3 = new Polygon(); pyg3.addPoint(p2.x, p2.y); pyg3.addPoint(p6.x, p6.y); pyg3.addPoint(p7.x, p7.y); pyg3.addPoint(p3.x, p3.y); //加颜色更立体 Color c1 = new Color(50,50,50); g.setColor(c1); g.fillPolygon(pyg); Color c2 = new Color(100,100,100); g.setColor(c2); g.fillPolygon(pyg2); Color c3 = new Color(150,150,150); g.setColor(c3); g.fillPolygon(pyg3); } @Override public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); System.out.println(ac); if (ac.equals("C")) { JButton btn = (JButton) e.getSource(); Color bg = btn.getBackground(); color = bg; g.setColor(color); } else { type = ac; } } public void mouseClicked(MouseEvent e) { System.out.println("点击"); } public void mousePressed(MouseEvent e) { System.out.println("按下"); x1 = e.getX(); y1 = e.getY(); } public void mouseReleased(MouseEvent e) { System.out.println("松开"); x2 = e.getX(); y2 = e.getY(); int x = Math.min(x1, x2); int y = Math.min(y1, y2); int w = Math.abs(x2 - x1); int h = Math.abs(y2 - y1); if (type.equals("直线")) { g.drawLine(x1, y1, x2, y2); } else if (type.equals("矩形")) { g.drawRect(x, y, w, h); } else if (type.equals("圆形")) { g.drawOval(x, y, w, h); } else if (type.equals("实心矩形")) { g.fillRect(x, y, w, h); } else if (type.equals("实心圆形")) { g.fillOval(x, y, w, h); } else if (type.equals("橡皮擦")) { g.setColor(new Color(238, 238, 238)); g.fillOval(x1, y1, 50, 50); g.setColor(color); } else if (type.equals("长方体")) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if(i>6&&j>3&&j<6){ break; } int dx = w / 3; int dy = h / 2; x += i * w; y += j * h; g.drawRect(x, y, w, h); g.drawRect(x + dx, y - dy, w, h); g.drawLine(x, y, x + dx, y - dy); g.drawLine(x + w, y, x + w + dx, y - dy); g.drawLine(x, y + h, x + dx, y + h - dy); g.drawLine(x + w, y + h, x + w + dx, y + h - dy); } } } else if (type.equals("球形")) { for (int i = 0; i < w; i++) { int cn = i; if(cn>255){ cn=255; } Color c = new Color(cn,cn/2,cn); g.setColor(c); g.fillOval(x+i/2, y+i/2, w-i, h-i); } } else if (type.equals("砖块画")) { int dx=w/3; int dy=h/2; int[][] arr ={ {1,0,0,0,1,1,1,0,0,0,1}, {1,0,0,0,1,1,1,0,0,0,1}, {1,0,0,0,1,1,1,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0,1,0,0}, {0,0,1,0,0,0,0,0,1,0,0}, {0,0,0,0,0,0,0,0,0,0,0} }; int len = arr.length; int len2 = arr[0].length; for (int i = 0;i<len;i++){ for (int j = 0;j<len2;j++){ if(arr[i][j]==0) { Draw3DBox(x1 + j * (w + 1), y1 - i * (h + 1), w, h, dx, dy); } } } } } public void mouseEntered(MouseEvent e) { System.out.println("进入"); } public void mouseExited(MouseEvent e) { System.out.println("离开"); } }