10 绘制数字
2016-01-25 21:05
316 查看
前言
题目来自于”编程珠玑”问题描述
在生活中, 我们经常看到使用6条线[横线, 竖线] 来表示各个数字对不对, 比如说 : 电子手表上面, 电表上面, 公交车上面等等思路
思路 : 将各个线条进行编码, 对于0-9各个数字 编辑对应的编码, 如果对应的线条存在, 则对应的位为true, 否则 为false, 各个线条的位置 以左上角为基准, 进行计算, 然后 绘制出来这里我对各个线条的编码如下 :
参考代码
[code]/** * file name : Test29ShowNumber.java * created at : 3:57:05 PM Jun 12, 2015 * created by 970655147 */ package com.hx.test05; public class Test29ShowNumber { // 横条的宽, 竖条的高 static int WIDTH = 20; static int HEIGHT = 20; // 利用八条线绘制数字 public static void main(String []args) { Number[] nums = new Number[]{ new Number(3, new Point(100, 100) ), new Number(4, new Point(200, 100) ) }; JFrame frame = new JFrame(); frame.add(new MyPanel(nums) ); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(100, 100, 400, 300); frame.setResizable(false); } // Panel 用于显示num static class MyPanel extends JPanel { Number[] nums; // 初始化 public MyPanel() { super(); } public MyPanel(Number[] nums) { super(); this.nums = nums; } // 重写, 绘制num public void paint(Graphics g) { super.paint(g); g.setColor(Color.white); g.fillRect(0, 0, 400, 300); drawNum(g); } // 绘制num private void drawNum(Graphics g) { g.setColor(Color.green); if(nums != null) { for(Number num : nums) { for(int i=0; i<num.numLength(); i++) { if(num.isNeedDraw(i) ) { Point[] line = num.posOf(i); g.drawLine(line[0].x, line[0].y, line[1].x, line[1].y); } } } } } } // 表示一个[0, 9]的模拟, 从上到下, 从左到右依次为 [0 - 6] // --- //| | // --- //| | // --- static class Number { // 0 - 9, 线的宽高 static boolean[][] oneToNine = new boolean [][] { generateNumModel(new int[] {0, 1, 2, 4, 5, 6 } ), generateNumModel(new int[] {2, 5 } ), generateNumModel(new int[] {0, 2, 3, 4, 6 } ), generateNumModel(new int[] {0, 2, 3, 5, 6 } ), generateNumModel(new int[] {1, 2, 3, 5 } ), generateNumModel(new int[] {0, 1, 3, 5, 6 } ), generateNumModel(new int[] {0, 1, 3, 4, 5, 6 } ), generateNumModel(new int[] {0, 2, 5 } ), generateNumModel(new int[] {0, 1, 2, 3, 4, 5, 6 } ), generateNumModel(new int[] {0, 1, 2, 3, 5, 6 } ) }; // 数据, 可以吧boolean[] 换成int/ byte/ ... [] [这样的话 就只需要, 需要绘制的路径的数据了...] boolean[] num; Point leftUp; // 初始化 public Number(int i, Point leftUp) { num = oneToNine[i]; this.leftUp = leftUp; } // idx所在的路径需要绘制吗 public boolean isNeedDraw(int idx) { return num[idx]; } // 需要绘制的线的个数 public int numLength() { return num.length; } // 根据让data所在的位置为true, 返回一个data的模型 private static boolean[] generateNumModel(int[] data) { boolean[] res = new boolean[8]; for(int i=0; i<data.length; i++) { res[data[i]] = true; } return res; } // 获取指定索引所在直线的位置 // 从上到下, 从左到右依次为 [0 - 6] // --- //| | // --- //| | // --- public Point[] posOf(int idx) { Point[] line = null; switch(idx) { case 0: line = new Point[] {leftUp, new Point(leftUp.x + WIDTH, leftUp.y) }; break; case 1: line = new Point[] {leftUp, new Point(leftUp.x, leftUp.y + HEIGHT) }; break; case 2: line = new Point[] {new Point(leftUp.x + WIDTH, leftUp.y), new Point(leftUp.x + WIDTH, leftUp.y + HEIGHT) }; break; case 3: line = new Point[] {new Point(leftUp.x, leftUp.y + HEIGHT), new Point(leftUp.x + WIDTH, leftUp.y + HEIGHT) }; break; case 4: line = new Point[] {new Point(leftUp.x, leftUp.y + HEIGHT), new Point(leftUp.x, leftUp.y + (HEIGHT << 1) ) }; break; case 5: line = new Point[] {new Point(leftUp.x + WIDTH, leftUp.y + HEIGHT), new Point(leftUp.x + WIDTH, leftUp.y + (HEIGHT << 1) ) }; break; case 6: line = new Point[] {new Point(leftUp.x, leftUp.y + (HEIGHT << 1)), new Point(leftUp.x + WIDTH, leftUp.y + (HEIGHT << 1) ) }; break; default : throw new RuntimeException("error ..."); } return line; } } }
效果截图
总结
自己瞎扯淡, 没事写的, 似乎 没什么技术含量注 : 因为作者的水平有限,必然可能出现一些bug, 所以请大家指出!
相关文章推荐
- MySQL 5.5版本解决中文乱码问题时my.ini内[mysqld]项中不能再写default-character-set=utf8
- kafka c++客户端编译
- 荷兰风车标志
- MySQL的典型八小时问题
- 新浪微博个人详情页
- 23种设计模式之观察者模式
- Amazon的网络服务aws
- URAL 1180. Stone Game (博弈 + 规律)
- 二级列表展示数据库查询
- LaTex 空格问题~~~~~
- python 条件判断与循环
- LeetCode 63. Unique Paths II
- jersey获取各个参数的总结
- componentsJoinedByString和componentsSeparatedByString的使用
- Spring和springmvc父子容器关系
- OkHttp获取request请求进度
- poj 1065 Wooden Sticks【贪心】
- 关于MyCAT字符集的验证
- 韩顺平 javascript教学视频_学习笔记8_js系统函数_js函数调用方式
- myeclipse10 2014(2015),终极破解方案