1 30 31 package com.jgoodies.forms.util; 32 33 import java.awt.Component ; 34 import java.awt.FontMetrics ; 35 import java.awt.Toolkit ; 36 37 47 abstract public class AbstractUnitConverter implements UnitConverter { 48 49 private static final int DTP_RESOLUTION = 72; 50 51 52 54 61 public int inchAsPixel(double in, Component component) { 62 return inchAsPixel(in, getScreenResolution(component)); 63 } 64 65 73 public int millimeterAsPixel(double mm, Component component) { 74 return millimeterAsPixel(mm, getScreenResolution(component)); 75 } 76 77 85 public int centimeterAsPixel(double cm, Component component) { 86 return centimeterAsPixel(cm, getScreenResolution(component)); 87 } 88 89 97 public int pointAsPixel(int pt, Component component) { 98 return pointAsPixel(pt, getScreenResolution(component)); 99 } 100 101 109 public int dialogUnitXAsPixel(int dluX, Component c) { 110 return dialogUnitXAsPixel(dluX, getDialogBaseUnitsX(c)); 111 } 112 113 121 public int dialogUnitYAsPixel(int dluY, Component c) { 122 return dialogUnitYAsPixel(dluY, getDialogBaseUnitsY(c)); 123 } 124 125 126 128 136 abstract protected double getDialogBaseUnitsX(Component component); 137 138 146 abstract protected double getDialogBaseUnitsY(Component component); 147 148 149 151 158 protected final int inchAsPixel(double in, int dpi) { 159 return (int) Math.round(dpi * in); 160 } 161 162 169 protected final int millimeterAsPixel(double mm, int dpi) { 170 return (int) Math.round(dpi * mm * 10 / 254); 171 } 172 173 180 protected final int centimeterAsPixel(double cm, int dpi) { 181 return (int) Math.round(dpi * cm * 100 / 254); 182 } 183 184 191 protected final int pointAsPixel(int pt, int dpi) { 192 return Math.round(dpi * pt / DTP_RESOLUTION); 193 } 194 195 202 protected int dialogUnitXAsPixel(int dluX, double dialogBaseUnitsX) { 203 return (int) Math.round(dluX * dialogBaseUnitsX / 4); 204 } 205 206 213 protected int dialogUnitYAsPixel(int dluY, double dialogBaseUnitsY) { 214 return (int) Math.round(dluY * dialogBaseUnitsY / 8); 215 } 216 217 218 220 protected double computeAverageCharWidth(FontMetrics metrics, 221 String testString) { 222 int width = metrics.stringWidth(testString); 223 double average = (double) width / testString.length(); 224 return average; 226 } 227 228 235 protected int getScreenResolution(Component c) { 236 if (c == null) 237 return getDefaultScreenResolution(); 238 239 Toolkit toolkit = c.getToolkit(); 240 return toolkit != null 241 ? toolkit.getScreenResolution() 242 : getDefaultScreenResolution(); 243 } 244 245 246 private static int defaultScreenResolution = -1; 247 248 253 protected int getDefaultScreenResolution() { 254 if (defaultScreenResolution == -1) { 255 defaultScreenResolution = 256 Toolkit.getDefaultToolkit().getScreenResolution(); 257 } 258 return defaultScreenResolution; 259 } 260 261 262 263 } | Popular Tags |