SHOW:
|
|
- or go back to the newest paste.
| 1 | import java.applet.Applet; | |
| 2 | import java.awt.*; | |
| 3 | import java.awt.event.*; | |
| 4 | import com.sun.j3d.utils.applet.MainFrame; | |
| 5 | import com.sun.j3d.utils.behaviors.vp.OrbitBehavior; | |
| 6 | import com.sun.j3d.utils.geometry.GeometryInfo; | |
| 7 | import com.sun.j3d.utils.geometry.Cylinder; | |
| 8 | import com.sun.j3d.utils.geometry.Sphere; | |
| 9 | import com.sun.j3d.utils.geometry.NormalGenerator; | |
| 10 | import com.sun.j3d.utils.universe.*; | |
| 11 | import java.util.Enumeration; | |
| 12 | import javax.media.j3d.*; | |
| 13 | import javax.vecmath.*; | |
| 14 | import java.util.Timer; | |
| 15 | import java.util.TimerTask; | |
| 16 | import javax.swing.JLabel; | |
| 17 | ||
| 18 | ||
| 19 | /** | |
| 20 | * Klasa główna 'Main' zawiera deklaracje zmiennych | |
| 21 | * @author | |
| 22 | */ | |
| 23 | - | public final class Main1 extends Applet implements KeyListener{
|
| 23 | + | public final class Main extends Applet implements KeyListener{
|
| 24 | ||
| 25 | final private static int Width = 900; | |
| 26 | final private static int Height = 600; | |
| 27 | final private OrbitBehavior obserwator; | |
| 28 | final private ViewingPlatform vPlatform; | |
| 29 | private RotationInterpolator punkt1; // arm sweep - baza robota | |
| 30 | private RotationInterpolator punkt2; // shoulder - ramie robota | |
| 31 | private RotationInterpolator punkt3; | |
| 32 | final private Timer zegar = new Timer(); | |
| 33 | final private SimpleUniverse universe; | |
| 34 | private double x = 0.0f; | |
| 35 | private double y = 0.65f; | |
| 36 | private double z = 0.5f; | |
| 37 | private boolean spacja = false; | |
| 38 | private boolean zatrzask = false; | |
| 39 | private boolean inCollision = false; | |
| 40 | JLabel label = new JLabel("Ramię Robota:");
| |
| 41 | private TransformGroup objTrans; | |
| 42 | private TransformGroup objTrans2; | |
| 43 | private TransformGroup objTrans3; | |
| 44 | private TransformGroup objTrans4; | |
| 45 | final private Transform3D trans = new Transform3D(); | |
| 46 | final private Transform3D trans2 = new Transform3D(); | |
| 47 | final private Transform3D trans3 = new Transform3D(); | |
| 48 | final private Transform3D trans4 = new Transform3D(); | |
| 49 | final private Transform3D hujRotation = new Transform3D(); | |
| 50 | final private Transform3D tempTransform = new Transform3D(); | |
| 51 | ||
| 52 | ||
| 53 | /** | |
| 54 | * W metodzie tworzymy scenę oraz elementy, które będą w niej zawarte - tj. | |
| 55 | * elementy manipulatora cylindrycznego (cylinder, cylinder2, chwytak i przedmiot), | |
| 56 | * oświetlenie sceny oraz podłoże służące jako punkt odniesienia. | |
| 57 | * @return obiekt 'scena' typu BranchGroup | |
| 58 | */ | |
| 59 | public BranchGroup createSceneGraph() {
| |
| 60 | ||
| 61 | Alpha alpha1 = new Alpha(-1, 5000); // arm sweep | |
| 62 | ||
| 63 | BranchGroup scena = new BranchGroup(); // scena główna - swiatla, podloga | |
| 64 | BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); | |
| 65 | ||
| 66 | Transform3D tmp = new Transform3D(); | |
| 67 | ||
| 68 | ||
| 69 | objTrans = new TransformGroup(); | |
| 70 | objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); | |
| 71 | objTrans2 = new TransformGroup(); | |
| 72 | objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); | |
| 73 | ||
| 74 | //statyw | |
| 75 | scena.addChild(objTrans); | |
| 76 | Cylinder cylinder1 = new Cylinder(0.1f, 1.5f); | |
| 77 | objTrans = new TransformGroup(); | |
| 78 | objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); | |
| 79 | Transform3D pos1 = new Transform3D(); | |
| 80 | pos1.setTranslation(new Vector3f(0.0f, 0.25f, 0.0f)); | |
| 81 | objTrans.setTransform(pos1); | |
| 82 | objTrans.addChild(cylinder1); | |
| 83 | //scena.addChild(objTrans); | |
| 84 | ||
| 85 | //ramie | |
| 86 | scena.addChild(objTrans2); | |
| 87 | Cylinder cylinder2 = new Cylinder(0.05f, 1.25f); | |
| 88 | objTrans2 = new TransformGroup(); | |
| 89 | objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); | |
| 90 | objTrans2.addChild(cylinder2); | |
| 91 | ||
| 92 | //kulka | |
| 93 | scena.addChild(objTrans3); | |
| 94 | Sphere chwytak = new Sphere(0.08f); | |
| 95 | objTrans3 = new TransformGroup(); | |
| 96 | objTrans3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); | |
| 97 | objTrans3.addChild(chwytak); | |
| 98 | scena.addChild(objTrans3); | |
| 99 | ||
| 100 | //kulka | |
| 101 | scena.addChild(objTrans4); | |
| 102 | Appearance app = new Appearance(); | |
| 103 | app.setColoringAttributes(new ColoringAttributes(1.0f, 0.0f, 0.0f,ColoringAttributes.ALLOW_COLOR_WRITE)); | |
| 104 | app.setCapability(app.ALLOW_COLORING_ATTRIBUTES_WRITE); | |
| 105 | Sphere przedmiot = new Sphere(0.1f, app); | |
| 106 | objTrans4 = new TransformGroup(); | |
| 107 | objTrans4.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); | |
| 108 | Transform3D kulka2 = new Transform3D(); | |
| 109 | kulka2.setTranslation(new Vector3f(0.0f, 0.0f, 1.0f)); | |
| 110 | objTrans4.setTransform(kulka2); | |
| 111 | objTrans4.addChild(przedmiot); | |
| 112 | scena.addChild(objTrans4); | |
| 113 | ||
| 114 | // podłoże | |
| 115 | Appearance ap = new Appearance(); | |
| 116 | Color3f black = new Color3f(.0f,.0f,.0f); | |
| 117 | Color3f white = new Color3f(0.8f,.8f,.8f); | |
| 118 | ap.setMaterial(new Material(white,black,white,black,80f)); | |
| 119 | podloga ob = new podloga(); | |
| 120 | ob.setAppearance(ap); | |
| 121 | scena.addChild(ob); | |
| 122 | ||
| 123 | // światło kierunkowe | |
| 124 | Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f); | |
| 125 | Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f); | |
| 126 | DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); | |
| 127 | light1.setInfluencingBounds(bounds); | |
| 128 | scena.addChild(light1); | |
| 129 | ||
| 130 | // światło | |
| 131 | Color3f ambientColor = new Color3f(1.0f, 1.0f, 1.0f); | |
| 132 | AmbientLight ambientLightNode = new AmbientLight(ambientColor); | |
| 133 | ambientLightNode.setInfluencingBounds(bounds); | |
| 134 | scena.addChild(ambientLightNode); | |
| 135 | ||
| 136 | //objTrans2.addChild(objTrans3); | |
| 137 | objTrans.addChild(objTrans2); | |
| 138 | scena.addChild(objTrans); | |
| 139 | ||
| 140 | // Create a new Behavior object that will perform the collision | |
| 141 | // detection on the specified object, and add it into | |
| 142 | // the scene graph. | |
| 143 | CollisionDetector cd = new CollisionDetector(przedmiot); | |
| 144 | BoundingSphere boundy = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), | |
| 145 | 1.0); | |
| 146 | cd.setSchedulingBounds(boundy); | |
| 147 | objTrans3.addChild(cd); | |
| 148 | ||
| 149 | scena.compile(); | |
| 150 | return scena; | |
| 151 | } | |
| 152 | ||
| 153 | ||
| 154 | /** | |
| 155 | * Konstruktor bezparametrowy klasy 'Main'. Ustawia on wygląd okna programu | |
| 156 | * (BorderLayout()), tworzy w nim podstawowe obiekty Java3D (SimpleUniverse). | |
| 157 | * Ustawia pozycję i zachowanie kamery oraz uruchamia zegar. | |
| 158 | */ | |
| 159 | - | public Main1() {
|
| 159 | + | public Main() {
|
| 160 | setLayout(new BorderLayout()); | |
| 161 | ||
| 162 | GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); | |
| 163 | Canvas3D canvas3D = new Canvas3D(config); | |
| 164 | add("Center", canvas3D);
| |
| 165 | canvas3D.addKeyListener(this); | |
| 166 | add("South",label);
| |
| 167 | ||
| 168 | // Create a simple scene and attach it to the virtual universe | |
| 169 | BranchGroup scene = createSceneGraph(); | |
| 170 | universe = new SimpleUniverse(canvas3D); | |
| 171 | universe.addBranchGraph(scene); | |
| 172 | obserwator = new OrbitBehavior(canvas3D); | |
| 173 | BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); | |
| 174 | obserwator.setSchedulingBounds(bounds); | |
| 175 | vPlatform = universe.getViewingPlatform(); | |
| 176 | Transform3D temp = new Transform3D(); | |
| 177 | temp.set(new Vector3f(0f,0f,7.0f)); | |
| 178 | vPlatform.getViewPlatformTransform().setTransform(temp); | |
| 179 | vPlatform.setViewPlatformBehavior(obserwator); | |
| 180 | zegar.scheduleAtFixedRate(new Ruch(), 20, 25); | |
| 181 | } | |
| 182 | ||
| 183 | /** | |
| 184 | * Funkcja tworzy okno typu MainFrame o zadanych wymiarach Width i Height. | |
| 185 | * Konstruktor okna wywołuje konstruktor klasy Main(). | |
| 186 | * @param args | |
| 187 | */ | |
| 188 | public static void main(String[] args) {
| |
| 189 | //Main bb = new Main(); | |
| 190 | MainFrame mf = new MainFrame(new Main(),Width, Height); | |
| 191 | } | |
| 192 | ||
| 193 | /******************************************************************************/ | |
| 194 | ||
| 195 | class CollisionDetector extends Behavior {
| |
| 196 | private final Color3f highlightColor = new Color3f(0.0f, 1.0f, 0.0f); | |
| 197 | ||
| 198 | private final ColoringAttributes highlight = new ColoringAttributes( | |
| 199 | highlightColor, ColoringAttributes.SHADE_GOURAUD); | |
| 200 | ||
| 201 | private Sphere sferka; | |
| 202 | ||
| 203 | private ColoringAttributes shapeColoring; | |
| 204 | ||
| 205 | private Appearance shapeAppearance; | |
| 206 | ||
| 207 | private WakeupOnCollisionEntry wEnter; | |
| 208 | ||
| 209 | private WakeupOnCollisionExit wExit; | |
| 210 | ||
| 211 | public CollisionDetector(Sphere s) {
| |
| 212 | sferka = s; | |
| 213 | shapeAppearance = sferka.getAppearance(); | |
| 214 | shapeColoring = shapeAppearance.getColoringAttributes(); | |
| 215 | inCollision = false; | |
| 216 | } | |
| 217 | ||
| 218 | public void initialize() {
| |
| 219 | wEnter = new WakeupOnCollisionEntry(sferka); | |
| 220 | wExit = new WakeupOnCollisionExit(sferka); | |
| 221 | wakeupOn(wEnter); | |
| 222 | } | |
| 223 | ||
| 224 | public void processStimulus(Enumeration criteria) {
| |
| 225 | inCollision = !inCollision; | |
| 226 | ||
| 227 | if (inCollision) {
| |
| 228 | shapeAppearance.setColoringAttributes(highlight); | |
| 229 | wakeupOn(wExit); | |
| 230 | } else {
| |
| 231 | shapeAppearance.setColoringAttributes(shapeColoring); | |
| 232 | wakeupOn(wEnter); | |
| 233 | } | |
| 234 | } | |
| 235 | } | |
| 236 | ||
| 237 | /******************************************************************************/ | |
| 238 | ||
| 239 | public static float round (double f) | |
| 240 | ||
| 241 | { float temp = (float)(f*(Math.pow(10, 0)));
| |
| 242 | ||
| 243 | temp = (Math.round(temp)); | |
| 244 | ||
| 245 | temp = temp/(int)(Math.pow(10, 0)); | |
| 246 | ||
| 247 | return temp; | |
| 248 | ||
| 249 | } | |
| 250 | /** | |
| 251 | * Funkcja wychwytuje zdarzenie wciśniecią przycisku na klawiaturze. | |
| 252 | * Zawiera obsługę przycisków służących do manipulacji robotem. | |
| 253 | * UP, DOWN - przesuw ramienia w pionie | |
| 254 | * LEFT, RIGHT - obrót ramienia wokół jego osi pionowej | |
| 255 | * A, S - przesuw ramienia w poziomie (wysuwanie, wsuwanie) | |
| 256 | * Z - standardowe ustawienie kamery | |
| 257 | * @param e (KeyEvent) | |
| 258 | */ | |
| 259 | @Override | |
| 260 | public void keyPressed(KeyEvent e) {
| |
| 261 | if(e.getKeyCode() == KeyEvent.VK_Z){
| |
| 262 | vPlatform.setNominalViewingTransform(); | |
| 263 | } | |
| 264 | if (Math.round(Math.toDegrees(x))==360) x=Math.toRadians(0); | |
| 265 | if (Math.toDegrees(x)==-1) x=Math.toRadians(358.9999999999974); | |
| 266 | switch(e.getKeyCode()){
| |
| 267 | case KeyEvent.VK_UP: | |
| 268 | if(y < 0.65f) {
| |
| 269 | y += 0.05; | |
| 270 | } | |
| 271 | break; | |
| 272 | case KeyEvent.VK_DOWN: | |
| 273 | if(y > -0.6f) {
| |
| 274 | y -= 0.05; | |
| 275 | } | |
| 276 | break; | |
| 277 | case KeyEvent.VK_LEFT: | |
| 278 | x -= Math.PI/180; | |
| 279 | break; | |
| 280 | case KeyEvent.VK_RIGHT: | |
| 281 | x += Math.PI/180; | |
| 282 | break; | |
| 283 | case KeyEvent.VK_A: | |
| 284 | if(z > -0.3f) {
| |
| 285 | z -= 0.05; | |
| 286 | } | |
| 287 | break; | |
| 288 | case KeyEvent.VK_S: | |
| 289 | if(z < 0.5f) {
| |
| 290 | z += 0.05; | |
| 291 | } | |
| 292 | break; | |
| 293 | case KeyEvent.VK_SPACE: | |
| 294 | spacja = !spacja; | |
| 295 | zatrzask = true; | |
| 296 | } | |
| 297 | } | |
| 298 | ||
| 299 | /** | |
| 300 | * Funkcja wychwytująca puszczenie przycisku klawiatury - nie używana. | |
| 301 | * @param e | |
| 302 | */ | |
| 303 | @Override | |
| 304 | public void keyReleased(KeyEvent e){
| |
| 305 | switch(e.getKeyCode()){
| |
| 306 | case KeyEvent.VK_SPACE: | |
| 307 | zatrzask = false; | |
| 308 | break; | |
| 309 | } | |
| 310 | } | |
| 311 | ||
| 312 | /** | |
| 313 | * Funkcja wychwytująca sygnał przycisku klawiatury - nie używana. | |
| 314 | * @param e | |
| 315 | */ | |
| 316 | @Override | |
| 317 | public void keyTyped(KeyEvent e){
| |
| 318 | } | |
| 319 | ||
| 320 | ||
| 321 | // @Override | |
| 322 | // public void mouseMoved(MouseEvent e) {
| |
| 323 | // // x = (float)(e.getX())/Width-.5f; | |
| 324 | // //y = -(float)(e.getY())/Height+.5f; | |
| 325 | // } | |
| 326 | ||
| 327 | /** | |
| 328 | * Klasa dziedzicząca po klasie TimerTask. | |
| 329 | * Zawiera funkcję run(). | |
| 330 | */ | |
| 331 | private class Ruch extends TimerTask{
| |
| 332 | /** | |
| 333 | * Zadaniem funkcji jest przekształcanie | |
| 334 | * sceny w zależności od parametrów (zmienne globalne) oraz wyświetlanie | |
| 335 | * informacji o aktualnych wartościach parametrów w dolnym pasku | |
| 336 | * informacyjnym. | |
| 337 | */ | |
| 338 | @Override | |
| 339 | public void run() {
| |
| 340 | hujRotation.rotY(x); | |
| 341 | ||
| 342 | trans2.rotX(Math.PI/2); | |
| 343 | trans2.setTranslation(new Vector3d(0.0f, y, z)); | |
| 344 | trans2.mul(hujRotation, trans2); | |
| 345 | objTrans2.setTransform(trans2); | |
| 346 | ||
| 347 | - | trans3.setTranslation(new Vector3d(0.0f,y+0.6f,z)); |
| 347 | + | trans3.setTranslation(new Vector3d(0.0f,y+0.25,z+0.6)); |
| 348 | trans3.mul(hujRotation, trans3); | |
| 349 | objTrans3.setTransform(trans3); | |
| 350 | ||
| 351 | - | tempTransform.setTranslation(new Vector3d(0.0f, y+0.6f, z)); |
| 351 | + | tempTransform.setTranslation(new Vector3d(0.0f, y+0.25, z-0.4)); |
| 352 | ||
| 353 | trans4.setTranslation(new Vector3d(0.0f, 0.0f, 1.0f)); | |
| 354 | trans4.mul(hujRotation, trans4); | |
| 355 | trans4.mul(tempTransform, trans4); | |
| 356 | if (spacja==true && inCollision==true) {
| |
| 357 | objTrans4.setTransform(trans4); | |
| 358 | } | |
| 359 | label.setText("Współrzędne: " + Math.round(Math.toDegrees(x)) + " , "+ y + " , " + z);
| |
| 360 | } | |
| 361 | } | |
| 362 | ||
| 363 | /** | |
| 364 | * Klasa 'podloga' dziedzicząca po klasie 'Shape3D'. | |
| 365 | */ | |
| 366 | public class podloga extends Shape3D{ // podłoże robota
| |
| 367 | final private Point3f A= new Point3f(-5.0f, -0.5f, -5.0f); | |
| 368 | final private Point3f B= new Point3f(-5.0f, -0.5f, 5.0f); | |
| 369 | final private Point3f C= new Point3f(5.0f, -0.5f, 5.0f); | |
| 370 | final private Point3f D= new Point3f(5.0f, -0.5f, -5.0f); | |
| 371 | final private Point3f[] pts = new Point3f[8]; | |
| 372 | int[] stripCounts= new int[2]; | |
| 373 | int[] contourCount=new int[2]; | |
| 374 | ||
| 375 | /** | |
| 376 | * Konstruktor bezparametrowy klasy 'podloga'. Tworzy punkt odniesienia | |
| 377 | * dla manipulatora. | |
| 378 | */ | |
| 379 | public podloga(){
| |
| 380 | // front | |
| 381 | pts[0]=C; | |
| 382 | pts[1]=D; | |
| 383 | pts[2]=A; | |
| 384 | pts[3]=B; | |
| 385 | //back | |
| 386 | pts[4]=C; | |
| 387 | pts[5]=B; | |
| 388 | pts[6]=A; | |
| 389 | pts[7]=D; | |
| 390 | ||
| 391 | stripCounts[0]=4; | |
| 392 | stripCounts[1]=4; | |
| 393 | contourCount[0]=1; | |
| 394 | contourCount[1]=1; | |
| 395 | GeometryInfo gInf = new GeometryInfo(GeometryInfo.POLYGON_ARRAY); | |
| 396 | gInf.setCoordinates(pts); | |
| 397 | gInf.setStripCounts(stripCounts); | |
| 398 | gInf.setContourCounts(contourCount); | |
| 399 | NormalGenerator ng= new NormalGenerator(); | |
| 400 | ng.setCreaseAngle ((float) Math.toRadians(30)); | |
| 401 | ng.generateNormals(gInf); | |
| 402 | this.setGeometry(gInf.getGeometryArray()); | |
| 403 | } | |
| 404 | } | |
| 405 | ||
| 406 | } |