Mercurial > hg > Members > tatsuki > Alice
changeset 88:33a19ca88e43 working
Singleton is removed .
author | sugi |
---|---|
date | Mon, 04 Jun 2012 16:08:53 +0900 |
parents | 899b2b78ac75 |
children | e269cedd8bae |
files | src/alice/datasegment/Receiver.java src/alice/test/topology/movement/CheckMyName.java src/alice/test/topology/movement/FishMovement.java src/alice/test/topology/movement/KeyInput.java src/alice/test/topology/movement/MakeFrame.java src/alice/test/topology/movement/MakeObject.java src/alice/test/topology/movement/SendLocation.java src/alice/test/topology/movement/checkLocation.java |
diffstat | 8 files changed, 299 insertions(+), 187 deletions(-) [+] |
line wrap: on
line diff
--- a/src/alice/datasegment/Receiver.java Sun Jun 03 18:56:24 2012 +0900 +++ b/src/alice/datasegment/Receiver.java Mon Jun 04 16:08:53 2012 +0900 @@ -78,6 +78,13 @@ return 0; } + public Float asFloat() { + if (val.getType() == ValueType.FLOAT) { + return val.asFloatValue().getFloat(); + } + return 0.0f; + } + public <T> T asClass(Class<T> clazz) { MessagePack msgpack = new MessagePack(); try {
--- a/src/alice/test/topology/movement/CheckMyName.java Sun Jun 03 18:56:24 2012 +0900 +++ b/src/alice/test/topology/movement/CheckMyName.java Mon Jun 04 16:08:53 2012 +0900 @@ -23,17 +23,16 @@ if (type.equals("cli")){ System.out.println("cli"+cliNum); - - FishMovement.getInstance(); - - SendLocation cs = new SendLocation(); - cs.nowX.setKey("parent","fishX"); + MakeFrame frame = new MakeFrame(); + MakeObject obj = null; + checkLocation cs = new checkLocation(frame,obj); cs.host.setKey("local","host"); - + cs.nowX.setKey("parent","fish1X"); + }else if(type.equals("node")){ System.out.println("node0"); - ods.update("local", "fishX", -1000); + ods.update("local", "fish1X", -1000); } }
--- a/src/alice/test/topology/movement/FishMovement.java Sun Jun 03 18:56:24 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,131 +0,0 @@ -package alice.test.topology.movement; - -import java.awt.GraphicsConfiguration; -import java.awt.Image; -import java.awt.MediaTracker; -import java.awt.Toolkit; - -import javax.media.j3d.Appearance; -import javax.media.j3d.BoundingSphere; -import javax.media.j3d.BranchGroup; -import javax.media.j3d.Canvas3D; -import javax.media.j3d.DirectionalLight; -import javax.media.j3d.Texture; -import javax.media.j3d.Transform3D; -import javax.media.j3d.TransformGroup; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.vecmath.Color3f; -import javax.vecmath.Vector3f; - -import com.sun.j3d.utils.geometry.Box; -import com.sun.j3d.utils.image.TextureLoader; -import com.sun.j3d.utils.universe.SimpleUniverse; -import com.sun.j3d.utils.universe.ViewingPlatform; - -public final class FishMovement { - - private Vector3f vector; - private Transform3D transform1; - private TransformGroup transform_group; - private Canvas3D canvas; - - private static class instanceHolder{ - private static final FishMovement instance = new FishMovement(); - } - public static FishMovement getInstance() { - return instanceHolder.instance; - - } - - private FishMovement(){ - JFrame frame = new JFrame(); - frame.setSize(1000,1000); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - JPanel cp = new JPanel(); - frame.add(cp); - - - GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); - - canvas = new Canvas3D(config); - canvas.setBounds(0,0,1000,1000);//set canvas size - cp.add(canvas);//register canvas with ContentPane - - SimpleUniverse universe = new SimpleUniverse(canvas); - - BranchGroup group1 = createBranch(); - universe.addBranchGraph(group1); - - BranchGroup group2 = createLight(); - universe.addBranchGraph(group2); - - ViewingPlatform camera = universe.getViewingPlatform(); - camera.setNominalViewingTransform(); - frame.setVisible(true); - } - - private BranchGroup createBranch(){ - BranchGroup scene = new BranchGroup(); - transform_group = new TransformGroup(); - scene.addChild(transform_group); - transform_group.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); - Box box = new Box(0.1f,0.1f,0.0f, - Box.GENERATE_NORMALS|Box.GENERATE_TEXTURE_COORDS,createAppearance()); - transform_group.addChild(box); - setLocation(-10.0f,0.0f); - return scene; - } - - private Appearance createAppearance() { - Appearance app = new Appearance(); - Image image = null; - Toolkit toolkit = Toolkit.getDefaultToolkit(); - image = toolkit.getImage("../image/fish.jpg"); - - MediaTracker mt = new MediaTracker(canvas); - mt.addImage(image, 0); - mt.checkAll(true); - try { - mt.waitForID(0); - - }catch (InterruptedException e){ - e.printStackTrace(); - - } - Texture texture = new TextureLoader(image,canvas).getTexture(); - app.setTexture(texture); - - - return app; - } - - private BranchGroup createLight(){ - BranchGroup scene = new BranchGroup(); - Color3f light_color = new Color3f(1.7f,1.7f,1.7f); - Vector3f light_direction = new Vector3f(0.2f,-0.2f,-0.6f); - DirectionalLight light = new DirectionalLight(light_color,light_direction); - BoundingSphere bounds = new BoundingSphere(); - light.setInfluencingBounds(bounds); - scene.addChild(light); - return scene; - } - - public void setLocation(float x,float y){ - transform1 = new Transform3D(); - vector = new Vector3f(x,y,0.0f); - transform1.setTranslation(vector); - transform_group.setTransform(transform1); - } - - public void setLocation(float x,float y,float z){ - transform1 = new Transform3D(); - vector = new Vector3f(x,y,z); - transform1.setTranslation(vector); - transform_group.setTransform(transform1); - } - - - - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/test/topology/movement/KeyInput.java Mon Jun 04 16:08:53 2012 +0900 @@ -0,0 +1,61 @@ +package alice.test.topology.movement; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +import javax.media.j3d.Transform3D; +import javax.media.j3d.TransformGroup; +import javax.vecmath.Vector3f; + + +public class KeyInput implements KeyListener{ + + int KeyCode = 0; + Vector3f vector; + Transform3D transform; + TransformGroup transformGroup; + + public KeyInput(){ + transform = new Transform3D(); + transformGroup= new TransformGroup(); + transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); + vector = new Vector3f(0.0f,0.0f,0.0f); + + + } + @Override + public void keyPressed(KeyEvent event) { + // TODO Auto-generated method stub + KeyCode = event.getKeyCode(); + switch(KeyCode) + { + case 37: + vector.x -= 0.1f; + break; + case 38: + vector.y += 0.1f; + break; + case 39: + vector.x += 0.1f; + break; + case 40: + vector.y -= 0.1f; + break; + } + transform.setTranslation(vector); + transformGroup.setTransform(transform); + } + + @Override + public void keyReleased(KeyEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void keyTyped(KeyEvent arg0) { + // TODO Auto-generated method stub + + } + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/test/topology/movement/MakeFrame.java Mon Jun 04 16:08:53 2012 +0900 @@ -0,0 +1,79 @@ +package alice.test.topology.movement; + +import java.awt.GraphicsConfiguration; + +import javax.media.j3d.BoundingSphere; +import javax.media.j3d.BranchGroup; +import javax.media.j3d.Canvas3D; +import javax.media.j3d.DirectionalLight; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.vecmath.Color3f; +import javax.vecmath.Vector3f; + +import com.sun.j3d.utils.universe.SimpleUniverse; +import com.sun.j3d.utils.universe.ViewingPlatform; + +public class MakeFrame { + + int fSizeX = 800; + int fSizeY = 800; + private Canvas3D canvas; + private SimpleUniverse universe; + private KeyInput key; + + public MakeFrame(){ + JFrame frame = new JFrame(); + frame.setSize(fSizeX,fSizeY); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + JPanel cp = new JPanel(); + cp.setLayout(null); + frame.add(cp); + + GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); + canvas = new Canvas3D(config); + canvas.setBounds(0,0,fSizeX,fSizeY); + cp.add(canvas); + + universe = new SimpleUniverse(canvas); + universe.addBranchGraph(createLight()); + + key = new KeyInput(); + canvas.addKeyListener(key); + + frame.setVisible(true); + + ViewingPlatform camera = universe.getViewingPlatform(); + camera.setNominalViewingTransform(); + } + + private BranchGroup createLight(){ + BranchGroup scene = new BranchGroup(); + Color3f light_color = new Color3f(1.7f,1.7f,1.7f); + Vector3f light_direction = new Vector3f(0.2f,-0.2f,-0.6f); + DirectionalLight light = new DirectionalLight(light_color,light_direction); + BoundingSphere bounds = new BoundingSphere(); + light.setInfluencingBounds(bounds); + scene.addChild(light); + return scene; + } + + public void register(BranchGroup group){ + this.universe.addBranchGraph(group); + } + + public Canvas3D getCanvas(){ + return this.canvas; + } + + public KeyInput getKey(){ + return this.key; + } + + public SimpleUniverse getUniverse(){ + return this.universe; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/test/topology/movement/MakeObject.java Mon Jun 04 16:08:53 2012 +0900 @@ -0,0 +1,86 @@ +package alice.test.topology.movement; + +import java.awt.Image; +import java.awt.MediaTracker; +import java.awt.Toolkit; +import java.io.File; + +import javax.media.j3d.Appearance; +import javax.media.j3d.BranchGroup; +import javax.media.j3d.Canvas3D; +import javax.media.j3d.Texture; +import javax.media.j3d.Transform3D; +import javax.media.j3d.TransformGroup; +import javax.vecmath.Vector3f; + +import com.sun.j3d.utils.geometry.Box; +import com.sun.j3d.utils.image.TextureLoader; + +public class MakeObject { + + public Vector3f vector; + private Transform3D transform; + private TransformGroup transform_group; + private Canvas3D canvas; + private KeyInput key; + + public MakeObject(Canvas3D C){ + this.canvas = C; + } + + public MakeObject(Canvas3D C,KeyInput K){ + this.canvas = C; + this.key = K; + } + + public BranchGroup createBranch(){ + BranchGroup scene = new BranchGroup(); + Box box = new Box(0.1f,0.1f,0.0f, + Box.GENERATE_NORMALS|Box.GENERATE_TEXTURE_COORDS,createAppearance()); + + transform_group = new TransformGroup(); + transform_group.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); + transform_group.addChild(box); + scene.addChild(transform_group); + + + /* + key.transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); + key.transformGroup.addChild(box); + scene.addChild(key.transformGroup); + */ + return scene; + + } + + private Appearance createAppearance(){ + System.out.println(new File(".").getAbsolutePath()); + Appearance app = new Appearance(); + Image image = null; + Toolkit toolkit = Toolkit.getDefaultToolkit(); + //image = toolkit.getImage("image/fish.jpg"); + image = toolkit.getImage("../image/fish.jpg");//jar + MediaTracker mt = new MediaTracker(canvas); + mt.addImage(image, 0); + mt.checkAll(true); + try { + mt.waitForID(0); + + }catch (InterruptedException e){ + e.printStackTrace(); + + } + Texture texture = new TextureLoader(image,canvas).getTexture(); + app.setTexture(texture); + return app; + + } + + public void setLocation(float x,float y){ + transform = new Transform3D(); + vector = new Vector3f(x,y,0.0f); + transform.setTranslation(vector); + transform_group.setTransform(transform); + } +} +
--- a/src/alice/test/topology/movement/SendLocation.java Sun Jun 03 18:56:24 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -package alice.test.topology.movement; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import alice.codesegment.CodeSegment; -import alice.datasegment.CommandType; -import alice.datasegment.Receiver; - - -public class SendLocation extends CodeSegment{ - - public Receiver nowX = ids.create(CommandType.PEEK); - public Receiver host = ids.create(CommandType.PEEK); - Pattern pattern = Pattern.compile("^(cli)([0-9]+)$"); - - @Override - public void run(){ - System.out.println("SendLocation"); - String name = host.asString(); - Matcher matcher = pattern.matcher(name); - - matcher.find(); - int num = new Integer(matcher.group(2)); - int x = this.nowX.asInteger(); - System.out.println("X : " +(float)x/1000); - - float start = num * 2.0f - 3; - float end = num * 2.0f - 0.9f; - - if (start <= (float)x/1000 && (float)x/1000<= end){ - float positionX = (float)x/1000 - 2*(num -1) ; - FishMovement fm = FishMovement.getInstance(); - fm.setLocation(positionX , 0.0f); - x++; - ods.update("parent", "fishX", x); - } - SendLocation cs = new SendLocation(); - cs.host.setKey("local","host"); - cs.nowX.setKey("parent","fishX",this.nowX.index); - - - - - - } - - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/test/topology/movement/checkLocation.java Mon Jun 04 16:08:53 2012 +0900 @@ -0,0 +1,60 @@ +package alice.test.topology.movement; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; + + +public class checkLocation extends CodeSegment{ + + public Receiver nowX = ids.create(CommandType.PEEK); + public Receiver host = ids.create(CommandType.PEEK); + public MakeFrame frame; + public MakeObject obj; + Pattern pattern = Pattern.compile("^(cli)([0-9]+)$"); + + public checkLocation(MakeFrame MF , MakeObject MO){ + this.frame = MF; + this.obj = MO; + } + + @Override + public void run(){ + String name = host.asString(); + Matcher matcher = pattern.matcher(name); + matcher.find(); + int num = new Integer(matcher.group(2)); + + float startX = 2*num - 3.0f; + float endX = 2*num -0.9f; + float x = (float)this.nowX.asInteger()/1000; + System.out.println("now position is :" + x); + if (startX < x && x < endX){ + obj.setLocation(x - 2*(num-1), 0); + ods.update("parent", "fish1X", this.nowX.asInteger()+1); + + } else if(startX == x){ + this.obj = new MakeObject(frame.getCanvas()); + frame.register(obj.createBranch()); + ods.update("parent", "fish1X", this.nowX.asInteger()+1); + + } else if(endX == x){ + System.out.println("out of range"); + } + + + checkLocation cs = new checkLocation(this.frame,this.obj); + cs.host.setKey("local","host"); + cs.nowX.setKey("parent","fish1X",this.nowX.index); + + + + + + } + + +}