异常处理和画板的保存

前几天超哥给我们讲了异常处理,左哥给我们讲了数据读取和数据保存。我做了用户名检测和画图板的保存。

其中异常的处理我做了用户名的检测,就是给用户抛出异常,让用户自己处理。下面是核心检测代码:

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号就开始华信的寒假集训了,我很期待呢微笑



你可能感兴趣的:(异常处理和画板的保存)