1 36 37 40 41 package java2d; 42 43 import static java.awt.Color .*; 44 import java.awt.*; 45 import java.awt.geom.Ellipse2D ; 46 import java.awt.image.BufferedImage ; 47 import java.awt.event.MouseListener ; 48 import java.awt.event.MouseEvent ; 49 import java.awt.font.TextLayout ; 50 import java.awt.font.FontRenderContext ; 51 import javax.swing.JPanel ; 52 import javax.swing.border.TitledBorder ; 53 import javax.swing.border.EtchedBorder ; 54 import java.awt.event.WindowEvent ; 55 import java.awt.event.WindowAdapter ; 56 57 58 62 public class TextureChooser extends JPanel { 63 64 static public Object texture = getGeomTexture(); 65 public int num; 66 67 public TextureChooser(int num) { 68 this.num = num; 69 setLayout(new GridLayout(0,2,5,5)); 70 setBorder(new TitledBorder (new EtchedBorder (), "Texture Chooser")); 71 72 add(new Surface( getGeomTexture(), this, 0)); 73 add(new Surface( getImageTexture(), this, 1)); 74 add(new Surface( getTextTexture(), this, 2)); 75 add(new Surface(getGradientPaint(), this, 3)); 76 } 77 78 79 static public TexturePaint getGeomTexture() { 80 BufferedImage bi = new BufferedImage (5, 5, BufferedImage.TYPE_INT_RGB); 81 Graphics2D tG2 = bi.createGraphics(); 82 tG2.setBackground(WHITE); 83 tG2.clearRect(0,0,5,5); 84 tG2.setColor(new Color (211,211,211,200)); 85 tG2.fill(new Ellipse2D.Float (0,0,5,5)); 86 Rectangle r = new Rectangle(0,0,5,5); 87 return new TexturePaint(bi,r); 88 } 89 90 public TexturePaint getImageTexture() { 91 Image img = DemoImages.getImage("java-logo.gif", this); 92 int iw = img.getWidth( this); 93 int ih = img.getHeight(this); 94 BufferedImage bi = new BufferedImage (iw, ih, BufferedImage.TYPE_INT_RGB); 95 Graphics2D tG2 = bi.createGraphics(); 96 tG2.drawImage(img, 0, 0, this); 97 Rectangle r = new Rectangle(0,0,iw,ih); 98 return new TexturePaint(bi,r); 99 } 100 101 102 public TexturePaint getTextTexture() { 103 Font f = new Font("Times New Roman", Font.BOLD, 10); 104 TextLayout tl = new TextLayout ("Java2D", f, new FontRenderContext (null, false, false)); 105 int sw = (int) tl.getBounds().getWidth(); 106 int sh = (int) (tl.getAscent()+tl.getDescent()); 107 BufferedImage bi = new BufferedImage (sw, sh, BufferedImage.TYPE_INT_RGB); 108 Graphics2D tG2 = bi.createGraphics(); 109 tG2.setBackground(WHITE); 110 tG2.clearRect(0,0,sw,sh); 111 tG2.setColor(LIGHT_GRAY); 112 tl.draw(tG2, 0, (float) tl.getAscent()); 113 Rectangle r = new Rectangle(0,0,sw,sh); 114 return new TexturePaint(bi,r); 115 } 116 117 118 public GradientPaint getGradientPaint() { 119 return new GradientPaint(0,0,WHITE,80,0,GREEN); 120 } 121 122 public class Surface extends JPanel implements MouseListener { 123 124 public boolean clickedFrame; 125 private int num; 126 private TextureChooser tc; 127 private boolean enterExitFrame = false; 128 private Object t; 129 130 public Surface(Object t, TextureChooser tc, int num) { 131 setBackground(WHITE); 132 this.t = t; 133 this.tc = tc; 134 this.clickedFrame = (num == tc.num); 135 this.num = num; 136 if (num == tc.num) 137 tc.texture = t; 138 addMouseListener(this); 139 } 140 141 public void paintComponent(Graphics g) { 142 super.paintComponent(g); 143 Graphics2D g2 = (Graphics2D) g; 144 int w = getSize().width; 145 int h = getSize().height; 146 if (t instanceof TexturePaint) 147 g2.setPaint((TexturePaint) t); 148 else { 149 g2.setPaint((GradientPaint) t); 150 } 151 g2.fill(new Rectangle(0,0,w,h)); 152 if (clickedFrame || enterExitFrame) { 153 g2.setColor(GRAY); 154 BasicStroke bs = new BasicStroke(3, BasicStroke.CAP_BUTT, 155 BasicStroke.JOIN_MITER); 156 g2.setStroke(bs); 157 g2.drawRect(0,0,w-1,h-1); 158 tc.num = num; 159 } 160 } 161 162 public void mouseClicked(MouseEvent e) { 163 tc.texture = t; 164 clickedFrame = true; 165 166 for (Component comp : tc.getComponents()) { 167 if (comp instanceof Surface) { 168 Surface surf = (Surface) comp; 169 if (!surf.equals(this) && surf.clickedFrame) { 170 surf.clickedFrame = false; 171 surf.repaint(); 172 } 173 } 174 } 175 176 if (Java2Demo.controls.textureCB.isSelected()) { 178 Java2Demo.controls.textureCB.doClick(); 179 Java2Demo.controls.textureCB.doClick(); 180 } 181 } 182 183 public void mousePressed(MouseEvent e) { 184 } 185 186 public void mouseReleased(MouseEvent e) { 187 } 188 189 public void mouseEntered(MouseEvent e) { 190 enterExitFrame = true; 191 repaint(); 192 } 193 194 public void mouseExited(MouseEvent e) { 195 enterExitFrame = false; 196 repaint(); 197 } 198 199 public Dimension getMinimumSize() { 200 return getPreferredSize(); 201 } 202 203 public Dimension getMaximumSize() { 204 return getPreferredSize(); 205 } 206 207 public Dimension getPreferredSize() { 208 return new Dimension(30,30); 209 } 210 211 } 212 213 public static void main(String s[]) { 214 Frame f = new Frame("Java2D Demo - TextureChooser"); 215 f.addWindowListener(new WindowAdapter () { 216 public void windowClosing(WindowEvent e) {System.exit(0);} 217 }); 218 f.add("Center", new TextureChooser(0)); 219 f.pack(); 220 f.setSize(new Dimension(400,400)); 221 f.setVisible(true); 222 } 223 } 224 | Popular Tags |