1 package org.jicengine.util; 2 import java.awt.Font ; 3 import java.awt.Color ; 4 import java.awt.Dimension ; 5 import java.awt.Point ; 6 import java.util.*; 7 8 15 public class StringConverter { 16 17 32 public static Color toColor(String colorSpec) throws IllegalArgumentException  33 { 34 try { 35 int red; 36 int green; 37 int blue; 38 int alpha = 255; 39 40 if( colorSpec.indexOf(",") != -1 ){ 41 StringTokenizer tokenizer = new StringTokenizer(colorSpec, ","); 43 red = Integer.parseInt(tokenizer.nextToken()); 44 green = Integer.parseInt(tokenizer.nextToken()); 45 blue = Integer.parseInt(tokenizer.nextToken()); 46 if( tokenizer.hasMoreTokens()){ 47 alpha = Integer.parseInt(tokenizer.nextToken()); 48 } 49 } 50 else { 51 red = Integer.parseInt(colorSpec.substring(0,2),16); 53 green = Integer.parseInt(colorSpec.substring(2,4),16); 54 blue = Integer.parseInt(colorSpec.substring(4,6),16); 55 if( colorSpec.length() == 8 ){ 56 alpha = Integer.parseInt(colorSpec.substring(6),16); 57 } 58 } 59 return new Color (red, green, blue, alpha); 60 } catch (Exception e){ 61 throw new IllegalArgumentException ("Failed to parse '" + colorSpec + "' to a java.awt.Color : " + e); 62 } 63 } 64 65 73 public static Dimension toDimension(String spec) throws IllegalArgumentException  74 { 75 try { 76 int separatorIndex = spec.indexOf("x"); 77 String width = spec.substring(0, separatorIndex).trim(); 78 String height = spec.substring(separatorIndex + 1).trim(); 79 return new java.awt.Dimension (Integer.parseInt(width), Integer.parseInt(height)); 80 } catch (RuntimeException e){ 81 throw new IllegalArgumentException ("Can't parse '" + spec + "' to java.awt.Dimension. Expected format 'width x height'."); 82 } 83 } 84 85 93 public static Point toPoint(String spec) throws IllegalArgumentException  94 { 95 try { 96 int separatorIndex = spec.indexOf(","); 97 String x = spec.substring(spec.indexOf("(") + 1, separatorIndex).trim(); 98 String y = spec.substring(separatorIndex + 1, spec.indexOf(")")).trim(); 99 return new java.awt.Point (Integer.parseInt(x), Integer.parseInt(y)); 100 } catch (Exception e){ 101 throw new IllegalArgumentException ("Can't parse '" + spec + "' to java.awt.Point. Expected format '(x,y)'."); 102 } 103 } 104 105 115 public static Locale toLocale(String spec) throws IllegalArgumentException  116 { 117 String [] params = spec.split("_"); 118 119 if( params.length == 2){ 120 return new java.util.Locale (params[0], params[1]); 121 } 122 else if( params.length == 3){ 123 return new java.util.Locale (params[0], params[1], params[2]); 124 } 125 else { 126 throw new IllegalArgumentException ("Can't parse '" + spec + "' to java.util.Locale. Expected format 'lang_country' or 'lang_country_variant'"); 127 } 128 } 129 } 130
| Popular Tags
|