异常处理和画板的保存
来源:互联网 发布:北航软件学院分数线 编辑:程序博客网 时间:2024/05/17 07:25
前几天超哥给我们讲了异常处理,左哥给我们讲了数据读取和数据保存。我做了用户名检测和画图板的保存。
其中异常的处理我做了用户名的检测,就是给用户抛出异常,让用户自己处理。下面是核心检测代码:
public void check(String name) throws Exception{char[] s = name.toCharArray() ;if(s[0] >= '0'&&s[0] <='9'){throw new Exception("用户名不能以数字开头");}if(s.length >= 10){throw new Exception("用户名不能超过十位");}for(int i = 1;i<s.length; i++){if(s[i]>='0'&&s[i]<='9'){break;}if(i == s.length -1){throw new Exception("用户名必须含有数字");}}JOptionPane.showMessageDialog(this, "用户名可用");}
jb.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {try {check(text1.getText());} catch (Exception e) {JOptionPane.showMessageDialog(UserTest.this, e.getMessage());}}});
画图板的保存核心代码:
try {FileOutputStream fos = new FileOutputStream(file);DataOutputStream dos = new DataOutputStream(fos);for(int i=0;i<Draw.list.size();i++){Shape shape = Draw.list.get(i);if(shape instanceof Line){dos.writeByte(1);}if(shape instanceof Oval){dos.writeByte(2);}if(shape instanceof Rect){dos.writeByte(3);}dos.writeInt(shape.x1);dos.writeInt(shape.y1);dos.writeInt(shape.x2);dos.writeInt(shape.y2);String s = Color2String(shape.color);dos.writeUTF(s);}其中打开保存文件是:
try {FileInputStream fis = new FileInputStream(file);DataInputStream dis = new DataInputStream(fis);while(dis.available()>0){int type = dis.readByte();if(type==1){int x1 = dis.readInt();int y1 = dis.readInt();int x2 = dis.readInt();int y2 = dis.readInt();String s = dis.readUTF();Color color = String2Color(s);Shape line = new Line(x1, x2, y1, y2, color);Draw.list.add(line);}if(type==2){int x1 = dis.readInt();int y1 = dis.readInt();int x2 = dis.readInt();int y2 = dis.readInt();String s = dis.readUTF();Color color = String2Color(s);Shape oval = new Oval(x1, x2, y1, y2, color);Draw.list.add(oval);}if(type==3){int x1 = dis.readInt();int y1 = dis.readInt();int x2 = dis.readInt();int y2 = dis.readInt();String s = dis.readUTF();Color color = String2Color(s);Shape rect = new Rect(x1, x2, y1, y2, color);Draw.list.add(rect);}}
其中我遇到了如何保存颜色属性的难题,我是想通过保存字符串来保存的,但是代码不会写,于是就在网上摘录下来了。
就是一下2个方法:
public static Color String2Color(String str){int i = Integer.parseInt(str.substring(1),16);return new Color(i);} public static String Color2String(Color color) { String R = Integer.toHexString(color.getRed()); R = R.length()<2?('0'+R):R; String B = Integer.toHexString(color.getBlue()); B = B.length()<2?('0'+B):B; String G = Integer.toHexString(color.getGreen()); G = G.length()<2?('0'+G):G; return '#'+R+B+G; }
还有就是要停课了,因为接近期末了,华信给我1个月的复习时间,让我们有充足的时间去复习考试。到1月18号就开始华信的寒假集训了,我很期待呢
2 0
- 异常处理和画板的保存
- 易语言-画板保存到文件和画板拷贝到画板图片
- iOS 类似于画板的撤回,清空和保存到相册
- Android客户端项目全局异常处理和服务器数据保存
- Android画板开发(四) 添加背景和保存画板内容为图片
- 异常处理和异常的代码优化
- Spring中关于保存数据时重名的处理异常
- 异常的捕获和处理
- 异常的捕获和处理
- 异常的捕获和处理
- 异常的捕获和处理
- 中断和异常的处理
- 中断和异常的处理
- php的错误处理和异常处理
- 异常和异常处理
- 异常和异常处理
- 结构化异常处理和C++的异常处理区别
- Hibernate一对多的简单例子 附带配置后保存异常和JSON转换异常和批量保存异常
- workerman性能测试
- 新下载kettle不能启动问题
- MFC 生成中英文版软件对应的工程属性设置
- ALSA声卡驱动中的DAPM详解之六:精髓所在,牵一发而动全身
- Android异步消息处理机制详解及源码分析
- 异常处理和画板的保存
- java 文件预览之 DocConverter工具类 将office 文件转换为 swf
- JNDI常见配置方式
- python爬虫:批量刷新网页
- ALSA声卡驱动中的DAPM详解之七:dapm事件机制(dapm event)
- 字符串分割(C++)
- android初学之如何去除gridview点击时出现的黄色边框
- Centos下安装samba
- asp.net WebPages 速记4