libgdx绘制矩形方框、绘制矩形、绘制矩形边框
libgdx绘制矩形方框、绘制矩形、绘制矩形边框
package base;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.ScreenUtils;
import org.junit.jupiter.api.Test;
/**
* @author lingkang
* created by 2023/11/1
* 绘制一个矩形,采用4条线组合
*/
public class TestJuXing2 extends ApplicationAdapter {
@Test
public void test() {
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.setForegroundFPS(30);
config.setTitle("yzcy");
config.setWindowedMode(800, 600);
new Lwjgl3Application(this, config);
}
protected SpriteBatch batch;
private MyRectangle rectangle;
@Override
public void create() {
batch = new SpriteBatch();
rectangle = new MyRectangle(200, 200, Color.RED);
}
@Override
public void render() {
// Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // 清除屏幕
ScreenUtils.clear(Color.GREEN);
batch.begin();
rectangle.draw(batch, 50, 50);
batch.end();
}
/**
* 方案一:采用4条线组合
* <pre>
* MyRectangle rectangle = new MyRectangle(200, 200, Color.RED);
*
*
* ScreenUtils.clear(Color.GREEN);
* batch.begin();
* rectangle.draw(batch, 50, 50);// 绘制
* batch.end();
* </pre>
* <p>
* 绘制顺序如下:<br/>
* <p>
* 1 2 2 2 2
* 1 3
* 1 3
* 1 4 4 4 3
*/
class MyRectangle implements Disposable {
private Texture texture1, texture2, texture3, texture4;
private int lineWidth = 5;
private int width, height;
public MyRectangle(int width, int height, Color color) {
this.width = width;
this.height = height;
Pixmap pixmap1 = new Pixmap(lineWidth, height, Pixmap.Format.RGB888);
pixmap1.drawLine(0, 0, 0, height);
pixmap1.setColor(color);
texture1 = new Texture(pixmap1);
Pixmap pixmap2 = new Pixmap(width, lineWidth, Pixmap.Format.RGB888);
pixmap2.drawLine(0, height, width, height);
pixmap2.setColor(color);
texture2 = new Texture(pixmap2);
Pixmap pixmap3 = new Pixmap(lineWidth, height, Pixmap.Format.RGB888);
pixmap3.drawLine(width, height, width, 0);
pixmap3.setColor(color);
texture3 = new Texture(pixmap3);
Pixmap pixmap4 = new Pixmap(width, lineWidth, Pixmap.Format.RGB888);
pixmap4.drawLine(0, 0, width, 0);
pixmap4.setColor(color);
texture4 = new Texture(pixmap4);
pixmap1.dispose();
pixmap2.dispose();
pixmap3.dispose();
pixmap4.dispose();
}
public void draw(Batch batch, float x, float y) {
batch.draw(texture1, x, y);
batch.draw(texture2, x, y + height - lineWidth);
batch.draw(texture3, x + width, y);
batch.draw(texture4, x, y);
}
@Override
public void dispose() {
texture1.dispose();
texture2.dispose();
texture3.dispose();
texture4.dispose();
}
}
@Override
public void dispose() {
batch.dispose();
rectangle.dispose();
}
}
依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.lingkang</groupId>
<artifactId>yzcy</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<gdx.version>1.12.0</gdx.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx</artifactId>
<version>${gdx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-backend-lwjgl3 -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-backend-lwjgl3</artifactId>
<version>${gdx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-platform -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-platform</artifactId>
<version>${gdx.version}</version>
<classifier>natives-desktop</classifier>
</dependency>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-freetype -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-freetype</artifactId>
<version>${gdx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-freetype-platform -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-freetype-platform</artifactId>
<version>${gdx.version}</version>
<classifier>natives-desktop</classifier>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<!-- 编译后保持方法形参名称不变 -->
<!--<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>-->
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>tencent</id>
<name>tencent</name>
<layout>default</layout>
<url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>nexus1</id>
<name>Nexus1</name>
<layout>default</layout>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>nexus</id>
<name>Nexus</name>
<layout>default</layout>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>aliyunmaven</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>