How do i print a 2d image on the screen?

I need make a nice interface for my game. What should i do to print an image on the screen?

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



And maybe look under User Interface there?!?

thank you, problem solved

sometimes i don`t know where to search :-s :frowning: because I opened a new topic for nothing

Go through all of the tutorials. There is really no reason not to… even if you don’t plan to use a particular piece the surrounding stuff is educational.



Then, when you need to find something specific, use the “Google Custom Search” box in the upper right of this page. It’s the only thing that searches everything.

Btw, just a fast question…Can I draw an filled oval or a rectangle on the HUD?Is there any method like g.fillOval(int x, int y, int width, int height) ?And if yes, then how can I do that:) thank you :slight_smile:

Short answer: no.



Long answer: you’d have to do it with Java2D drawing to a BufferedImage and then transfer that image to a texture.

ahh, tnx for the ideea :slight_smile: i know to do that

or…not…the code from https://wiki.jmonkeyengine.org/legacy/doku.php/jme2:using_java2d_to_create_the_hud_texture?s[]=bufferedimage&s[]=texture

is to old.

I tried to write an image file on HDD and load it from there as Texture but doesn’t correct. I mean it work but only first time, only when i call the refresh(float) method first time…it make the image from HDD correct but it doesn’t update it on my HUD…i suspect the problem is somewhere in writeImg(float) method



here is some code:

HpEnergyIndicator class

[java]package HUD;



import game.MyGame;



import java.awt.AlphaComposite;

import java.awt.Dimension;

import java.awt.Graphics2D;

import java.awt.geom.Point2D;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.Random;



import javax.imageio.ImageIO;



import auxiliar.Calculator;

import auxiliar.MyPicture;



import com.jme3.math.ColorRGBA;

import com.jme3.scene.Node;



public abstract class HpEnergyIndicator extends Node

{

public final static float DIST_DE_LA_MARGINE_ECRAN=Calculator.calcXPercentFromMax(MyGame.settings.getWidth(), 15);

protected final static Dimension DIM=Calculator.calcSizeFromPercent(new Dimension(MyGame.settings.getHeight(), MyGame.settings.getHeight()),

18.5f, 10);

private ColorRGBA color;

protected MyPicture picture,bgBallPicture;

protected Point2D.Float loc;

public HpEnergyIndicator(ColorRGBA color)

{

this.color=color;

initImg();

bgBallPicture=new MyPicture(“Hp ball indicator img”,loc.x, loc.y, DIM.width, DIM.height, “Textures\Interface\currentHp.png”);

attachChild(picture);

attachChild(bgBallPicture);

}

protected abstract void initImg();

private void writeImg(float percent)

{

File destFile=new File(“assets\Textures\Interface\current”+name+".png");

Graphics2D g2d;

BufferedImage imgSrc, imgDest;

try {

imgSrc=ImageIO.read(new File(“assets\Textures\Interface"+name+“Cerc.png”));

imgDest=new BufferedImage(imgSrc.getWidth(), imgSrc.getHeight(), BufferedImage.TYPE_INT_ARGB);

g2d=imgDest.createGraphics();

g2d.drawImage(imgSrc, 0, 0, null);

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OUT, 0));

g2d.fillRect(0, 0, imgDest.getWidth(), imgDest.getHeight()-(int) Calculator.calcXPercentFromMax(imgDest.getHeight(), percent));



if(destFile.exists())

destFile.delete();

ImageIO.write(imgDest, “PNG”, destFile);

g2d.dispose();



} catch (IOException e) {

e.printStackTrace();

}

}

public void refresh(float percent)

{

if(percent>100)

percent=100;

writeImg(percent);

bgBallPicture.setImage(MyGame.assetManager, “Textures\Interface\current”+name+”.png", true);

}

}

[/java]



HpIndicator and EnergyIndicator class[java]package HUD;



import java.awt.geom.Point2D;



import auxiliar.MyPicture;



import com.jme3.math.ColorRGBA;



public class HpIndicator extends HpEnergyIndicator

{

public HpIndicator(ColorRGBA color)

{

super(color);

}

@Override

protected void initImg()

{

name=“Hp”;

picture=new MyPicture(“Hp Indicator img”, DIST_DE_LA_MARGINE_ECRAN, 0, DIM.width, DIM.height, “Textures\Interface\Hp.png”);

loc=new Point2D.Float(DIST_DE_LA_MARGINE_ECRAN, 0);

// refresh(50);

}

}

[/java]



[java]

package HUD;



import java.awt.geom.Point2D;



import game.MyGame;

import auxiliar.MyPicture;



import com.jme3.math.ColorRGBA;



public class EnergyIndicator extends HpEnergyIndicator

{

public EnergyIndicator(ColorRGBA color)

{

super(color);

}

@Override

protected void initImg()

{

name=“Energy”;

picture=new MyPicture(“Energy Indicator img”, MyGame.settings.getWidth()-DIM.width-DIST_DE_LA_MARGINE_ECRAN, 0, DIM.width, DIM.height,

“Textures\Interface\Energy.png”);

loc=new Point2D.Float(MyGame.settings.getWidth()-DIM.width-DIST_DE_LA_MARGINE_ECRAN, 0);

// refresh(75);

}



}

[/java]



HUD class

[java]package HUD;



import game.MyGame;

import auxiliar.MyPicture;



import com.jme3.font.BitmapFont;

import com.jme3.font.BitmapText;

import com.jme3.math.ColorRGBA;

import com.jme3.scene.Node;

import com.jme3.system.AppSettings;



public class HUD extends Node

{

private AppSettings settings;

private BitmapFont guiFont;

private HpEnergyIndicator hpBar, energyBar;

private BitmapText scoreText;

public HUD(AppSettings settings, BitmapFont guiFont)

{

super();

this.settings=settings;

this.guiFont=guiFont;

hpBar=new HpIndicator(ColorRGBA.Red);

energyBar=new EnergyIndicator(ColorRGBA.Blue);

attachChild(hpBar);

attachChild(energyBar);

initScore();

}

private void initScore()

{

scoreText= new BitmapText(guiFont, false);

scoreText.setSize(guiFont.getCharSet().getRenderedSize()); // font size

scoreText.setColor(ColorRGBA.Green); // font color

scoreText.setText("Score: "); // the text

scoreText.setLocalTranslation(scoreText.getLineHeight(), settings.getHeight()-scoreText.getLineHeight(), 0); // position

attachChild(scoreText);

}

public void refreshHpIndicator(float percent)

{

// detachChild(hpBar);

hpBar.refresh(percent);

// attachChild(hpBar);

}

public void refreshEnergyIndicator(float percent)

{

// detachChild(energyBar);

energyBar.refresh(percent);

// attachChild(energyBar);

}

public void refreshScore(int score)

{

scoreText.setText("Score: "+score);

}

}[/java]





game screen shoot





currentEnergy img(the background img)





CercEnergy img (this is the source image from where i make the currentEnergy img-this image will not be posted on the HUD but i use it to make the currentEnergy img)





Energy img(the forground img)





pls help…i need to display how much energy and how much hp player have



thank you :slight_smile:

There are a few utility methods that can convert a java.awt BufferedImage to a JME Image and/or Texture. Just poke around a little.



I think I use the AWT loader: http://hub.jmonkeyengine.org/javadoc/com/jme3/texture/plugins/AWTLoader.html



And just instantiate one to convert my BufferedImage to a JME Image. I don’t know if it’s the best way, though.

and how do i load an Image in a Picture? if i use picture.setImage(AssetManager,String,boolean) I must send a String parameter not an Image one

Put it in a texture.

I can’t look at my code right now… there is some way to get what you want.

thank you again :slight_smile: problem solved :slight_smile:



[java]

private BufferedImage img(float percent)

{

Graphics2D g2d;

BufferedImage imgSrc, imgDest=null;

try {

imgSrc=ImageIO.read(new File("assets\Textures\Interface"+name+"Cerc.png"));

imgDest=new BufferedImage(imgSrc.getWidth(), imgSrc.getHeight(), BufferedImage.TYPE_INT_ARGB);

g2d=imgDest.createGraphics();

g2d.drawImage(imgSrc, 0, 0, null);

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OUT, 0));

g2d.fillRect(0, 0, imgDest.getWidth(), imgDest.getHeight()-(int) Calculator.calcXPercentFromMax(imgDest.getHeight(), percent));



g2d.dispose();



} catch (IOException e) {

e.printStackTrace();

}

return imgDest;

}

public void refresh(float percent)

{

if(percent>100)

percent=100;

Image img=awtLoader.load(img(percent), true);

Texture2D texture=new Texture2D(img);

bgBallPicture.setTexture(MyGame.assetManager, texture, true);

}

[/java]