www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: AGP on March 28, 2008, 11:12:44 pm

Title: Java 2D Behind JPCT
Post by: AGP on March 28, 2008, 11:12:44 pm
Is that possible using the software engine? If so, how, because the 2D graphics always seem to come out on top?
Title: Re: Java 2D Behind JPCT
Post by: paulscode on March 28, 2008, 11:20:27 pm
I just so happen to have done this before:
http://www.paulscode.com/source/jPCTSwingMix/ (http://www.paulscode.com/source/jPCTSwingMix/)

You can get the source code at:
http://www.paulscode.com/source/jPCTSwingMix/jPCTSwingMixSource.zip (http://www.paulscode.com/source/jPCTSwingMix/jPCTSwingMixSource.zip)
Title: Re: Java 2D Behind JPCT
Post by: AGP on March 28, 2008, 11:30:35 pm
Thanks a lot, buddy. I'll have a look.
Title: Re: Java 2D Behind JPCT
Post by: AGP on March 29, 2008, 01:01:33 am
After looking at it for twenty minutes I finally saw what I needed: FrameBuffer.getGraphics()! Not once had I used that one before. Thanks again, pal.
Title: Re: Java 2D Behind JPCT
Post by: AGP on March 29, 2008, 03:49:07 am
Okay, you did 2D-3D-2D. That seems easy, in retrospect. But what I need is 3D-2D-3D. I tried making the last 3D object invisible, render and draw, draw 2D, then add that object and make everything else invisible, render and draw, then display(). As I expected, it didn't work (I lost the very first object but the 2D was behind the last object). So now what do I do?
Title: Re: Java 2D Behind JPCT
Post by: paulscode on March 29, 2008, 04:14:43 am
I am not sure.  The logic behind your approach seems sound, but it sounds like the buffer is getting cleared between the first 3D layer and the 2D part.  Any chance I can see the code you are using for the render loop?
Title: Re: Java 2D Behind JPCT
Post by: AGP on March 29, 2008, 05:55:20 am
Sure thing. Thanks in advance.

Code: [Select]
    protected void draw() {
bow.rotateX(rotateX);
bow.rotateZ(rotateZ);
rotateX = 0;
rotateZ = 0;
buffer.clear();

scene.setVisibility(true);
arrow.setVisibility(false);
bow.setVisibility(false);

theWorld.renderScene(buffer);
theWorld.draw(buffer);
// buffer.update();

Graphics2D g2 = (Graphics2D) buffer.getGraphics();
java.awt.image.BufferedImage map = thePanther.getCurrentMap();
Polygon current = thePanther.getCurrent();
g2.setPaint(new TexturePaint(map, current.getBounds()));
g2.fill(current);

scene.setVisibility(false);
arrow.setVisibility(true);
bow.setVisibility(true);
theWorld.renderScene(buffer);
theWorld.draw(buffer);

if (adjustLine)
    bowTopAndBottom();
SimpleVector point1 = Interact2D.project3D2D(mainCamera, buffer, uppermost);
SimpleVector point2 = Interact2D.project3D2D(mainCamera, buffer, lowermost);
SimpleVector arrowPoint = Interact2D.project3D2D(mainCamera, buffer, arrowBack);

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.white);
g2.drawLine((int)point1.x, (int)point1.y, (int)arrowPoint.x, (int)arrowPoint.y);
g2.drawLine((int)arrowPoint.x, (int)arrowPoint.y, (int)point2 .x, (int)point2.y);

buffer.display(g);
    }
Title: Re: Java 2D Behind JPCT
Post by: EgonOlsen on March 29, 2008, 11:44:59 pm
Looks reasonable to me at the first glance. What happens if you clear the buffer with a specific color instead of black. Is the background still in that color after the method has finished drawing everything?
Title: Re: Java 2D Behind JPCT
Post by: AGP on April 02, 2008, 01:03:13 am
Just so the both of you know, the code did work. I appreciate both your help. For the record, the reason I wasn't seeing it work is the fact that I had some leftover code from a previous attempt. Thanks again.
Title: Re: Java 2D Behind JPCT
Post by: paulscode on April 02, 2008, 02:13:57 am
Awesome, that is great.  I don't know if I will ever need the capability of putting a 2D layer between two 3D layers, but it is nice to know it is possible.