David,
> g.setComposite(AlphaComposite.Src);
it is not needed for J3D1.2 fcs, it was proposed as a workaround
for J3D1.2 beta.
Attached is an example of the animated Graphics2D which revealed
the problem in J3D1.2 beta but is fixed in J3D1.2 fcs release.
Regards, Jean
Java 3D Team
Sun Microsystems
>Date: Fri, 26 May 2000 18:06:58 +0200
>From: David Hall� <[EMAIL PROTECTED]>
>Subject: Re: [JAVA3D] Graphics2D operations. Searching an example ?
>To: [EMAIL PROTECTED]
>
>Hi,
>
>You have to derived from Canvas3D and override the postRender() method
>and do
>your drawing operations in it.
>
> public void postRender() {
> super.postRender();
> Rectangle2D shape = new Rectangle2D.Double((getWidth() - 70.0)/2.0,
>(getHeight() - 70.0)/2.0, 70.0, 70.0);
> J3DGraphics2D g = getGraphics2D();
> g.draw(shape);
> g.setComposite(AlphaComposite.Src);
> g.flush(true);
> }
>
>David.
>
>Patricio Inostroza wrote:
>
>> Hi,
>>
>> Somebody have an example and/or some hints of the Graphics2D
>> operations in Java3D?
>>
>> something like:
>>
>> ...
>> Canvas3D c = new Canvas3D (config);
>> ....
>> J3DGraphics2D g2d = canvas.getGraphics2D();
>> ...
>>
>> // and now I want to draw in 2D... but, how ? where ?
>>
>> Thank you
>>
>> Pato I.
>> [EMAIL PROTECTED]
>>
import java.applet.Applet;
import java.awt.*;
import java.awt.AlphaComposite;
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.image.BufferedImage;
import java.awt.font.TextLayout;
import java.awt.font.FontRenderContext;
import java.awt.geom.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Arc2D;
import java.awt.geom.AffineTransform;
import javax.swing.*;
/**
* Pure immediate mode example program. In pure immediate mode, the
* renderer must be stopped on the Canvas being rendered into. In our
* example, this is done immediately after the canvas is created. A
* separate thread is started up to do the immediate mode rendering.
*/
/**
* The Sample class demonstrates the three closure types for arcs,
* Arc2D Open, Chord & Pie.
*/
public class BezierAnim extends Applet implements Runnable {
private MyCanvas3D canvas;
private Thread t = null;
private GraphicsContext3D gc = null;
private J3DGraphics2D g2 = null;
private Geometry cube = null;
private long startTime = 0L;
private Transform3D cmt = new Transform3D();
private Dimension d = new Dimension(0, 0);
// 1/6 revolution (60 degrees) of rotation per second
private static final double rotationPerSecond = (2.0 * Math.PI) / 6.0;
RenderingAttributes renderingAttributes = new RenderingAttributes();
Appearance appearance = new Appearance();
private Thread thread;
private BufferedImage bimg;
private static final int NUMPTS = 6;
// solid line stoke
protected BasicStroke solid = new BasicStroke(10.0f,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
// dashed line stroke
protected BasicStroke dashed = new BasicStroke(10.0f,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 10, new float[] {5}, 0)
;
private float animpts[] = new float[NUMPTS * 2];
private float deltas[] = new float[NUMPTS * 2];
protected Paint fillPaint, drawPaint;
// indicates whether or not to fill shape
protected boolean doFill = true;
// indicates whether or not to draw shape
protected boolean doDraw = true;
protected GradientPaint gradient;
protected BasicStroke stroke;
private Composite saveAC = null;
/*
* generates random points with the specified surface width
* and height for the path
*/
public void reset(int w, int h) {
for (int i = 0; i < animpts.length; i += 2) {
animpts[i + 0] = (float) (Math.random() * w);
animpts[i + 1] = (float) (Math.random() * h);
deltas[i + 0] = (float) (Math.random() * 6.0 + 4.0);
deltas[i + 1] = (float) (Math.random() * 6.0 + 4.0);
if (animpts[i + 0] > w / 2.0f) {
deltas[i + 0] = -deltas[i + 0];
}
if (animpts[i + 1] > h / 2.0f) {
deltas[i + 1] = -deltas[i + 1];
}
}
gradient = new GradientPaint(0,0,Color.red,w*.7f,h*.7f,Color.yellow)
;
}
// generates new points for the path
public void animate(float[] pts, float[] deltas, int i, int limit) {
float newpt = pts[i] + deltas[i];
if (newpt <= 0) {
newpt = -newpt;
deltas[i] = (float) (Math.random() * 4.0 + 2.0);
} else if (newpt >= (float) limit) {
newpt = 2.0f * limit - newpt;
deltas[i] = - (float) (Math.random() * 4.0 + 2.0);
}
pts[i] = newpt;
}
// calls animate for every point in animpts
public void step(int w, int h) {
for (int i = 0; i < animpts.length; i += 2) {
animate(animpts, deltas, i + 0, w);
animate(animpts, deltas, i + 1, h);
}
}
// sets the points of the path and draws and fills the path
public void drawDemo(int w, int h, Graphics2D g2) {
float[] ctrlpts = animpts;
int len = ctrlpts.length;
float prevx = ctrlpts[len - 2];
float prevy = ctrlpts[len - 1];
float curx = ctrlpts[0];
float cury = ctrlpts[1];
float midx = (curx + prevx) / 2.0f;
float midy = (cury + prevy) / 2.0f;
g2.setColor(new Color(0.0f, 0.0f, 0.0f, 0.0f));
g2.fillRect( 0, 0, w, h);
GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO);
gp.moveTo(midx, midy);
for (int i = 2; i <= ctrlpts.length; i += 2) {
float x1 = (midx + curx) / 2.0f;
float y1 = (midy + cury) / 2.0f;
prevx = curx;
prevy = cury;
if (i < ctrlpts.length) {
curx = ctrlpts[i + 0];
cury = ctrlpts[i + 1];
} else {
curx = ctrlpts[0];
cury = ctrlpts[1];
}
midx = (curx + prevx) / 2.0f;
midy = (cury + prevy) / 2.0f;
float x2 = (prevx + midx) / 2.0f;
float y2 = (prevy + midy) / 2.0f;
gp.curveTo(x1, y1, x2, y2, midx, midy);
}
gp.closePath();
if (doDraw) {
g2.setPaint(drawPaint);
g2.setStroke(stroke);
g2.draw(gp);
}
if (doFill) {
if (fillPaint instanceof GradientPaint) {
fillPaint = gradient;
} g2.setPaint(fillPaint);
g2.fill(gp);
}
}
//
// Renders a single frame by clearing the canvas, drawing the
// geometry, and swapping the draw and display buffer.
//
public void render() {
if (gc == null) {
// Set up Graphics context
gc = canvas.getGraphicsContext3D();
appearance.setRenderingAttributes(renderingAttributes);
gc.setAppearance(appearance);
// Set up geometry
cube = new ColorCube(0.4).getGeometry();
}
if (g2 == null) {
// Set up Graphics2D
g2 = canvas.getGraphics2D();
canvas.getSize(d);
reset(d.width, d.height);
// The following two methods are unsupported.
// g2.setBackground(getBackground());
// g2.clearRect(0, 0, d.width, d.height);
gradient = new GradientPaint(0,0,Color.red,200,200,Color.yellow);
fillPaint = gradient;
drawPaint = Color.blue;
stroke = solid;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
canvas.getSize(d);
step(d.width, d.height);
drawDemo(d.width, d.height, g2);
g2.dispose();
// Get current system time
long currTime = System.currentTimeMillis();
if (startTime == 0)
startTime = currTime;
// Compute angle of rotation based on elapsed time since startup
long elapsedTime = currTime - startTime;
double angle = (double)elapsedTime / 1000.0 * rotationPerSecond;
cmt.rotY(angle);
// Render the geometry for this frame
gc.clear();
gc.setModelTransform(cmt);
gc.draw(cube);
//g2.setComposite(AlphaComposite.Src);
g2.flush(true);
canvas.swap();
}
//
// Run method for our immediate mode rendering thread.
//
public void run() {
System.out.println("BezierAnim.run: starting main loop");
while (true) {
render();
Thread.yield();
}
}
//
//Applet init routine (called by browser or by MainFrame)
//
public BezierAnim () {
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
canvas = new MyCanvas3D(config);
canvas.stopRenderer();
add("Center", canvas);
// 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();
// Start a new thread that will continuously render
t = new Thread(this);
t.start();
}
//
// The following allows BezierAnim to be run as an application
// as well as an applet
//
public static void main(String[] args) {
new MainFrame(new BezierAnim(), 256, 300);
}
}