Hi Saurabh,

   You can use RenderingAttributes
   setRasterOp(RenderingAttributes.ROP_XOR)

to achieve rubber banding effect. Attach is an example.

Please note that ROP_XOR is not support under DirectX
implementation of Java3D currently.

- Kelvin
------------
Java 3D Team
Sun Microsystems Inc.

>X-Unix-From: [EMAIL PROTECTED]  Tue May 22 04:17:19 2001
>MIME-Version: 1.0
>Content-Transfer-Encoding: 7bit
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
>Date: Tue, 22 May 2001 16:59:56 +0530
>From: Saurabh Akhauri <[EMAIL PROTECTED]>
>Subject: [JAVA3D] Rubberbanding on canvas3D
>To: [EMAIL PROTECTED]
>
>hi
>I am trying to draw rectangle on canvas3D
>by dragging the mouse over it, also have a rubber banding
>effect
>
>It works fine on frame but nothing is visible when mouse dragged on
>Canvas3D
>How is that done on canvas3D ?
>
>will highly appreciate any help in this regards
>
>Saurabh
>
>===========================================================================
>To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
>of the message "signoff JAVA3D-INTEREST".  For general help, send email to
>[EMAIL PROTECTED] and include in the body of the message "help".
/***************************************************************
"Copyright (c) 2001 Sun Microsystems, Inc. All Rights Reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

-Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

-Redistribution in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

Neither the name of Sun Microsystems, Inc. or the names of contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.

This software is provided "AS IS," without a warranty of any kind. ALL
EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.

You acknowledge that Software is not designed,licensed or intended for use in
the design, construction, operation or maintenance of any nuclear facility."

****************************************************************************/

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.*;
import java.awt.image.DataBufferInt;
import java.awt.image.BufferedImage;
import java.util.Enumeration;
import java.text.NumberFormat;

/**
 * An example of mixing immediate mode rendering with normal rendering.  The
 * "HelloUniverse" spinning cube is draw until there is a mouse button pressed.
 * Then a rubber-band line is drawn in immediate mode in front of the existing
 * image by making updates to the front buffer.  The previous lines (if any)
 * are "undrawn" by drawing them with the XOR raster op.
 */
public class MixedImmediateFront extends Applet {

    Canvas3D            canvas;

    GraphicsContext3D   gc = null;
    LineArray           line = null;
    Transform3D         sphereTransform = new Transform3D();

    WakeupCriterion[]   mouseEvents;
    WakeupOr            mouseCriterion;
    boolean             rendererRunning = true;

    Point3d             coords[] = new Point3d[2];
    Point3d             baseMouse = new Point3d();
    Point3d             curMouse = new Point3d();
    Transform3D         ipToVworld = new Transform3D();

    Appearance          whiteAppearance;

    // only works in single buffer mode right now
    static final boolean singleBuffer = false;

    public void setBase(int x, int y) {
        canvas.getPixelLocationInImagePlate(x, y, baseMouse);
        canvas.getImagePlateToVworld(ipToVworld);
        ipToVworld.transform(baseMouse);
    }

    public void render(int x, int y) {
        if (line != null) {
            gc.draw(line);
        }

        canvas.getPixelLocationInImagePlate(x, y, curMouse);
        canvas.getImagePlateToVworld(ipToVworld);
        ipToVworld.transform(curMouse);

        line = new LineArray(2, LineArray.COORDINATES);
        coords[0] = baseMouse;
        coords[1] = curMouse;
        line.setCoordinates(0, coords);

        // Render the geometry for this frame
        gc.draw(line);

        // flush to make sure the display is correct
        gc.flush(false);

    }



    //
    //Applet init routine (called by browser or by MainFrame)
    //
    public void init() {
        setLayout(new BorderLayout());

        canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());

        if (singleBuffer) {
            canvas.setDoubleBufferEnable(false);
        }

        add("Center", canvas);

        // set up the graphics context
        gc = canvas.getGraphicsContext3D();
        gc.setBufferOverride(true);

