// Java 3D Test Applet // Text3DTest.java // Copyright (c) 1999 ENDO Yasuyuki // mailto:yasuyuki@javaopen.org // http://www.javaopen.org/j3dbook/index.html import java.applet.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.SimpleUniverse; public class Text3DTest extends Applet { private Canvas3D canvas = null; private SimpleUniverse universe = null; private BranchGroup scene = null; private TransformGroup trans = null; private TransformGroup strans = null; private float scale = 0.2f; private Material mat = null; private Font[] fonts = null; private int nfont = 0; private Font font = null; private int style = java.awt.Font.PLAIN; private String text = "Java 3D"; private Text3D text3d = null; private Shape shape = null; private Line2D.Double line2d = new Line2D.Double(0.0, 0.0, 1.0, 0.0); private CubicCurve2D.Double c2d = new CubicCurve2D.Double(0.0, 0.0, 0.2, 0.7, 0.7, 0.8, 1.0, 1.0); private QuadCurve2D.Double q2d = new QuadCurve2D.Double(0.0, 0.0, 0.0, 1.0, 1.0, 1.0); private Color3f fontcolor = new Color3f(1.0f, 0.0f, 0.0f); // フォント色 赤 private TuplePanel fcpanel = new TuplePanel(fontcolor); private Point3f fontposition = new Point3f(0.0f, 0.0f, 0.0f); // フォント位置 原点 private TuplePanel fppanel = new TuplePanel(fontposition); public Text3DTest() { this.setLayout(new BorderLayout()); shape = line2d; final ShapePanel spanel = new ShapePanel(shape); this.add(spanel, BorderLayout.NORTH); Panel outpanel = new Panel(); outpanel.setLayout( new BorderLayout() ); this.add(outpanel, BorderLayout.SOUTH); Panel outswitch = new Panel(); outpanel.add(outswitch, BorderLayout.NORTH); final Panel outcardpanel = new Panel(); final CardLayout outcardlayout = new CardLayout(); outcardpanel.setLayout(outcardlayout); outpanel.add(outcardpanel, BorderLayout.CENTER); Panel fxpanel = new Panel(); fxpanel.setLayout( new BorderLayout() ); outcardpanel.add(fxpanel, "0"); Panel text3dpanel = new Panel(); text3dpanel.setLayout( new GridLayout(3, 1) ); outcardpanel.add(text3dpanel, "1"); CheckboxGroup outg = new CheckboxGroup(); Checkbox fxcheck = new Checkbox("FontExtrusion", outg, true); fxcheck.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { outcardlayout.show(outcardpanel, "0"); } } }); outswitch.add(fxcheck); Checkbox tx3check = new Checkbox("Text3D", outg, false); tx3check.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { outcardlayout.show(outcardpanel, "1"); } } }); outswitch.add(tx3check); Panel fxswitch = new Panel(); fxpanel.add(fxswitch, BorderLayout.NORTH); final Panel fxcardpanel = new Panel(); final CardLayout fxcardlayout = new CardLayout(); fxcardpanel.setLayout(fxcardlayout); fxpanel.add(fxcardpanel, BorderLayout.CENTER); Panel[] fxcpanels = new Panel[3]; for (int i=0; i<3; i++) { fxcpanels[i] = new Panel(); fxcpanels[i].setLayout( new GridLayout(2, 1) ); fxcardpanel.add(fxcpanels[i], Integer.toString(i)); } CheckboxGroup fxg = new CheckboxGroup(); Checkbox lcheck = new Checkbox("Line2D", fxg, true); lcheck.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { shape = line2d; spanel.setShape(shape); fxcardlayout.show(fxcardpanel, "0"); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } } }); fxswitch.add(lcheck); Checkbox ccheck = new Checkbox("CubicCurve2D", fxg, false); ccheck.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { shape = c2d; spanel.setShape(shape); fxcardlayout.show(fxcardpanel, "1"); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } } }); fxswitch.add(ccheck); Checkbox qcheck = new Checkbox("QuadCurve2D", fxg, false); qcheck.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { shape = q2d; spanel.setShape(shape); fxcardlayout.show(fxcardpanel, "2"); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } } }); fxswitch.add(qcheck); Panel l2panel = new Panel(); l2panel.setLayout( new FlowLayout(FlowLayout.LEFT) ); fxcpanels[0].add(l2panel); l2panel.add( new Label("x1") ); TextField lx1field = new TextField("0.0"); lx1field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double lx1 = 0.0; try { lx1 = Double.parseDouble(e.getActionCommand()); Point2D bp = line2d.getP1(); Point2D ep = line2d.getP2(); ((Point2D.Double)bp).x = lx1; line2d.setLine(bp, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); l2panel.add(lx1field); l2panel.add( new Label("y1") ); final TextField ly1field = new TextField("0.0"); ly1field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double ly1 = 0.0; try { ly1 = Double.parseDouble(e.getActionCommand()); Point2D bp = line2d.getP1(); Point2D ep = line2d.getP2(); ((Point2D.Double)bp).y = ly1; line2d.setLine(bp, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); l2panel.add(ly1field); l2panel.add( new Label("x2") ); final TextField lx2field = new TextField("1.0"); lx2field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double lx2 = 0.0; try { lx2 = Double.parseDouble(e.getActionCommand()); Point2D bp = line2d.getP1(); Point2D ep = line2d.getP2(); ((Point2D.Double)ep).x = lx2; line2d.setLine(bp, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); l2panel.add(lx2field); l2panel.add( new Label("y2") ); final TextField ly2field = new TextField("0.0"); ly2field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double ly2 = 0.0; try { ly2 = Double.parseDouble(e.getActionCommand()); Point2D bp = line2d.getP1(); Point2D ep = line2d.getP2(); ((Point2D.Double)ep).y = ly2; line2d.setLine(bp, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); l2panel.add(ly2field); Panel c2upanel = new Panel(); c2upanel.setLayout( new FlowLayout(FlowLayout.LEFT) ); fxcpanels[1].add(c2upanel); Panel c2dpanel = new Panel(); c2dpanel.setLayout( new FlowLayout(FlowLayout.LEFT) ); fxcpanels[1].add(c2dpanel); c2upanel.add( new Label("x1") ); final TextField cx1field = new TextField("0.0"); cx1field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double cx1 = 0.0; try { cx1 = Double.parseDouble(e.getActionCommand()); Point2D bp = c2d.getP1(); Point2D c1 = c2d.getCtrlP1(); Point2D c2 = c2d.getCtrlP2(); Point2D ep = c2d.getP2(); ((Point2D.Double)bp).x = cx1; c2d.setCurve(bp, c1, c2, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); c2upanel.add(cx1field); c2upanel.add( new Label("y1") ); final TextField cy1field = new TextField("0.0"); cy1field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double cy1 = 0.0; try { cy1 = Double.parseDouble(e.getActionCommand()); Point2D bp = c2d.getP1(); Point2D c1 = c2d.getCtrlP1(); Point2D c2 = c2d.getCtrlP2(); Point2D ep = c2d.getP2(); ((Point2D.Double)bp).y = cy1; c2d.setCurve(bp, c1, c2, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); c2upanel.add(cy1field); c2upanel.add( new Label("cx1") ); final TextField ccx1field = new TextField("0.2"); ccx1field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double ccx1 = 0.0; try { ccx1 = Double.parseDouble(e.getActionCommand()); Point2D bp = c2d.getP1(); Point2D c1 = c2d.getCtrlP1(); Point2D c2 = c2d.getCtrlP2(); Point2D ep = c2d.getP2(); ((Point2D.Double)c1).x = ccx1; c2d.setCurve(bp, c1, c2, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); c2upanel.add(ccx1field); c2upanel.add( new Label("cy1") ); final TextField ccy1field = new TextField("0.7"); ccy1field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double ccy1 = 0.0; try { ccy1 = Double.parseDouble(e.getActionCommand()); Point2D bp = c2d.getP1(); Point2D c1 = c2d.getCtrlP1(); Point2D c2 = c2d.getCtrlP2(); Point2D ep = c2d.getP2(); ((Point2D.Double)c1).y = ccy1; c2d.setCurve(bp, c1, c2, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); c2upanel.add(ccy1field); c2dpanel.add( new Label("cx2") ); final TextField ccx2field = new TextField("0.7"); ccx2field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double ccx2 = 0.0; try { ccx2 = Double.parseDouble(e.getActionCommand()); Point2D bp = c2d.getP1(); Point2D c1 = c2d.getCtrlP1(); Point2D c2 = c2d.getCtrlP2(); Point2D ep = c2d.getP2(); ((Point2D.Double)c2).x = ccx2; c2d.setCurve(bp, c1, c2, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); c2dpanel.add(ccx2field); c2dpanel.add( new Label("cy2") ); final TextField ccy2field = new TextField("0.8"); ccy2field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double ccy2 = 0.0; try { ccy2 = Double.parseDouble(e.getActionCommand()); Point2D bp = c2d.getP1(); Point2D c1 = c2d.getCtrlP1(); Point2D c2 = c2d.getCtrlP2(); Point2D ep = c2d.getP2(); ((Point2D.Double)c2).y = ccy2; c2d.setCurve(bp, c1, c2, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); c2dpanel.add(ccy2field); c2dpanel.add( new Label("x2") ); final TextField cx2field = new TextField("1.0"); cx2field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double cx2 = 0.0; try { cx2 = Double.parseDouble(e.getActionCommand()); Point2D bp = c2d.getP1(); Point2D c1 = c2d.getCtrlP1(); Point2D c2 = c2d.getCtrlP2(); Point2D ep = c2d.getP2(); ((Point2D.Double)ep).x = cx2; c2d.setCurve(bp, c1, c2, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); c2dpanel.add(cx2field); c2dpanel.add( new Label("y2") ); final TextField cy2field = new TextField("1.0"); cy2field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double cy2 = 0.0; try { cy2 = Double.parseDouble(e.getActionCommand()); Point2D bp = c2d.getP1(); Point2D c1 = c2d.getCtrlP1(); Point2D c2 = c2d.getCtrlP2(); Point2D ep = c2d.getP2(); ((Point2D.Double)ep).y = cy2; c2d.setCurve(bp, c1, c2, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); c2dpanel.add(cy2field); Panel q2upanel = new Panel(); q2upanel.setLayout( new FlowLayout(FlowLayout.LEFT) ); fxcpanels[2].add(q2upanel); Panel q2dpanel = new Panel(); q2dpanel.setLayout( new FlowLayout(FlowLayout.LEFT) ); fxcpanels[2].add(q2dpanel); q2upanel.add( new Label("x1") ); final TextField q2x1field = new TextField("0.0"); q2x1field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double qx1 = 0.0; try { qx1 = Double.parseDouble(e.getActionCommand()); Point2D bp = q2d.getP1(); Point2D cp = q2d.getCtrlPt(); Point2D ep = q2d.getP2(); ((Point2D.Double)bp).x = qx1; q2d.setCurve(bp, cp, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); q2upanel.add(q2x1field); q2upanel.add( new Label("y1") ); final TextField q2y1field = new TextField("0.0"); q2y1field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double qy1 = 0.0; try { qy1 = Double.parseDouble(e.getActionCommand()); Point2D bp = q2d.getP1(); Point2D cp = q2d.getCtrlPt(); Point2D ep = q2d.getP2(); ((Point2D.Double)bp).y = qy1; q2d.setCurve(bp, cp, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); q2upanel.add(q2y1field); q2upanel.add( new Label("cx") ); final TextField q2cxfield = new TextField("0.0"); q2cxfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double qcx = 0.0; try { qcx = Double.parseDouble(e.getActionCommand()); Point2D bp = q2d.getP1(); Point2D cp = q2d.getCtrlPt(); Point2D ep = q2d.getP2(); ((Point2D.Double)cp).x = qcx; q2d.setCurve(bp, cp, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); q2upanel.add(q2cxfield); q2upanel.add( new Label("cy") ); final TextField q2cyfield = new TextField("1.0"); q2cyfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double qcy = 0.0; try { qcy = Double.parseDouble(e.getActionCommand()); Point2D bp = q2d.getP1(); Point2D cp = q2d.getCtrlPt(); Point2D ep = q2d.getP2(); ((Point2D.Double)cp).y = qcy; q2d.setCurve(bp, cp, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); q2upanel.add(q2cyfield); q2dpanel.add( new Label("x2") ); final TextField q2x2field = new TextField("1.0"); q2x2field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double qx2 = 0.0; try { qx2 = Double.parseDouble(e.getActionCommand()); Point2D bp = q2d.getP1(); Point2D cp = q2d.getCtrlPt(); Point2D ep = q2d.getP2(); ((Point2D.Double)ep).x = qx2; q2d.setCurve(bp, cp, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); q2dpanel.add(q2x2field); q2dpanel.add( new Label("y2") ); final TextField q2y2field = new TextField("1.0"); q2y2field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { double qy2 = 0.0; try { qy2 = Double.parseDouble(e.getActionCommand()); Point2D bp = q2d.getP1(); Point2D cp = q2d.getCtrlPt(); Point2D ep = q2d.getP2(); ((Point2D.Double)ep).y = qy2; q2d.setCurve(bp, cp, ep); spanel.setShape(shape); Font3D f3d = new Font3D( font, new FontExtrusion(shape) ); universe.getLocale().removeBranchGraph(scene); text3d.setFont3D(f3d); universe.addBranchGraph(scene); } catch (NumberFormatException ex) {} } }); q2dpanel.add(q2y2field); Panel[] dpanels = new Panel[3]; for (int i=0; i<3; i++) { dpanels[i] = new Panel(); dpanels[i].setLayout( new FlowLayout(FlowLayout.LEFT) ); text3dpanel.add(dpanels[i]); } dpanels[0].add( new Label("Text") ); final TextField txfield = new TextField(text, 24); txfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { text = e.getActionCommand(); universe.getLocale().removeBranchGraph(scene); text3d.setString(text); universe.addBranchGraph(scene); } }); dpanels[0].add(txfield); final Choice fchoice = new Choice(); fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(); font = fonts[0]; for (int i=0; i