1 16 17 package com.buchuki.ensmer.text; 18 19 import com.buchuki.ensmer.object.Square; 20 import java.awt.*; 21 import javax.media.j3d.*; 22 import javax.vecmath.Vector3f; 23 24 31 public class SquareTextDisplay extends BranchGroup { 32 33 49 public SquareTextDisplay(Color color, Font font, float margin, float minWidth, float minHeight) { 50 text = new TextDisplay(color, font); 51 background = new Square(); 52 textTG = new TransformGroup(); 53 textTG.addChild(text); 54 addChild(textTG); 55 addChild(background); 56 textTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 57 this.minWidth = minWidth; 58 this.minHeight = minHeight; 59 this.margin = margin; 60 setText(""); 61 } 62 63 70 public void setText(TextInput newText) { 71 text.setText(newText); 72 float width = text.getWidth(); 73 float height = text.getHeight(); 74 width = width < minWidth ? minWidth : width; 75 height = height < minHeight ? minHeight : height; 76 Transform3D trans = new Transform3D(); 77 trans.setTranslation(new Vector3f(-0.5f * width, 0.5f * height, 0.001f)); 78 textTG.setTransform(trans); 79 background.setSize(width + margin, height + margin); 80 } 81 82 87 public void setText(String newText) { 88 TextInput ti = new TextInput(); 89 ti.setText(newText); 90 ti.setFocused(false); 91 setText(ti); 92 } 93 94 100 public Appearance getAppearance() { 101 return background.getAppearance(); 102 } 103 104 110 public float getWidth() { 111 float width = text.getWidth(); 112 width = width < minWidth ? minWidth : width; 113 return width + 2*margin; 114 } 115 116 120 public float getHeight() { 121 float height = text.getHeight(); 122 height = height < minHeight ? minHeight : height; 123 return height + 2*margin; 124 } 125 126 129 private TextDisplay text; 130 131 134 private Square background; 135 136 140 private TransformGroup textTG; 141 142 145 private float margin; 146 147 150 private float minWidth; 151 152 155 private float minHeight; 156 157 } 158 | Popular Tags |