-
Notifications
You must be signed in to change notification settings - Fork 0
Graphics
Most graphic functionality is accessed through the Graphics class, an instance of which is passed to the Draw() method in your GarnetApp derived application.
A simple example which draws an entire texture object at position 100,100 to the display:
@Override
public void draw(Graphics g) {
g.drawImage(texture, 100,100);
}To load an image in order to display sprites and graphics, call the method Texture.loadTexture passing in the name of the .png format file.
After loading the Texture, register it with the graphics system to enable it for drawing by calling the addTexture method of Graphics.
@Override
public void init(Garnet garnet) {
texture = Texture.loadTexture("garnetCrystal.png");
garnet.getGraphics().addTexture(texture);
}To draw a Texture to the Display, there are several drawImage methods made available in the Graphics class.
The simplest drawImage method draws an entire image Texture to the Display at the specified position:
public void drawImage(Texture texture, int x, int y)To specify the texture source coordinates and destination coordinates directly, the following version can be used:
public void drawImage(Texture texture, float[] vertexCoords, float[] texCoords)The TileSheet class is a convenient way to refer to multiple smaller image areas from a larger Texture. You start with an image that has each sprite or tile laid out in a regular grid, and then create the TileSheet based on that. The TileSheet can then be used to supply sub images based on the column and row coordinates of the sub image inside the larger image.
To create one, you need to already have created the Texture object that will be the source of the pixel data.
Texture spritesTexture = Texture.loadTexture(spritesFileName);
graphics.addTexture(spritesTexture);
TileSheet spritesTilesheet = new TileSheet(spritesTexture, 16, 16);The TileSheet constructor takes 3 parameters, the first is the Texture object containing image data, and the other two represent the width and height of the tiles in the image. Sub images in the TileSheet are specified by the grid coordinates of each tile.
Note that the Texture still needs to be registered with the Graphics class.
There are several primitive (lines, circles etc) drawing methods in the Graphics class.
public void drawLine(float x1, float y1, float x2, float y2)
public void drawRect(float x, float y, float w, float h)
public void filledRect(float _x, float _y, float _w, float _h)
public void drawCircle(float x, float y, float w, float h)
public void filledCircle(float x, float y, float w, float h)