/* * $RCSfile$ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision$ * $Date$ * $State$ */ package GuiTest; import com.sun.j3d.utils.behaviors.vp.OrbitBehavior; import com.sun.j3d.utils.universe.SimpleUniverse; import com.sun.j3d.utils.universe.ViewingPlatform; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Font; import java.awt.Panel; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Optional; import java.util.function.BiFunction; import javax.media.j3d.Appearance; import javax.media.j3d.BoundingSphere; import javax.media.j3d.BranchGroup; import javax.media.j3d.Canvas3D; import javax.media.j3d.ColoringAttributes; import javax.media.j3d.J3DGraphics2D; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.media.j3d.ViewPlatform; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.vecmath.Color3f; import javax.vecmath.Point3d; import javax.vecmath.Vector3d; public class MultiLineText3DTest extends JFrame { private final JButton alignAllLinesAtLeft; private final JButton alignAllLinesAtCenter; private final JButton alignAllLinesAtRight; private final JButton setText; private final JButton updateLine; private final BiFunction getTextWidth; private final Canvas3D canvas; private final SimpleUniverse simpleUniverse; private final MultiLineText3D multiLineText3D; private final BranchGroup root; private final OrbitBehavior orbit; public MultiLineText3DTest() { this.getTextWidth = getTextWidth(); this.multiLineText3D = buildMultiLineText3D(); this.alignAllLinesAtLeft = buildButton("align all lines at left", event -> multiLineText3D.alignAtLeft()); this.alignAllLinesAtCenter = buildButton("align all lines at center", event -> multiLineText3D.alignAtCenter()); this.alignAllLinesAtRight = buildButton("align all lines at right", event -> multiLineText3D.alignAtRight()); this.setText = buildButton("set text", event -> promptUserForMultiLineText() .ifPresent(text -> multiLineText3D.setText(text))); this.updateLine = buildButton("update line", updateLine_onClick()); this.canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration()); this.simpleUniverse = new SimpleUniverse(canvas); this.root = new BranchGroup(); this.orbit = buildOrbitBehaviour(); } private OrbitBehavior buildOrbitBehaviour() { return new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL | OrbitBehavior.STOP_ZOOM); } private MultiLineText3D buildMultiLineText3D() { Appearance appearance = new Appearance(); ColoringAttributes ca = new ColoringAttributes(); Font font = new Font("Courier New", Font.PLAIN, 1); Vector3d pos = new Vector3d(0, 0, 0); MultiLineText3D multiLineText3D1 = new MultiLineText3D(font, appearance, pos, getTextWidth); ca.setColor(new Color3f(Color.white)); multiLineText3D1.setText("line0\nline1\nline2"); return multiLineText3D1; } private BiFunction getTextWidth() { return (text, font) -> { J3DGraphics2D g = canvas.getGraphics2D(); g.setFont(font); return g.getFontMetrics().stringWidth(text); }; } private ActionListener updateLine_onClick() { return event -> { Optional text = promptUserForSingleLineText(); Optional index = promptUserForLineIndex(); if(text.isPresent() && index.isPresent()) { multiLineText3D.updateLine(index.get(), text.get()); } }; } private Optional promptUserForLineIndex() { String result = JOptionPane.showInputDialog(this, "Index of line (starts with 0):"); if(result == null || result.isEmpty()) { return Optional.empty(); } int index = toInt(result) .orElseThrow(() -> new IllegalArgumentException( "Unable to parse as Integer: " + result)); return Optional.of(index); } private Optional promptUserForMultiLineText() { return promptUserForText("Text (e.g.: line0\\nline1\\nline2):"); } private Optional promptUserForSingleLineText() { return promptUserForText("Text (e.g.: line0):"); } private Optional promptUserForText(String prompt) { String result = JOptionPane.showInputDialog(this, prompt); return result == null || result.isEmpty() ? Optional.empty() : Optional.of(result.replace("\\n", "\n")); } private Optional toInt(String s) { try { return Optional.ofNullable(Integer.parseInt(s)); } catch(NumberFormatException ex) { } return Optional.empty(); } private JButton buildButton(String label, ActionListener onClick) { JButton button = new JButton(label); button.addActionListener(onClick); return button; } public void populateSceneGraph() { root.addChild(multiLineText3D); } public void init() { initScene(); initFrame(); } private void initScene() { ViewingPlatform viewingPlatform = simpleUniverse.getViewingPlatform(); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); //System.setProperty("sun.awt.noerasebackground", "true"); populateSceneGraph(); viewingPlatform.setNominalViewingTransform(); setInitialCameraPosition(); orbit.setSchedulingBounds(bounds); viewingPlatform.setViewPlatformBehavior(orbit); simpleUniverse.addBranchGraph(root); } private void setInitialCameraPosition() { Transform3D t3d = new Transform3D(); t3d.setTranslation(new Vector3d(0, 0, 10)); ViewPlatform viewPlatform = canvas.getView().getViewPlatform(); TransformGroup camera = (TransformGroup)viewPlatform.getParent(); camera.setTransform(t3d); } private void initFrame() { setPreferredSize(new Dimension(800, 800)); setLayout(new BorderLayout()); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); addWindowListener(onWindowClosing()); addSouthPanel(); add("Center", canvas); pack(); setVisible(true); } private WindowAdapter onWindowClosing() { return new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { simpleUniverse.cleanup(); dispose(); System.exit(0); } }; } private void addSouthPanel() { Panel southPanel = new Panel(); southPanel.add(alignAllLinesAtLeft); southPanel.add(alignAllLinesAtCenter); southPanel.add(alignAllLinesAtRight); southPanel.add(setText); southPanel.add(updateLine); add("South", southPanel); } /** * @param args the command line arguments */ public static void main(String args[]) { EventQueue.invokeLater(() -> { new MultiLineText3DTest().init(); }); } }