        // set up the appearances
        RenderingAttributes ra = new RenderingAttributes();
        ra.setDepthBufferEnable(false);
        ra.setRasterOpEnable(true);
        ra.setRasterOp(RenderingAttributes.ROP_XOR);
        whiteAppearance = new Appearance();
        whiteAppearance.setRenderingAttributes(ra);

        gc.setAppearance(whiteAppearance);

        // Create a simple scene and attach it to the virtual universe
        SimpleUniverse u = new SimpleUniverse(canvas);

        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        u.getViewingPlatform().setNominalViewingTransform();

        BranchGroup scene = createSceneGraph();
        u.addBranchGraph(scene);

        BranchGroup behaviorRoot = new BranchGroup();
        behaviorRoot.addChild(new Callback());
        u.addBranchGraph(behaviorRoot);
    }


    public class Callback extends Behavior {
        public void initialize() {
            mouseEvents = new WakeupCriterion[3];
            mouseEvents[0] = new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED);
            mouseEvents[1] = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
            mouseEvents[2] = new WakeupOnAWTEvent(MouseEvent.MOUSE_RELEASED);
            mouseCriterion = new WakeupOr(mouseEvents);
            wakeupOn (mouseCriterion);
            setSchedulingBounds(new BoundingSphere(new Point3d(),1000));
        }

        public void processStimulus (Enumeration criteria) {
            WakeupCriterion wakeup;
            AWTEvent[] event;
            int id;
            int dx, dy;

            while (criteria.hasMoreElements()) {
                wakeup = (WakeupCriterion) criteria.nextElement();
                if (wakeup instanceof WakeupOnAWTEvent) {
                    event = ((WakeupOnAWTEvent)wakeup).getAWTEvent();
                    for (int i=0; i<event.length; i++) {
                        MouseEvent mevent = (MouseEvent) event[i];
                        if (!mevent.isMetaDown() &&
                            !mevent.isAltDown()){

                            id = event[i].getID();

                            switch (id) {
                              case MouseEvent.MOUSE_PRESSED:
                                setBase(mevent.getX(), mevent.getY());
                                // no break here on purpose, fall through to
                                // drag case
                              case MouseEvent.MOUSE_DRAGGED:
                                if (rendererRunning) {
                                    canvas.stopRenderer();
                                    rendererRunning = false;
                                    gc.setFrontBufferRendering(true);

                                }
                                render(mevent.getX(), mevent.getY());
                                break;
                              case MouseEvent.MOUSE_RELEASED:
                                //System.out.println("start renderer");
                                if (!singleBuffer) {
                                    gc.setFrontBufferRendering(false);
                                }
                                canvas.startRenderer();
                                rendererRunning = true;

                                // no need to undraw previous anymore
                                line = null;
                                break;
                            }
                        }
                    }
                }
            }
            wakeupOn (mouseCriterion);
        }
    }

    public BranchGroup createSceneGraph() {
        // Create the root of the branch graph
        BranchGroup objRoot = new BranchGroup();

        // Create the transform group node and initialize it to the
        // identity.  Enable the TRANSFORM_WRITE capability so that
        // our behavior code can modify it at runtime.  Add it to the
        // root of the subgraph.
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);

        // Create a simple shape leaf node, add it to the scene graph.
        objTrans.addChild(new ColorCube(0.4));

        // Create a new Behavior object that will perform the desired
        // operation on the specified transform object and add it into
        // the scene graph.
        Transform3D yAxis = new Transform3D();
        Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
                                        0, 0,
                                        4000, 0, 0,
                                        0, 0, 0);

        RotationInterpolator rotator =
            new RotationInterpolator(rotationAlpha, objTrans, yAxis,
                                     0.0f, (float) Math.PI*2.0f);
        BoundingSphere bounds =
            new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
        rotator.setSchedulingBounds(bounds);
        objTrans.addChild(rotator);

        // Have Java 3D perform optimizations on this scene graph.
        objRoot.compile();

        return objRoot;
    }

    //
    // The following allows MixedImmediateFront to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
        new MainFrame(new MixedImmediateFront(), 600, 600);
    }
}

Reply via email to