Author Topic: Java 2D Behind JPCT  (Read 6326 times)

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Java 2D Behind JPCT
« 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?

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Java 2D Behind JPCT
« Reply #1 on: March 28, 2008, 11:20:27 pm »

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Java 2D Behind JPCT
« Reply #2 on: March 28, 2008, 11:30:35 pm »
Thanks a lot, buddy. I'll have a look.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Java 2D Behind JPCT
« Reply #3 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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Java 2D Behind JPCT
« Reply #4 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?

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Java 2D Behind JPCT
« Reply #5 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?

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Java 2D Behind JPCT
« Reply #6 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);
    }

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Java 2D Behind JPCT
« Reply #7 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?

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Java 2D Behind JPCT
« Reply #8 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.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Java 2D Behind JPCT
« Reply #9 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.