summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/kernel/perspective/Viewer3D.java
diff options
context:
space:
mode:
Diffstat (limited to 'logo/src/xlogo/kernel/perspective/Viewer3D.java')
-rw-r--r--logo/src/xlogo/kernel/perspective/Viewer3D.java208
1 files changed, 104 insertions, 104 deletions
diff --git a/logo/src/xlogo/kernel/perspective/Viewer3D.java b/logo/src/xlogo/kernel/perspective/Viewer3D.java
index 0b7a312..2ade695 100644
--- a/logo/src/xlogo/kernel/perspective/Viewer3D.java
+++ b/logo/src/xlogo/kernel/perspective/Viewer3D.java
@@ -1,29 +1,29 @@
/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Loic Le Coq
* Copyright (C) 2013 Marko Zivkovic
- *
+ *
* Contact Information: marko88zivkovic at gmail dot com
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
- * any later version. This program is distributed in the hope that it will be
- * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
- * Public License for more details. You should have received a copy of the
- * GNU General Public License along with this program; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version. This program is distributed in the hope that it will be
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+ * Public License for more details. You should have received a copy of the
+ * GNU General Public License along with this program; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
- *
- *
+ *
+ *
* This Java source code belongs to XLogo4Schools, written by Marko Zivkovic
* during his Bachelor thesis at the computer science department of ETH Zurich,
* in the year 2013 and/or during future work.
- *
+ *
* It is a reengineered version of XLogo written by Loic Le Coq, published
* under the GPL License at http://xlogo.tuxfamily.org/
- *
+ *
* Contents of this file were initially written by Loic Le Coq,
- * modifications, extensions, refactorings might have been applied by Marko Zivkovic
+ * modifications, extensions, refactorings might have been applied by Marko Zivkovic
*/
package xlogo.kernel.perspective;
@@ -80,9 +80,9 @@ import java.awt.event.ActionEvent;
/**
* This Frame displays the 3D main Scene
- *
+ *
* BG scene
- *
+ *
* BG backBranchGroup | BG Mylight (4) | BG
* Background Light 1,2,3,4 Other objects
*/
@@ -95,13 +95,13 @@ public class Viewer3D extends JFrame implements ActionListener
private final static String ACTION_LIGHT2 = "light2";
private final static String ACTION_LIGHT3 = "light3";
private final static String ACTION_FOG = "fog";
-
+
// To store the Light attributes
private MyLight[] myLights;
// To store the Fog attributes
private MyFog myFog;
// Gui Components
- private ImageIcon iscreenshot = Utils.dimensionne_image("screenshot.png", this);
+ private final ImageIcon iscreenshot = Utils.dimensionne_image("screenshot.png", this);
private JButton screenshot;
private static final long serialVersionUID = 1L;
private World3D w3d;
@@ -117,33 +117,33 @@ public class Viewer3D extends JFrame implements ActionListener
private Background back;
private PanelLight panelLight;
private PanelFog panelFog;
-
- public Viewer3D(World3D w3d, Color c)
+
+ public Viewer3D(final World3D w3d, final Color c)
{
this.w3d = w3d;
this.backgroundColor = new Color3f(c);
initGui();
}
-
- public void actionPerformed(ActionEvent e)
+
+ public void actionPerformed(final ActionEvent e)
{
- String cmd = e.getActionCommand();
+ final String cmd = e.getActionCommand();
// Take a screenshot
if (cmd.equals(Viewer3D.ACTION_SCREENSHOT))
{
- WriteImage wi = new WriteImage(this, null);
- int id = wi.chooseFile();
+ final WriteImage wi = new WriteImage(this, null);
+ final int id = wi.chooseFile();
if (id == JFileChooser.APPROVE_OPTION)
{
- GraphicsContext3D ctx = canvas3D.getGraphicsContext3D();
- java.awt.Dimension scrDim = canvas3D.getSize();
-
+ final GraphicsContext3D ctx = canvas3D.getGraphicsContext3D();
+ final java.awt.Dimension scrDim = canvas3D.getSize();
+
// setting raster component
- Raster ras = new Raster(new Point3f(-1.0f, -1.0f, -1.0f), Raster.RASTER_COLOR, 0, 0, scrDim.width,
+ final Raster ras = new Raster(new Point3f(-1.0f, -1.0f, -1.0f), Raster.RASTER_COLOR, 0, 0, scrDim.width,
scrDim.height, new ImageComponent2D(ImageComponent.FORMAT_RGB, new BufferedImage(scrDim.width,
scrDim.height, java.awt.image.BufferedImage.TYPE_INT_RGB)), null);
ctx.readRaster(ras);
- BufferedImage img = ras.getImage().getImage();
+ final BufferedImage img = ras.getImage().getImage();
wi.setImage(img);
wi.start();
}
@@ -172,83 +172,83 @@ public class Viewer3D extends JFrame implements ActionListener
else if (cmd.equals(Viewer3D.ACTION_FOG))
{
new FogDialog(this, myFog, Logo.messages.getString("3d.fog"));
-
+
}
}
-
+
public void setText()
{
setTitle(Logo.messages.getString("3d.viewer"));
panelFog.setText();
panelLight.setText();
-
+
}
-
+
private void initGui()
{
- this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+ this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setIconImage(Toolkit.getDefaultToolkit().createImage(Utils.class.getResource("icone.png")));
-
+
// Creation d'un composant de classe Canvas3D permettant de visualiser
// une scène 3D
canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
// Création d'un univers 3D rattaché au composant 3D
universe = new SimpleUniverse(canvas3D);
-
+
// Install the camera at the valid position with correct orientation
- TransformGroup tg = universe.getViewingPlatform().getViewPlatformTransform();
- Transform3D trans = new Transform3D();
+ final TransformGroup tg = universe.getViewingPlatform().getViewPlatformTransform();
+ final Transform3D trans = new Transform3D();
if (null == w3d)
{
w3d = new World3D();
}
trans.setTranslation(new Vector3d(-w3d.xCamera / 1000, -w3d.yCamera / 1000, -w3d.zCamera / 1000));
- Transform3D rot = new Transform3D();
+ final Transform3D rot = new Transform3D();
rot.lookAt(new Point3d(-w3d.xCamera / 1000, -w3d.yCamera / 1000, -w3d.zCamera / 1000), new Point3d(0, 0, 0),
new Vector3d(0, 1, 0));
rot.invert();
trans.mul(rot);
tg.setTransform(trans);
- OrbitBehavior ob = new OrbitBehavior(canvas3D, OrbitBehavior.REVERSE_ALL);
+ final OrbitBehavior ob = new OrbitBehavior(canvas3D, OrbitBehavior.REVERSE_ALL);
ob.setRotationCenter(new Point3d(0, 0, 0));
ob.setSchedulingBounds(new BoundingSphere(new Point3d(0, 0, 0), 2 * w3d.r / 1000));
universe.getViewingPlatform().setViewPlatformBehavior(ob);
-
+
// Create the root of the branch graph
scene = new BranchGroup();
branchManager = new BranchManager();
// scene.setName("Main Branch");
// We can add New BranchGroup dynamically in the main scene
- scene.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
+ scene.setCapability(Group.ALLOW_CHILDREN_EXTEND);
// We can remove BranchGroup dynamically in the main scene
- scene.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
-
+ scene.setCapability(Group.ALLOW_CHILDREN_WRITE);
+
// Configure and create background
createBackground();
-
+
// Configure Lights
initLights();
-
+
// Configure Fog
myFog = new MyFog(MyFog.FOG_OFF, backgroundColor);
scene.addChild(myFog);
-
+
// Rattachement de la scène 3D à l'univers
universe.addBranchGraph(scene);
-
- Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
- int width = d.width * 9 / 10;
- int height = d.height * 9 / 10;
+
+ final Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
+ final int width = d.width * 9 / 10;
+ final int height = d.height * 9 / 10;
setSize(width, height);
getContentPane().setLayout(new GridBagLayout());
screenshot = new JButton(iscreenshot);
screenshot.addActionListener(this);
screenshot.setMaximumSize(new Dimension(100, 100));
screenshot.setActionCommand(Viewer3D.ACTION_SCREENSHOT);
-
+
panelLight = new PanelLight(this);
panelFog = new PanelFog(this);
-
+
getContentPane().add(
canvas3D,
new GridBagConstraints(0, 0, 1, 3, 2.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
@@ -268,29 +268,29 @@ public class Viewer3D extends JFrame implements ActionListener
setText();
getContentPane().validate();
}
-
+
/**
* This method adds a shape3D to the main scene
- *
+ *
* @param s
* The Shape to add
*/
- public void add3DObject(Shape3D s)
+ public void add3DObject(final Shape3D s)
{
branchManager.add3DObject(s);
}
-
- public void add2DText(TransformGroup tg)
+
+ public void add2DText(final TransformGroup tg)
{
branchManager.add2DText(tg);
}
-
+
public void insertBranch()
{
branchManager.insertBranch();
branchManager = new BranchManager();
}
-
+
/**
* This methods adds two default PointLight int the 3d Scene
*/
@@ -298,14 +298,14 @@ public class Viewer3D extends JFrame implements ActionListener
{
myLights = new MyLight[4];
// First Default Point Light
- Color3f color = new Color3f(1f, 1f, 1f);
+ final Color3f color = new Color3f(1f, 1f, 1f);
Point3f pos = new Point3f((float) w3d.xCamera / 1000, (float) w3d.yCamera / 1000, (float) w3d.zCamera / 1000);
myLights[0] = new MyLight(MyLight.LIGHT_POINT, color, pos);
-
+
// Second default Point Light
pos = new Point3f(-(float) w3d.xCamera / 1000, -(float) w3d.yCamera / 1000, -(float) w3d.zCamera / 1000);
myLights[1] = new MyLight(MyLight.LIGHT_POINT, color, pos);
-
+
myLights[2] = new MyLight(MyLight.LIGHT_OFF);
myLights[3] = new MyLight(MyLight.LIGHT_OFF);
for (int i = 0; i < 4; i++)
@@ -314,26 +314,26 @@ public class Viewer3D extends JFrame implements ActionListener
}
addAllLights(scene);
}
-
+
/**
* Add all lights in the main 3D scene
*/
- private void addAllLights(BranchGroup bg)
+ private void addAllLights(final BranchGroup bg)
{
for (int i = 0; i < 4; i++)
{
bg.addChild(myLights[i]);
}
}
-
+
/**
* add a light in the main scene
*/
- protected void addNode(Node l)
+ protected void addNode(final Node l)
{
scene.addChild(l);
}
-
+
/**
* This methods erase all drawings on the 3D Viewer Drawing Area
*/
@@ -365,16 +365,16 @@ public class Viewer3D extends JFrame implements ActionListener
* }
* }
*/
-
+
}
-
- public void updateBackGround(Color c)
+
+ public void updateBackGround(final Color c)
{
- backgroundColor = new Color3f(c);
+ backgroundColor = new Color3f(c.getRGBColorComponents(null));
backBranchgroup.detach();
createBackground();
}
-
+
/**
* This method creates the Background with the defined color
*/
@@ -382,67 +382,67 @@ public class Viewer3D extends JFrame implements ActionListener
{
backBranchgroup = new BranchGroup();
backBranchgroup.setCapability(BranchGroup.ALLOW_DETACH);
- backBranchgroup.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
-
+ backBranchgroup.setCapability(Group.ALLOW_CHILDREN_WRITE);
+
back = new Background(backgroundColor);
back.setApplicationBounds(new BoundingSphere());
back.setCapability(Background.ALLOW_COLOR_WRITE);
backBranchgroup.addChild(back);
scene.addChild(backBranchgroup);
}
-
+
class BranchManager
{
BranchGroup bg;
-
+
BranchManager()
{
bg = new BranchGroup();
bg.setCapability(BranchGroup.ALLOW_DETACH);
}
-
+
/**
* This method adds a shape3D to the main scene
- *
+ *
* @param s
* The Shape to add
*/
- void add3DObject(Shape3D s)
+ void add3DObject(final Shape3D s)
{
bg.addChild(s);
}
-
- void add2DText(TransformGroup tg)
+
+ void add2DText(final TransformGroup tg)
{
bg.addChild(tg);
}
-
+
void insertBranch()
{
bg.compile();
scene.addChild(bg);
}
}
-
+
class PanelLight extends JPanel
{
private static final long serialVersionUID = 1L;
private JButton[] buttonLights;
- private Viewer3D viewer3d;
-
- PanelLight(Viewer3D viewer3d)
+ private final Viewer3D viewer3d;
+
+ PanelLight(final Viewer3D viewer3d)
{
this.viewer3d = viewer3d;
initGui();
}
-
+
private void initGui()
{
buttonLights = new JButton[4];
setLayout(new GridLayout(2, 2, 10, 10));
for (int i = 0; i < 4; i++)
{
- ImageIcon ilight = Utils.dimensionne_image("light" + i + ".png", viewer3d);
+ final ImageIcon ilight = Utils.dimensionne_image("light" + i + ".png", viewer3d);
buttonLights[i] = new JButton(ilight);
add(buttonLights[i]);
buttonLights[i].addActionListener(viewer3d);
@@ -450,44 +450,44 @@ public class Viewer3D extends JFrame implements ActionListener
}
setText();
}
-
+
void setText()
{
- TitledBorder tb = BorderFactory.createTitledBorder(Logo.messages.getString("3d.light"));
+ final TitledBorder tb = BorderFactory.createTitledBorder(Logo.messages.getString("3d.light"));
tb.setTitleFont(WSManager.getWorkspaceConfig().getFont());
setBorder(tb);
}
}
-
+
class PanelFog extends JPanel
{
-
+
private static final long serialVersionUID = 1L;
private JButton buttonFog;
- private Viewer3D viewer3d;
-
- PanelFog(Viewer3D viewer3d)
+ private final Viewer3D viewer3d;
+
+ PanelFog(final Viewer3D viewer3d)
{
this.viewer3d = viewer3d;
initGui();
}
-
+
private void initGui()
{
- ImageIcon ifog =Utils.dimensionne_image("fog.png", viewer3d);
+ final ImageIcon ifog =Utils.dimensionne_image("fog.png", viewer3d);
buttonFog = new JButton(ifog);
buttonFog.setActionCommand(Viewer3D.ACTION_FOG);
buttonFog.addActionListener(viewer3d);
add(buttonFog);
setText();
}
-
+
void setText()
{
- TitledBorder tb = BorderFactory.createTitledBorder(Logo.messages.getString("3d.fog"));
+ final TitledBorder tb = BorderFactory.createTitledBorder(Logo.messages.getString("3d.fog"));
tb.setTitleFont(WSManager.getWorkspaceConfig().getFont());
setBorder(tb);
}
}
-
+
}