preface
This section is present in the second quarter, is introduced in the second section of com. Badlogic. Etf has. Graphics. The pixmap class.
review
To recall the introduction of Section 2:
The Pixmap pixel figure
We can use com. Badlogic. Etf has. The graphics. The Pixmap class to build a simple pixel texture. The constructors commonly used for Pixmap are:
- Pixmap(FileHande) : Pass in a FileHande and read the image into Pixmap
- Pixmap(int,int,Pixmap.Format) :Pass in the image width and height (integers in pixels) and the Format object (image encoding, in pixmap. Format, generally usedFormat.RGBA8888)
At this point the image is still blank, we need to draw a graph in the image:
Set the color of the following graphics:
// Both are set to black
pixmap.setColor(com.badloc.gdx.graphics.Color.BLACK);
pixmap.setColor(0.0.0.1);
Copy the code
Draw a graph with coordinates based on the image (0,0 on the upper left) and the new graph will overwrite the old one:
pixmap.fill(); // Fill the entire image
pixmap.fillCircle(10.10.10); // Draw a (10,10) solid circle with radius 10
pixmap.fillRectangle(30.10.10.20); // draw a (30,10) solid rectangle of size 10 by 20
Pixmap.fillTriangle(0.10.5.0.10.10); // draw a solid triangle (0,10),(5,0),(10,10)
pixmap.drawCircle(10.10.10); // Draw a hollow circle
pixmap.draRectangle(30.10.10.20); // Draw a hollow rectangle
pixmap.drawLine(0.0.100.100); // draw a line from (0,0) to (100,100) (1 pixel)
pixmap.drawPixmap(pixmap2,0.0); // Draw another Pixmap on the image (doll doge)
Copy the code
Pixmap advanced
The review ends and the body begins.
Here is the pixel-based approach:
// Get the pixel color
int color=pixmap.getPixel(pixelX,pixelY);
// Draw a pixel
pixmap.drawPixel(pixelX,pixelY,Color.BLACK);
pixmap.setColor(Color.BLACK);
pixmap.drawPixel(pixelX,pixelY);
Copy the code
Use int to store RGBA colors
The int value returned by pixmap.getPixel () represents the RGBA color.
RGBA colors are represented by four octet binary numbers, a total of 32 bits, whereas Java ints are exactly 32 bits, so using int to represent RGBA colors is a good fit
The constant in the libGDX Color class is of type int, and also uses int to store RGBA colors.
if(color==Color.Black){/ *... * /}
Copy the code
So how do you store/read RGBA colors? Is simple:
// From color to RGBA
public int[] colorToRGBA(int color){
// Alpha is the highest bit
int alpha=color>>>24;
// The rest in order
int r=( color & 0xff0000) > >16 ;
int g=( color & 0xff00) > >8;
int b= color & 0xff ;
int colors[] =new int[4];
/ /... RGBA is assigned to colors in turn, omitted here
return colors;
}
// from RGBA to color
public int RGBAtoColor(int r,int g,int b,int alpna){
// Alpha is the highest bit
int color = (alpha<<24) | (r<<16) | (g<<8) | b;
return color;
}
Copy the code
PixmapIO image flow
Com. Badlogic. Etf has. Graphics. PixmapIO class static method provides a picture of reading and writing:
// Read the file to pixmap
Pixmap pixmap = PixmapIO.readCIM(fileHander);
// Write pixmap to the file
PixmapIO.wirteCIM(fileHander,pixmap);
PixmapIO.writePNG(fileHander,pixmap);
Copy the code
.png to .cim
Note that PixmapIO can only read.cim files, not.png files. But we can check the “Bug” :
Pixmap has a constructor that reads.png files
PixmapIO can save.cim files
We can make a **.png to. Cim tool.
Jar download: Click here, extract code: Wood
Download source code: click here, extract code: Wood
The interface is as follows:
Key source code is as follows:
Pixmap pixmap=new Pixmap(loadFile);
PixmapIO.writeCIM(saveFile, pixmap);
Copy the code
DrawPixmap (new pixMap (PNGpath)) is better 😀
Try: drawing applet
In any case, we now have a rough idea of the advanced use of Pixmap. So next, we will make a drawing applet, it can change the brush size and color, also can save the file
package com.libGDX.test;
import java.awt.Container;
import java.io.File;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.PixmapIO;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
// Apply the main class
public class Test extends ApplicationAdapter {
private SpriteBatch batch;
private Pixmap pixmap; / / drawing board
private Texture texture; // Draw the carrier
private int r; // Brush radius
/* * Save the file dialog box. LibGDX itself does not contain a file selection box, so linkage with Java Swing's file selection box is required
private JFileChooser saveDirecrotyChooser;
@Override
public void create(a) {
batch=new SpriteBatch();
// Create a Pixmap with a screen size of 500 x 500 for the artboard
pixmap=new Pixmap(500.500,Pixmap.Format.RGBA8888);
// White for artboard background
pixmap.setColor(Color.WHITE);
pixmap.fill();
// The default is black brush
pixmap.setColor(Color.BLACK);
// The default brush radius is 5, small
r=5;
// Initialize the texture
texture=new Texture(pixmap);
// Set the input feedback
Gdx.input.setInputProcessor(new InputAdapter() {
// Release the keyboard event
@Override
public boolean keyUp(int keycode) {
// See if you want to change the properties
setColor(keycode);
setSize(keycode);
// If you press S to save the file
if(keycode==Keys.S)save();
return false;
}
// Draw on the artboard as you click and drag
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
draw(screenX,screenY);
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
draw(screenX,screenY);
return false; }});// Initializes the file selection box
saveDirecrotyChooser=new JFileChooser("");
// Set the.png image to store
saveDirecrotyChooser.setAcceptAllFileFilterUsed(false);
saveDirecrotyChooser.addChoosableFileFilter(new FileNameExtensionFilter("PNG"."png"));
}
@Override
public void render(a) {
// White clear screen
Gdx.gl.glClearColor(1.1.1.1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
/ / to draw
batch.draw(texture, 0.0);
batch.end();
}
// Brush drawing
private void draw(int x,int y) {
// The brush is actually a circle
pixmap.fillCircle(x, y, r);
/ / refresh
texture.draw(pixmap, 0.0);
}
// Set the brush color
private void setColor(int keycode) {
switch(keycode) {
case Keys.R:pixmap.setColor(Color.RED);break; / / red
case Keys.G:pixmap.setColor(Color.GREEN);break; / / green
case Keys.B:pixmap.setColor(Color.BLUE);break; / / blue
case Keys.A:pixmap.setColor(Color.BLACK);break; / / black
case Keys.W:pixmap.setColor(Color.WHITE);break; // White, eraser}}// Set the brush size
private void setSize(int keycode) {
switch(keycode) {
case Keys.F1:r=5;break; / / the trumpet
case Keys.F2:r=10;break; / / medium
case Keys.F3:r=20;break; / / large.}}// Save the file
private void save(a) {
// Set the default file name photo year-month-date.png
saveDirecrotyChooser.setSelectedFile(new File("photo-"+String.format("%tF".new Date())+".png"));
// Displays the file save dialog box
int i =saveDirecrotyChooser.showSaveDialog(new Container());
// If you press OK/Save
if(i==JFileChooser.APPROVE_OPTION) {
// Create FileHande with the selected pathFileHandle saveFile=newFileHandle(saveDirecrotyChooser.getSelectedFile().getPath()); PixmapIO.writePNG(saveFile, pixmap); }}@Override
public void dispose(a) { pixmap.dispose(); texture.dispose(); batch.dispose(); }}Copy the code
Running results: