/** Company: Shout3D LLC Project: Shout3D Core Class: LabelEffect Date: June 7, 2000 Description: An example of a PostRenderEffect that displays a label (C) Copyright Shout3D LLC. - 1997-2001 - All rights reserved */ package custom_nodes; import shout3d.core.*; import shout3d.*; import shout3d.core.awt.*; import java.awt.*; import java.awt.image.*; /** * LabelEffect * * @author Jim Stewartson */ public class LabelEffect extends PostRenderEffect implements FieldObserver{ final public FloatArrayField label_bg_color = new FloatArrayField( this, "label_bg_color", Field.COLOR, new float[]{0,0,0}); final public FloatArrayField label_border_color = new FloatArrayField( this, "label_border_color", Field.COLOR, new float[]{0,1,0}); final public FloatArrayField label_text_color = new FloatArrayField( this, "label_text_color", Field.COLOR, new float[]{1,1,0}); final public StringField label_text_font = new StringField( this, "label_text_font", Field.ANY, "Arial"); final public StringField label_text = new StringField( this, "label_text", Field.ANY, ""); final public IntField label_text_size = new IntField( this, "label_text_size", Field.NON_NEGATIVE_INT, 12); final public StringField label_text_style = new StringField( this, "label_text_style", Field.ANY, "bold"); final public IntField xPosition = new IntField( this, "xPosition", Field.NON_NEGATIVE_INT, 0); final public IntField yPosition = new IntField( this, "yPosition", Field.NON_NEGATIVE_INT, 0); static final String PLAIN = "plain"; static final String BOLD = "bold"; static final String ITALIC = "italic"; static final String BOLDITALIC = "bolditalic"; static final int BG_COLOR = 0; static final int BORDER_COLOR = 1; static final int TEXT_COLOR = 2; static final int TEXT = 3; static final int FONT = 4; static final int SIZE = 5; static final int STYLE = 6; static final int X = 7; static final int Y = 8; /** * Constructs a default LabelEffect node. */ public LabelEffect() { label_bg_color.addFieldObserver(this, new Integer(BG_COLOR)); label_border_color.addFieldObserver(this, new Integer(BORDER_COLOR)); label_text_color.addFieldObserver(this, new Integer(TEXT_COLOR)); label_text.addFieldObserver(this, new Integer(TEXT)); label_text_font.addFieldObserver(this, new Integer(FONT)); label_text_size.addFieldObserver(this, new Integer(SIZE)); label_text_style.addFieldObserver(this, new Integer(STYLE)); xPosition.addFieldObserver(this, new Integer(X)); yPosition.addFieldObserver(this, new Integer(Y)); } public void onFieldChange(Field f, Object userData) { int whichField = ((Integer)userData).intValue(); switch(whichField) { case BG_COLOR: j_label_bg_color = new java.awt.Color(label_bg_color.getValue()[0], label_bg_color.getValue()[1], label_bg_color.getValue()[2]); break; case BORDER_COLOR: j_label_border_color = new java.awt.Color(label_border_color.getValue()[0], label_border_color.getValue()[1], label_border_color.getValue()[2]); break; case TEXT_COLOR: j_label_text_color = new java.awt.Color(label_text_color.getValue()[0], label_text_color.getValue()[1], label_text_color.getValue()[2]); break; case SIZE: case FONT: case STYLE: String style = label_text_style.getValue(); int javaFontStyle; if (style.equalsIgnoreCase(BOLD)) { javaFontStyle = Font.BOLD; } else if (style.equalsIgnoreCase(ITALIC)) { javaFontStyle = Font.ITALIC; } else if (style.equalsIgnoreCase(BOLDITALIC)) { javaFontStyle = Font.BOLD+Font.ITALIC; } else { javaFontStyle = Font.PLAIN; } font = new Font(label_text_font.getValue(), javaFontStyle, label_text_size.getValue()); break; } } java.awt.Color j_label_bg_color = new java.awt.Color(0f, 0f, 0f); java.awt.Color j_label_border_color = new java.awt.Color(0f, 1f, 0f); java.awt.Color j_label_text_color = new java.awt.Color(1f, 1f, 0f); Font font = new Font("Arial", Font.BOLD, 12); public void filter(Graphics g, int surface_pixel_bits[], float z_buffer[], int deviceWidth, int deviceHeight) { drawLabel(g); } public boolean drawLabel(Graphics g) { g.setFont(font); String string = label_text.getValue(); if (string != null){ int x = xPosition.getValue(); int y = yPosition.getValue(); int width = g.getFontMetrics().stringWidth(string); int height = g.getFontMetrics().getHeight(); int descent = g.getFontMetrics().getMaxDescent()/2; g.setColor(j_label_bg_color); g.fillRect(x, y, width+4, height+4); g.setColor(j_label_border_color); g.drawRect(x, y, width+4, height+4); g.setColor(j_label_text_color); g.drawString(string, x+2, y+height-descent); return true; } return false; } }