1 36 37 40 41 package java2d.demos.Clipping; 42 43 import java.awt.*; 44 import java.awt.event.*; 45 import java.awt.image.BufferedImage ; 46 import java.awt.geom.Line2D ; 47 import java.awt.geom.AffineTransform ; 48 import java.awt.image.BufferedImage ; 49 import java.awt.font.TextLayout ; 50 import java.awt.font.FontRenderContext ; 51 import javax.swing.*; 52 import java2d.ControlsSurface; 53 import java2d.CustomControls; 54 55 import static java.awt.Color .*; 56 57 58 61 public class Text extends ControlsSurface { 62 63 static Image img; 64 static TexturePaint texture; 65 static { 66 BufferedImage bi = new BufferedImage (5, 5, BufferedImage.TYPE_INT_RGB); 67 Graphics2D big = bi.createGraphics(); 68 big.setBackground(YELLOW); 69 big.clearRect(0,0,5,5); 70 big.setColor(RED); 71 big.fillRect(0,0,3,3); 72 texture = new TexturePaint(bi,new Rectangle(0,0,5,5)); 73 } 74 private String clipType = "Lines"; 75 private DemoControls controls; 76 protected boolean doClip = true; 77 78 79 public Text() { 80 setBackground(WHITE); 81 img = getImage("clouds.jpg"); 82 setControls(new Component[] { new DemoControls(this) }); 83 } 84 85 86 public void render(int w, int h, Graphics2D g2) { 87 88 FontRenderContext frc = g2.getFontRenderContext(); 89 Font f = new Font("sansserif",Font.BOLD,32); 90 String s = new String ("JAVA"); 91 TextLayout tl = new TextLayout (s, f, frc); 92 double sw = tl.getBounds().getWidth(); 93 double sh = tl.getBounds().getHeight(); 94 double sx = (w-40)/sw; 95 double sy = (h-40)/sh; 96 AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy); 97 Shape shape = tl.getOutline(Tx); 98 sw = shape.getBounds().getWidth(); 99 sh = shape.getBounds().getHeight(); 100 Tx = AffineTransform.getTranslateInstance(w/2-sw/2, h/2+sh/2); 101 shape = Tx.createTransformedShape(shape); 102 Rectangle r = shape.getBounds(); 103 104 if (doClip) { 105 g2.clip(shape); 106 } 107 108 if (clipType.equals("Lines")) { 109 g2.setColor(BLACK); 110 g2.fill(r); 111 g2.setColor(YELLOW); 112 g2.setStroke(new BasicStroke(1.5f)); 113 for (int j = r.y; j < r.y + r.height; j=j+3) { 114 Line2D line = new Line2D.Float ( (float) r.x, (float) j, 115 (float) (r.x+r.width), (float) j); 116 g2.draw(line); 117 } 118 } else if (clipType.equals("Image")) { 119 g2.drawImage(img, r.x, r.y, r.width, r.height, null); 120 } else if (clipType.equals("TP")) { 121 g2.setPaint(texture); 122 g2.fill(r); 123 } else if (clipType.equals("GP")) { 124 g2.setPaint(new GradientPaint(0,0,BLUE,w,h,YELLOW)); 125 g2.fill(r); 126 } else if (clipType.equals("Text")) { 127 g2.setColor(BLACK); 128 g2.fill(shape.getBounds()); 129 g2.setColor(CYAN); 130 f = new Font("serif",Font.BOLD,10); 131 tl = new TextLayout ("java", f, frc); 132 sw = tl.getBounds().getWidth(); 133 134 int x = r.x; 135 int y = (int) (r.y + tl.getAscent()); 136 sh = r.y + r.height; 137 while ( y < sh ) { 138 tl.draw(g2, x, y); 139 if ((x += (int) sw) > (r.x+r.width)) { 140 x = r.x; 141 y += (int) tl.getAscent(); 142 } 143 } 144 } 145 g2.setClip(new Rectangle(0, 0, w, h)); 146 147 g2.setColor(GRAY); 148 g2.draw(shape); 149 } 150 151 152 public static void main(String s[]) { 153 createDemoFrame(new Text()); 154 } 155 156 157 static class DemoControls extends CustomControls implements ActionListener { 158 159 Text demo; 160 JToolBar toolbar; 161 162 public DemoControls(Text demo) { 163 super(demo.name); 164 this.demo = demo; 165 add(toolbar = new JToolBar()); 166 toolbar.setFloatable(false); 167 addTool("Clip", true ); 168 addTool("Lines", true ); 169 addTool("Image", false); 170 addTool("TP", false); 171 addTool("GP", false); 172 addTool("Text" , false); 173 } 174 175 public void addTool(String str, boolean state) { 176 JToggleButton b = (JToggleButton) toolbar.add(new JToggleButton(str)); 177 b.setFocusPainted(false); 178 b.setSelected(state); 179 b.addActionListener(this); 180 int width = b.getPreferredSize().width; 181 Dimension prefSize = new Dimension(width, 21); 182 b.setPreferredSize(prefSize); 183 b.setMaximumSize( prefSize); 184 b.setMinimumSize( prefSize); 185 } 186 187 public void actionPerformed(ActionEvent e) { 188 if (e.getSource().equals(toolbar.getComponentAtIndex(0))) { 189 JToggleButton b = (JToggleButton) e.getSource(); 190 demo.doClip = b.isSelected(); 191 } else { 192 for(Component comp : toolbar.getComponents()) { 193 ((JToggleButton) comp).setSelected(false); 194 } 195 JToggleButton b = (JToggleButton) e.getSource(); 196 b.setSelected(true); 197 demo.clipType = b.getText(); 198 } 199 demo.repaint(); 200 } 201 202 public Dimension getPreferredSize() { 203 return new Dimension(200,40); 204 } 205 206 207 public void run() { 208 try { thread.sleep(1111); } catch (Exception e) { return; } 209 Thread me = Thread.currentThread(); 210 while (thread == me) { 211 for (int i = 1; i < toolbar.getComponentCount()-1; i++) { 212 ((AbstractButton) toolbar.getComponentAtIndex(i)).doClick(); 213 try { 214 thread.sleep(4444); 215 } catch (InterruptedException e) { return; } 216 } 217 } 218 thread = null; 219 } 220 } } | Popular Tags |