1 36 37 40 41 package java2d.demos.Composite; 42 43 import java.awt.*; 44 import java.awt.geom.GeneralPath ; 45 import java.awt.image.BufferedImage ; 46 import java.awt.font.TextLayout ; 47 import java.awt.font.FontRenderContext ; 48 import java.awt.font.LineMetrics ; 49 import java2d.AnimatingSurface; 50 51 import static java.awt.AlphaComposite .*; 52 53 54 57 public class ACrules extends AnimatingSurface { 58 59 private static String compNames[] = { 60 "Src", 61 "SrcOver", 62 "SrcIn", 63 "SrcOut", 64 "SrcAtop", 65 "Clear", 66 67 "Dst", 68 "DstOver", 69 "DstIn", 70 "DstOut", 71 "DstAtop", 72 "Xor", 73 }; 74 75 private static AlphaComposite compObjs[] = { 76 Src, SrcOver, SrcIn, SrcOut, SrcAtop, Clear, 77 Dst, DstOver, DstIn, DstOut, DstAtop, Xor, 78 }; 79 80 private static int NUM_RULES = compObjs.length; 81 private static int HALF_NUM_RULES = NUM_RULES / 2; 82 83 private int fadeIndex; 84 private static float fadeValues[][] = { 85 { 1.0f,-0.1f, 0.0f, 1.0f, 0.0f, 1.0f}, 86 { 0.0f, 0.1f, 1.0f, 1.0f,-0.1f, 0.0f}, 87 { 1.0f, 0.0f, 1.0f, 0.0f, 0.1f, 1.0f}, 88 }; 89 private static String fadeNames[] = { 90 "Src => transparent, Dest opaque", 91 "Src => opaque, Dest => transparent", 92 "Src opaque, Dest => opaque", 93 }; 94 private static Font f = new Font("serif", Font.PLAIN, 10); 95 private float srca = fadeValues[fadeIndex][0]; 96 private float dsta = fadeValues[fadeIndex][3]; 97 private String fadeLabel = fadeNames[0]; 98 private BufferedImage statBI, animBI; 99 private int PADLEFT, PADRIGHT, HPAD; 100 private int PADABOVE, PADBELOW, VPAD; 101 private int RECTWIDTH, RECTHEIGHT; 102 private int PADDEDWIDTH, PADDEDHEIGHT; 103 private GeneralPath srcpath = new GeneralPath (); 104 private GeneralPath dstpath = new GeneralPath (); 105 private LineMetrics lm; 106 private BufferedImage dBI, sBI; 107 private GradientPaint gradientDst, gradientSrc; 108 109 110 public ACrules() { 111 setBackground(Color.white); 112 } 113 114 115 public void reset(int w, int h) { 116 setSleepAmount(400); 117 FontRenderContext frc = new FontRenderContext (null, false, false); 118 lm = f.getLineMetrics(compNames[0], frc); 119 120 PADLEFT = (w < 150) ? 10 : 15; 121 PADRIGHT = (w < 150) ? 10 : 15; 122 HPAD = (PADLEFT + PADRIGHT); 123 PADBELOW = (h < 250) ? 1 : 2; 124 PADABOVE = PADBELOW + (int) lm.getHeight(); 125 VPAD = (PADABOVE + PADBELOW); 126 RECTWIDTH = w/4 - HPAD; 127 RECTWIDTH = (RECTWIDTH < 6) ? 6 : RECTWIDTH; 128 RECTHEIGHT = (h-VPAD)/HALF_NUM_RULES - VPAD; 129 RECTHEIGHT = (RECTHEIGHT < 6) ? 6 : RECTHEIGHT; 130 PADDEDWIDTH = (RECTWIDTH + HPAD); 131 PADDEDHEIGHT = (RECTHEIGHT + VPAD); 132 133 srcpath.reset(); 134 srcpath.moveTo(0,0); 135 srcpath.lineTo(RECTWIDTH,0); 136 srcpath.lineTo(0,RECTHEIGHT); 137 srcpath.closePath(); 138 139 dstpath.reset(); 140 dstpath.moveTo(0,0); 141 dstpath.lineTo(RECTWIDTH,RECTHEIGHT); 142 dstpath.lineTo(RECTWIDTH,0); 143 dstpath.closePath(); 144 145 dBI = new BufferedImage (RECTWIDTH, RECTHEIGHT, 146 BufferedImage.TYPE_INT_ARGB); 147 sBI = new BufferedImage (RECTWIDTH, RECTHEIGHT, 148 BufferedImage.TYPE_INT_ARGB); 149 gradientDst = new GradientPaint(0, 0, 150 new Color(1.0f, 0.0f, 0.0f, 1.0f), 151 0, RECTHEIGHT, 152 new Color(1.0f, 0.0f, 0.0f, 0.0f)); 153 gradientSrc = new GradientPaint(0, 0, 154 new Color(0.0f, 0.0f, 1.0f, 1.0f), 155 RECTWIDTH, 0, 156 new Color(0.0f, 0.0f, 1.0f, 0.0f)); 157 statBI = new BufferedImage (w/2, h, BufferedImage.TYPE_INT_RGB); 158 statBI = drawCompBI(statBI, true); 159 animBI = new BufferedImage (w/2, h, BufferedImage.TYPE_INT_RGB); 160 } 161 162 163 public void step(int w, int h) { 164 if (getSleepAmount() == 5000) { 165 setSleepAmount(200); 166 } 167 168 srca = srca + fadeValues[fadeIndex][1]; 169 dsta = dsta + fadeValues[fadeIndex][4]; 170 fadeLabel = fadeNames[fadeIndex]; 171 if (srca < 0 || srca > 1.0 || dsta < 0 || dsta > 1.0) { 172 setSleepAmount(5000); 173 srca = fadeValues[fadeIndex][2]; 174 dsta = fadeValues[fadeIndex][5]; 175 if (fadeIndex++ == fadeValues.length-1) { 176 fadeIndex = 0; 177 } 178 } 179 } 180 181 182 public void render(int w, int h, Graphics2D g2) { 183 184 if (statBI == null || animBI == null) { 185 return; 186 } 187 g2.drawImage(statBI, 0, 0, null); 188 g2.drawImage(drawCompBI(animBI, false), w/2, 0, null); 189 190 g2.setColor(Color.black); 191 FontRenderContext frc = g2.getFontRenderContext(); 192 TextLayout tl = new TextLayout ("AC Rules", g2.getFont(), frc); 193 tl.draw(g2, 15.0f, (float) tl.getBounds().getHeight()+3.0f); 194 195 tl = new TextLayout (fadeLabel, f, frc); 196 float x = (float) (w*0.75-tl.getBounds().getWidth()/2); 197 if ((x + tl.getBounds().getWidth()) > w) { 198 x = (float) (w - tl.getBounds().getWidth()); 199 } 200 tl.draw(g2, x, (float) tl.getBounds().getHeight()+3.0f); 201 } 202 203 204 private BufferedImage drawCompBI(BufferedImage bi, boolean doGradient) 205 { 206 Graphics2D big = bi.createGraphics(); 207 big.setColor(getBackground()); 208 big.fillRect(0, 0, bi.getWidth(), bi.getHeight()); 209 big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias); 210 big.setFont(f); 211 212 Graphics2D gD = dBI.createGraphics(); 213 gD.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias); 214 Graphics2D gS = sBI.createGraphics(); 215 gS.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias); 216 217 int x = 0, y = 0; 218 int yy = (int) lm.getHeight() + VPAD; 219 220 for (int i = 0; i < compNames.length; i++) { 221 y = (i == 0 || i == HALF_NUM_RULES) ? yy : y + PADDEDHEIGHT; 222 x = (i >= HALF_NUM_RULES) ? bi.getWidth()/2+PADLEFT : PADLEFT; 223 big.translate(x, y); 224 225 gD.setComposite(Clear); 226 gD.fillRect(0, 0, RECTWIDTH, RECTHEIGHT); 227 gD.setComposite(Src); 228 if (doGradient) { 229 gD.setPaint(gradientDst); 230 gD.fillRect(0, 0, RECTWIDTH, RECTHEIGHT); 231 } else { 232 gD.setPaint(new Color(1.0f, 0.0f, 0.0f, dsta)); 233 gD.fill(dstpath); 234 } 235 236 gS.setComposite(Clear); 237 gS.fillRect(0, 0, RECTWIDTH, RECTHEIGHT); 238 gS.setComposite(Src); 239 if (doGradient) { 240 gS.setPaint(gradientSrc); 241 gS.fillRect(0, 0, RECTWIDTH, RECTHEIGHT); 242 } else { 243 gS.setPaint(new Color(0.0f, 0.0f, 1.0f, srca)); 244 gS.fill(srcpath); 245 } 246 247 gD.setComposite(compObjs[i]); 248 gD.drawImage(sBI, 0, 0, null); 249 250 big.drawImage(dBI, 0, 0, null); 251 big.setColor(Color.black); 252 big.drawString(compNames[i], 0, -lm.getDescent()); 253 big.drawRect(0, 0, RECTWIDTH, RECTHEIGHT); 254 big.translate(-x, -y); 255 } 256 257 gD.dispose(); 258 gS.dispose(); 259 big.dispose(); 260 261 return bi; 262 } 263 264 265 public static void main(String argv[]) { 266 createDemoFrame(new ACrules()); 267 } 268 } 269 | Popular Tags |