Package com.sun.j3d.utils.geometry

Source Code of com.sun.j3d.utils.geometry.Text2D

/*     */ package com.sun.j3d.utils.geometry;
/*     */
/*     */ import java.awt.Color;
/*     */ import java.awt.Font;
/*     */ import java.awt.FontMetrics;
/*     */ import java.awt.Graphics;
/*     */ import java.awt.Toolkit;
/*     */ import java.awt.image.BufferedImage;
/*     */ import java.util.Hashtable;
/*     */ import javax.media.j3d.Appearance;
/*     */ import javax.media.j3d.ImageComponent;
/*     */ import javax.media.j3d.ImageComponent2D;
/*     */ import javax.media.j3d.Material;
/*     */ import javax.media.j3d.QuadArray;
/*     */ import javax.media.j3d.Shape3D;
/*     */ import javax.media.j3d.Texture;
/*     */ import javax.media.j3d.Texture2D;
/*     */ import javax.media.j3d.TransparencyAttributes;
/*     */ import javax.vecmath.Color3f;
/*     */ import javax.vecmath.Color4f;
/*     */
/*     */ public class Text2D extends Shape3D
/*     */ {
/*  72 */   private static Hashtable metricsTable = new Hashtable();
/*  73 */   float rectangleScaleFactor = 0.0039063F;
/*     */
/*  75 */   Color3f color = new Color3f();
/*     */   String fontName;
/*     */   int fontSize;
/*     */   int fontStyle;
/*     */   String text;
/*     */
/*     */   public Text2D(String text, Color3f color, String fontName, int fontSize, int fontStyle)
/*     */   {
/*  97 */     this.color.set(color);
/*  98 */     this.fontName = fontName;
/*  99 */     this.fontSize = fontSize;
/* 100 */     this.fontStyle = fontStyle;
/* 101 */     this.text = text;
/*     */
/* 103 */     updateText2D(text, color, fontName, fontSize, fontStyle);
/*     */   }
/*     */
/*     */   public void setString(String text)
/*     */   {
/* 113 */     this.text = text;
/*     */
/* 115 */     Texture tex = getAppearance().getTexture();
/* 116 */     int width = tex.getWidth();
/* 117 */     int height = tex.getHeight();
/*     */
/* 119 */     ImageComponent imageComponent = setupImage(text, this.color, this.fontName, this.fontSize, this.fontStyle);
/*     */
/* 121 */     if ((imageComponent.getWidth() == width) && (imageComponent.getHeight() == height))
/*     */     {
/* 123 */       tex.setImage(0, imageComponent);
/*     */     } else {
/* 125 */       Texture2D newTex = setupTexture(imageComponent);
/*     */
/* 129 */       newTex.setBoundaryModeS(tex.getBoundaryModeS());
/* 130 */       newTex.setBoundaryModeT(tex.getBoundaryModeT());
/* 131 */       newTex.setMinFilter(tex.getMinFilter());
/* 132 */       newTex.setMagFilter(tex.getMagFilter());
/* 133 */       newTex.setEnable(tex.getEnable());
/* 134 */       newTex.setAnisotropicFilterMode(tex.getAnisotropicFilterMode());
/* 135 */       newTex.setAnisotropicFilterDegree(tex.getAnisotropicFilterDegree());
/* 136 */       int pcount = tex.getFilter4FuncPointsCount();
/* 137 */       if (pcount > 0) {
/* 138 */         float[] weights = new float[pcount];
/* 139 */         tex.getFilter4Func(weights);
/* 140 */         newTex.setFilter4Func(weights);
/*     */       }
/* 142 */       Color4f c = new Color4f();
/* 143 */       tex.getBoundaryColor(c);
/* 144 */       newTex.setBoundaryColor(c);
/* 145 */       newTex.setUserData(tex.getUserData());
/* 146 */       getAppearance().setTexture(newTex);
/*     */     }
/*     */   }
/*     */
/*     */   private void updateText2D(String text, Color3f color, String fontName, int fontSize, int fontStyle)
/*     */   {
/* 152 */     ImageComponent imageComponent = setupImage(text, color, fontName, fontSize, fontStyle);
/*     */
/* 155 */     Texture2D t2d = setupTexture(imageComponent);
/*     */
/* 157 */     QuadArray rect = setupGeometry(imageComponent.getWidth(), imageComponent.getHeight());
/*     */
/* 159 */     setGeometry(rect);
/*     */
/* 161 */     Appearance appearance = setupAppearance(t2d);
/* 162 */     setAppearance(appearance);
/*     */   }
/*     */
/*     */   public void setRectangleScaleFactor(float newScaleFactor)
/*     */   {
/* 173 */     this.rectangleScaleFactor = newScaleFactor;
/* 174 */     updateText2D(this.text, this.color, this.fontName, this.fontSize, this.fontStyle);
/*     */   }
/*     */
/*     */   public float getRectangleScaleFactor()
/*     */   {
/* 184 */     return this.rectangleScaleFactor;
/*     */   }
/*     */
/*     */   private Texture2D setupTexture(ImageComponent imageComponent)
/*     */   {
/* 191 */     Texture2D t2d = new Texture2D(1, 6, imageComponent.getWidth(), imageComponent.getHeight());
/*     */
/* 195 */     t2d.setMinFilter(3);
/* 196 */     t2d.setMagFilter(3);
/* 197 */     t2d.setImage(0, imageComponent);
/* 198 */     t2d.setEnable(true);
/* 199 */     t2d.setCapability(7);
/* 200 */     t2d.setCapability(8);
/* 201 */     t2d.setCapability(0);
/* 202 */     t2d.setCapability(2);
/* 203 */     t2d.setCapability(3);
/* 204 */     t2d.setCapability(6);
/* 205 */     t2d.setCapability(12);
/* 206 */     t2d.setCapability(14);
/* 207 */     return t2d;
/*     */   }
/*     */
/*     */   private ImageComponent setupImage(String text, Color3f color, String fontName, int fontSize, int fontStyle)
/*     */   {
/* 219 */     Toolkit toolkit = Toolkit.getDefaultToolkit();
/* 220 */     Font font = new Font(fontName, fontStyle, fontSize);
/*     */     FontMetrics metrics;
/* 223 */     if ((metrics = (FontMetrics)metricsTable.get(font)) == null) {
/* 224 */       metrics = toolkit.getFontMetrics(font);
/* 225 */       metricsTable.put(font, metrics);
/*     */     }
/* 227 */     int width = metrics.stringWidth(text);
/* 228 */     int descent = metrics.getMaxDescent();
/* 229 */     int ascent = metrics.getMaxAscent();
/* 230 */     int leading = metrics.getLeading();
/* 231 */     int height = descent + ascent;
/*     */
/* 235 */     int pow = 1;
/* 236 */     for (int i = 1; i < 32; i++) {
/* 237 */       pow *= 2;
/* 238 */       if (width <= pow)
/*     */         break;
/*     */     }
/* 241 */     width = Math.max(width, pow);
/* 242 */     pow = 1;
/* 243 */     for (int i = 1; i < 32; i++) {
/* 244 */       pow *= 2;
/* 245 */       if (height <= pow)
/*     */         break;
/*     */     }
/* 248 */     height = Math.max(height, pow);
/*     */
/* 251 */     BufferedImage bImage = new BufferedImage(width, height, 2);
/*     */
/* 253 */     Graphics offscreenGraphics = bImage.createGraphics();
/*     */
/* 256 */     Color myFill = new Color(0.0F, 0.0F, 0.0F, 0.0F);
/* 257 */     offscreenGraphics.setColor(myFill);
/* 258 */     offscreenGraphics.fillRect(0, 0, width, height);
/*     */
/* 261 */     offscreenGraphics.setFont(font);
/* 262 */     Color myTextColor = new Color(color.x, color.y, color.z, 1.0F);
/* 263 */     offscreenGraphics.setColor(myTextColor);
/* 264 */     offscreenGraphics.drawString(text, 0, height - descent);
/*     */
/* 266 */     ImageComponent imageComponent = new ImageComponent2D(2, bImage);
/*     */
/* 270 */     imageComponent.setCapability(0);
/*     */
/* 272 */     return imageComponent;
/*     */   }
/*     */
/*     */   private QuadArray setupGeometry(int width, int height)
/*     */   {
/* 281 */     float zPosition = 0.0F;
/* 282 */     float rectWidth = width * this.rectangleScaleFactor;
/* 283 */     float rectHeight = height * this.rectangleScaleFactor;
/* 284 */     float[] verts1 = { rectWidth, 0.0F, zPosition, rectWidth, rectHeight, zPosition, 0.0F, rectHeight, zPosition, 0.0F, 0.0F, zPosition };
/*     */
/* 290 */     float[] texCoords = { 0.0F, -1.0F, 0.0F, 0.0F, -1.0F, 0.0F, -1.0F, -1.0F };
/*     */
/* 297 */     QuadArray rect = new QuadArray(4, 33);
/*     */
/* 299 */     rect.setCoordinates(0, verts1);
/* 300 */     rect.setTextureCoordinates(0, 0, texCoords);
/*     */
/* 302 */     return rect;
/*     */   }
/*     */
/*     */   private Appearance setupAppearance(Texture2D t2d)
/*     */   {
/* 313 */     TransparencyAttributes transp = new TransparencyAttributes();
/* 314 */     transp.setTransparencyMode(2);
/* 315 */     transp.setTransparency(0.0F);
/* 316 */     Appearance appearance = new Appearance();
/* 317 */     appearance.setTransparencyAttributes(transp);
/* 318 */     appearance.setTexture(t2d);
/*     */
/* 320 */     Material m = new Material();
/* 321 */     m.setLightingEnable(false);
/* 322 */     appearance.setMaterial(m);
/*     */
/* 324 */     return appearance;
/*     */   }
/*     */
/*     */   public String getString()
/*     */   {
/* 333 */     return this.text;
/*     */   }
/*     */
/*     */   public Color3f getColor()
/*     */   {
/* 342 */     return this.color;
/*     */   }
/*     */
/*     */   public String getFontName()
/*     */   {
/* 351 */     return this.fontName;
/*     */   }
/*     */
/*     */   public int getFontSize()
/*     */   {
/* 360 */     return this.fontSize;
/*     */   }
/*     */
/*     */   public int getFontStyle()
/*     */   {
/* 369 */     return this.fontStyle;
/*     */   }
/*     */ }

/* Location:           Z:\System\Library\Java\Extensions\j3dutils.jar
* Qualified Name:     com.sun.j3d.utils.geometry.Text2D
* JD-Core Version:    0.6.2
*/
TOP

Related Classes of com.sun.j3d.utils.geometry.Text2D

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.