1 36 37 40 41 package java2d.demos.Paint; 42 43 44 import static java.awt.Color .*; 45 import java.awt.*; 46 import java.awt.geom.Ellipse2D ; 47 import java.awt.geom.GeneralPath ; 48 import java.awt.geom.AffineTransform ; 49 import java.awt.image.BufferedImage ; 50 import java.awt.font.TextLayout ; 51 import java.awt.font.FontRenderContext ; 52 import java2d.Surface; 53 54 55 58 public class Texture extends Surface { 59 60 private static TexturePaint bluedots, greendots, triangles; 61 private static TexturePaint blacklines, gradient; 62 static { 63 BufferedImage bi = new BufferedImage (10, 10, BufferedImage.TYPE_INT_RGB); 64 Graphics2D gi = bi.createGraphics(); 65 gi.setBackground(WHITE); 66 gi.clearRect(0,0,10,10); 67 GeneralPath p1 = new GeneralPath (); 68 p1.moveTo(0,0); 69 p1.lineTo(5,10); 70 p1.lineTo(10,0); 71 p1.closePath(); 72 gi.setColor(LIGHT_GRAY); 73 gi.fill(p1); 74 triangles = new TexturePaint(bi,new Rectangle(0,0,10,10)); 75 76 bi = new BufferedImage (5, 5, BufferedImage.TYPE_INT_RGB); 77 gi = bi.createGraphics(); 78 gi.setColor(BLACK); 79 gi.fillRect(0,0,5,5); 80 gi.setColor(GRAY); 81 gi.fillRect(1,1,4,4); 82 blacklines = new TexturePaint(bi,new Rectangle(0,0,5,5)); 83 84 int w = 30; int h = 30; 85 bi = new BufferedImage (w, h, BufferedImage.TYPE_INT_RGB); 86 gi = bi.createGraphics(); 87 Color oc = WHITE; Color ic = LIGHT_GRAY; 88 gi.setPaint(new GradientPaint(0,0,oc,w*.35f,h*.35f,ic)); 89 gi.fillRect(0, 0, w/2, h/2); 90 gi.setPaint(new GradientPaint(w,0,oc,w*.65f,h*.35f,ic)); 91 gi.fillRect(w/2, 0, w/2, h/2); 92 gi.setPaint(new GradientPaint(0,h,oc,w*.35f,h*.65f,ic)); 93 gi.fillRect(0, h/2, w/2, h/2); 94 gi.setPaint(new GradientPaint(w,h,oc,w*.65f,h*.65f,ic)); 95 gi.fillRect(w/2, h/2, w/2, h/2); 96 gradient = new TexturePaint(bi,new Rectangle(0,0,w,h)); 97 98 bi = new BufferedImage (2,2,BufferedImage.TYPE_INT_RGB); 99 bi.setRGB(0, 0, 0xffffffff); bi.setRGB(1, 0, 0xffffffff); 100 bi.setRGB(0, 1, 0xffffffff); bi.setRGB(1, 1, 0xff0000ff); 101 bluedots = new TexturePaint(bi,new Rectangle(0,0,2,2)); 102 103 bi = new BufferedImage (2,2,BufferedImage.TYPE_INT_RGB); 104 bi.setRGB(0, 0, 0xffffffff); bi.setRGB(1, 0, 0xffffffff); 105 bi.setRGB(0, 1, 0xffffffff); bi.setRGB(1, 1, 0xff00ff00); 106 greendots = new TexturePaint(bi,new Rectangle(0,0,2,2)); 107 } 108 109 110 public Texture() { 111 setBackground(WHITE); 112 } 113 114 115 public void render(int w, int h, Graphics2D g2) { 116 117 Rectangle r = new Rectangle(10,10,w-20,h/2-20); 118 g2.setPaint(gradient); 119 g2.fill(r); 120 g2.setPaint(GREEN); 121 g2.setStroke(new BasicStroke(20)); 122 g2.draw(r); 123 g2.setPaint(blacklines); 124 g2.setStroke(new BasicStroke(15)); 125 g2.draw(r); 126 127 Font f = new Font("Times New Roman", Font.BOLD, w/5); 128 TextLayout tl = new TextLayout ("Texture", f, g2.getFontRenderContext()); 129 int sw = (int) tl.getBounds().getWidth(); 130 int sh = (int) tl.getBounds().getHeight(); 131 Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2, h*.25+sh/2)); 132 g2.setColor(BLACK); 133 g2.setStroke(new BasicStroke(3)); 134 g2.draw(sha); 135 g2.setPaint(greendots); 136 g2.fill(sha); 137 138 r.setLocation(10,h/2+10); 139 g2.setPaint(triangles); 140 g2.fill(r); 141 g2.setPaint(blacklines); 142 g2.setStroke(new BasicStroke(20)); 143 g2.draw(r); 144 g2.setPaint(GREEN); 145 g2.setStroke(new BasicStroke(4)); 146 g2.draw(r); 147 148 f = new Font("serif", Font.BOLD, w/4); 149 tl = new TextLayout ("Paint", f, g2.getFontRenderContext()); 150 sw = (int) tl.getBounds().getWidth(); 151 sh = (int) tl.getBounds().getHeight(); 152 sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2, h*.75+sh/2)); 153 g2.setColor(BLACK); 154 g2.setStroke(new BasicStroke(5)); 155 g2.draw(sha); 156 g2.setPaint(bluedots); 157 g2.fill(sha); 158 } 159 160 161 public static void main(String s[]) { 162 createDemoFrame(new Texture()); 163 } 164 } 165 | Popular Tags |