[Solved] Pie chart

I need to create a pie chart on a cylinder and give different colors to the pies. But I’m not able to find any methodology to cut a cylinder vertically in jme3. Can any one please help me in this regard?

Cheers,

Siddu

Hi Moe,



Thanks for the reply. I’m a newbie to this gaming world and I couldn’t quite understand the code you’ve given. I tried using your code but it is simply making the cylinder area black. Could you please give me some more code snippets of it? Also, my requirement is that, “I have an open cylinder and on the peripheral of the cylinder I have to attach different colored square pieces”.



Cheers,

Siddu

Hi siddu,



i did something like that a few weeks ago. You maybe could build a texture using the awt BufferedImage. Create a graphics object and paint different colored rectangles horizontally. Then convert the BufferedImage into a JME texture and apply it with unshaded or lighting material to the cylinder. I’m not sure but i think that you have to play around a little bit with the top and bottom texture coordinates from the cylinder.



Code for generating a Texture2D from a BufferedImage:

[java]

BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);

// here you could paint to the BufferedImage graphics object

AWTLoader loader = new AWTLoader();

Texture tex = new Texture2D(loader.load(img, false));

// now you have to apply the texture to a material.

[/java]



Regards

Moe

1 Like

Please dont use the developer forum for this kind of help request, moved the thread.

Ok, let’s start from the beginning.

I coded a simple example on the fly. Not sure if it works but i hope so.

[java]

// build geometry object

Geometry cylinder = new Geometry(“Pie-Chart”, new Cylinder(4, 21, 0.5f, 1f));

// generate BufferedImage and graphics object

BufferedImage img = new BufferedImage(99, 100, BufferedImage.TYPE_INT_ARGB);

Graphics2D g2d = (Graphics2D) img.createGraphics();

// paint the pie chart areas

g2d.setColor(Color.RED);

g2d.fillRect(0, 0, 33, 100);

g2d.setColor(Color.YELLOW);

g2d.fillRect(33, 0, 33, 100);

g2d.setColor(Color.GREEN);

g2d.fillRect(66, 0, 33, 100);

// convert BufferedImage into jme texture

AWTLoader awtLoader = new AWTLoader();

Texture2D pieTexture = new Texture2D(awtLoader.load(img, false));

// apply texture to a material (could also be Lighting.j3md)

Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

mat.setTexture(“ColorMap”, pieTexture);

cylinder.setMaterial(mat);

// scale cylinder

cylinder.setLocalScale(10, 10, 10);

[/java]

If you’ve done so far you’ll see that the top and bottom of the cylinder won’t be right. To fix that you could change the texture corrdinates, build your one cylinder mesh or apply a quad with an extra texture at the top and bottom using the fillArc-method in the graphics object.

For understanding the engine, using material, generating meshes and geometries have a look at the tutorials at

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3#tutorials_for_beginners

Regards

Moe

1 Like

Hi Moe,

The code provided is working fine and it fits my requirement as well. Thanks for your help. :slight_smile:



Cheers,

Siddu

Hi,



no problem :wink: You could maybe put a “[SOLVED]” before the topics heading.



Regards

Moe