After all this time, i finally got around to wanting to add a canvas to the guiNode. After picking through every post I could find, It still took a lot of work and I still have one problem. First, there is a BitmapText node, why is there still not a CanvasNode (what it looks like everyone wants)? So, here is mine. I’ll put in in Contribudions, too. That said, I’m resizing my BufferedImage but the output doesn’t resize. The dump of my BufferedImage says that the size changed, but it still comes out at the old size. It looks like it is scaling 1:2 (going from 200, 200 to 100,100). But, why? I’ll try going from small to big rather than big to small and see if that makes a difference.
Also, FYI, you have to remove comment blocks before posting code here. Otherwise the code gets broken up in segments. (or my browser is broken)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package my.asteroid.nodes;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.material.RenderState;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.post.SceneProcessor;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Line;
import com.jme3.scene.shape.Quad;
import com.jme3.shader.VarType;
import com.jme3.texture.FrameBuffer;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D;
import com.jme3.texture.Texture3D;
import com.jme3.texture.plugins.AWTLoader;
import com.jme3.ui.Picture;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import my.asteroid.utilities.Utilities;
/**
*
* @author Peter
*/
public class CanvasNode extends Node implements SceneProcessor{
//protected Point[] point;\
protected AssetManager assetManager;
protected AWTLoader awtLoader;
protected VarType varType;
protected int imageType;
protected int width = 1;
protected int height = 1;
protected int newWidth = 1;
protected int newHeight = 1;
protected BufferedImage bufferedImage;
protected Picture picture;
protected Geometry geometry;
protected Graphics graphics;
protected Material material;
protected Texture2D texture;
//SceneProcessor
protected Object lock = new Object();
protected RenderManager rm;
protected ViewPort vp;
protected boolean reshapeNeeded = false;
protected int counter = 0;
protected boolean clearBackground = false;
//https://hub.jmonkeyengine.org/t/question-regarding-2d-sprite/36209/2
public CanvasNode(String name, int width, int height, AssetManager assetManager){
this(name, width, height, assetManager, VarType.Texture2D, BufferedImage.TYPE_INT_ARGB);//type_int_argb
}
public CanvasNode(String name, int width, int height, AssetManager assetManager, VarType varType, int imageType){
super(name);
this.width = width;
this.height = height;
this.assetManager = assetManager;
this.awtLoader = awtLoader = new AWTLoader();
this.varType = varType;
this.imageType = imageType;
//
Mesh mesh = new Quad(width, height);
geometry = new Geometry("quad", mesh);
bufferedImage = new BufferedImage(width, height, imageType);//BufferedImage.TYPE_INT_ARGB
graphics = bufferedImage.createGraphics();
//
texture = new Texture2D(awtLoader.load(bufferedImage, true));
material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");//"Common/MatDefs/Misc/Unshaded.j3md"
material.setTexture("ColorMap", texture);
//material.setTextureParam("ColorMap", varType, new Texture2D(awtLoader.load(bufferedImage, true)));
geometry.setMaterial(material);
setImage(bufferedImage, true);
attachChild(geometry);
}
protected final void setImage(BufferedImage bufferedImage, boolean flipY){
//material.setTextureParam("ColorMap", new Texture2D(awtLoader.load(bufferedImage, flipY)));
material.setTexture("ColorMap", new Texture2D(awtLoader.load(bufferedImage, flipY)));
material.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
geometry.setMaterial(material);
}
public void setRGB(Raster raster){
bufferedImage.setData(raster);
}
public void setRGB(int x, int y, int color){
System.out.println("CanvasNode.setRGB(" + x + "," + y + "," + color + " Illegal value");
bufferedImage.setRGB(x, y, color);
}
public void setRGB(int startX, int startY, int[] rgbArray, int offset, int scansize){
boolean ok = true;
bufferedImage.setRGB(startX, startY, width, width, rgbArray, offset, scansize);
}
//SceneProcessor
public void initialize(RenderManager rm, ViewPort vp){
System.out.println("CN.initialize()");
if (this.rm == null){
this.rm = rm;
this.vp = vp;
reshape(new ViewPort("test", new Camera(newWidth, newHeight)), newWidth, newHeight);
}
}
private void reshapeInThread(int newWidth, int newHeight){
System.out.println("CN.reshapeInThread(" + width + "," + height + ", " + newWidth + "," + newHeight + ")");
width = newWidth;
height = newHeight;
bufferedImage = new BufferedImage(width, height, imageType);//BufferedImage.TYPE_INT_ARGB
graphics = bufferedImage.createGraphics();
texture = new Texture2D(awtLoader.load(bufferedImage, true));
material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");//"Common/MatDefs/Misc/Unshaded.j3md"
material.setTexture("ColorMap", texture);
geometry.setMaterial(material);
geometry.updateGeometricState();
geometry.updateLogicalState(60f);
setImage(bufferedImage, true);
}
public boolean isInitialized(){
return(bufferedImage != null);
}
//@Override
public void update(Graphics g){
counter++;
System.out.println("CN.update()" + " width=" + width + ", height=" + height + ", counter=" + counter);
if (clearBackground){
g.setColor(Color.red);
g.fillRect(0, 0, width, height);
}
g.setColor(Color.white);
paint(g);
}
//@Override
public void paint(Graphics g){
System.out.println("CN.paint()" + " width=" + width + ", height=" + height);
System.out.println("CN.paint()" + " bufferedImage=" + bufferedImage);
// Graphics2D g2d = (Graphics2D) g;
synchronized (lock){
g.drawImage(bufferedImage, 0, 0, null);
}
if (counter == 100){
System.out.println("CN.paint() " + " test reshape()");
reshape(new ViewPort("test", new Camera(100, 100)), 100, 100);
}
}
public void preFrame(float tpf){
//System.out.println("CN.preFrame()");
System.out.println("CN.preframe())" + " width=" + width + ", height=" + height);
//graphics.setColor(Color.red);
//graphics.fillRect(0, 0, width, height);
for (int h = 0;h < height;h++){
for (int w = 0;w < width;w++){
bufferedImage.setRGB(h, w, (int)(Math.random() * Integer.MAX_VALUE));
}
}
setImage(bufferedImage, true);
update(graphics);
}
public void postQueue(RenderQueue rq){
System.out.println("CN.postQueue()");
}
public void postFrame(FrameBuffer out){
System.out.println("CN.postFrame()");
if (reshapeNeeded){
reshapeInThread(newWidth, newHeight);
reshapeNeeded = false;
}
}
public void reshape(ViewPort vp, int w, int h){
System.out.println("CN.reshape(" + w + "," + h + ")");
newWidth = w;
newHeight = h;
reshapeNeeded = true;
}
public void cleanup(){
}
}