This simple program shows how to apply a texture Image to a rotated 3D box. The user can put a title into the canvas. A list that allows the user to select one of the available fonts on his/her local system is also provided. The output can be saved to disk as a PNG file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | import com.sun.j3d.utils.geometry.Primitive; import com.sun.j3d.utils.geometry.Box; import com.sun.j3d.utils.image.TextureLoader; import com.sun.j3d.utils.universe.SimpleUniverse; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.media.j3d.Appearance; import javax.media.j3d.BranchGroup; import javax.media.j3d.Canvas3D; import javax.media.j3d.Font3D; import javax.media.j3d.FontExtrusion; import javax.media.j3d.GraphicsContext3D; import javax.media.j3d.ImageComponent; import javax.media.j3d.ImageComponent2D; import javax.media.j3d.Material; import javax.media.j3d.Raster; import javax.media.j3d.Shape3D; import javax.media.j3d.Text3D; import javax.media.j3d.Texture; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.swing.*; import javax.vecmath.Color3f; import javax.vecmath.Point3f; import javax.vecmath.Vector3f; public class TextToImage3D extends JFrame{ private JPanel panel=null; private JTextField tfTitle=null; private JList lFont=null; private JScrollPane splFont=null; private JButton bLoad=null; private JButton bShow=null; private JButton bSave=null; private BranchGroup bg=null; private SimpleUniverse su=null; private Canvas3D canvas=null; private Box box=null; public TextToImage3D(){ super("Text To Image 3D"); setSize(700,550); setLayout(new BorderLayout()); panel=new JPanel(); panel.setPreferredSize(new Dimension(200,550)); panel.setLayout(new FlowLayout()); tfTitle=new JTextField(); tfTitle.setColumns(16); panel.add(tfTitle); GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); String []fontFamilies=ge.getAvailableFontFamilyNames(); lFont=new JList(fontFamilies); splFont=new JScrollPane(lFont); panel.add(splFont); bLoad=new JButton("Load"); bLoad.addActionListener(new ButtonHandler()); panel.add(bLoad); bShow=new JButton("Show"); bShow.addActionListener(new ButtonHandler()); panel.add(bShow); bSave=new JButton("Save"); bSave.addActionListener(new ButtonHandler()); panel.add(bSave); add("West",panel); GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration(); canvas=new Canvas3D(config); canvas.setSize(500,550); add("Center",canvas); su=new SimpleUniverse(canvas); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="Load"){ su.getViewingPlatform().setNominalViewingTransform(); bg=new BranchGroup(); FileDialog fd=new FileDialog(TextToImage3D.this,"Open Image File",FileDialog.LOAD); fd.setDirectory("C:\\"); fd.setVisible(true); String filename=fd.getDirectory()+fd.getFile(); TextureLoader loader = new TextureLoader(filename,"RGB", new Container()); Texture texture = loader.getTexture(); Appearance ap = new Appearance(); ap.setTexture(texture); box=new Box(0.5f,0.5f,0.5f,Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS,ap); TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D(); Vector3f v3f = new Vector3f(0.0f,-0.2f,0.0f); t3d.setTranslation(v3f); Transform3D rot = new Transform3D(); rot.rotX(Math.PI / 5); rot.rotY(Math.PI / 5); t3d.mul(rot); tg.setTransform(t3d); tg.addChild(box); bg.addChild(tg); su.addBranchGraph(bg); } if(e.getActionCommand()=="Show"){ su.getViewingPlatform().setNominalViewingTransform(); bg=new BranchGroup(); String temp=tfTitle.getText(); String font=(String)lFont.getSelectedValue(); Font3D f3d=new Font3D(new Font(font,Font.PLAIN,1),new FontExtrusion()); Text3D tx3d=new Text3D(f3d,temp,new Point3f(0.0f,1.0f,-1.9f)); tx3d.setAlignment(Text3D.ALIGN_CENTER); Color3f white = new Color3f(1.0f,1.0f,1.0f); Color3f blue = new Color3f(0.2f,0.2f,1f); Appearance a = new Appearance(); a.setCapability(Appearance.ALLOW_TEXTURE_WRITE); a.setCapability(Appearance.ALLOW_TEXGEN_WRITE); Material m = new Material(blue,blue,blue,white,60.0f); m.setLightingEnable(true); a.setMaterial(m); Shape3D sh=new Shape3D(); sh.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE); sh.setGeometry(tx3d); sh.setAppearance(a); bg.addChild(sh); su.addBranchGraph(bg); } if(e.getActionCommand()=="Save"){ GraphicsContext3D ctx=canvas.getGraphicsContext3D(); int w=canvas.getWidth(); int h=canvas.getHeight(); BufferedImage bi=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); ImageComponent2D im=new ImageComponent2D(ImageComponent.FORMAT_RGB,bi); Raster ras = new Raster(new Point3f(-1.0f,-1.0f,-1.0f ),Raster.RASTER_COLOR,0,0,w,h,im,null ); ctx.flush(true); ctx.readRaster(ras); BufferedImage bufImage = new BufferedImage(w,h, BufferedImage.TYPE_INT_ARGB); bufImage=ras.getImage().getImage(); try { ImageIO.write(bufImage,"png",new File("3dresult.png") ); } catch (IOException ioe) { ioe.printStackTrace(); } } } } public static void main(String[] args){ TextToImage3D ap=new TextToImage3D(); } } |
No comments:
Post a Comment