java 2d 图形,Java 2D游戏图形

本文介绍了一种提高Java 2D图形绘制性能的方法,通过使用BufferStrategy对象和Canvas组件来减少屏幕刷新带来的闪烁问题。作者分享了如何在圣诞假期期间为团队游戏模块练习,并提供了一个示例代码,展示了如何实现代理缓冲以实现稳定60+FPS的游戏性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Next semester we have a module in making Java applications in a team. The requirement of the module is to make a game. Over the Christmas holidays I've been doing a little practice, but I can't figure out the best way to draw graphics.

I'm using the Java Graphics2D object to paint shapes on screen, and calling repaint() 30 times a second, but this flickers terribly. Is there a better way to paint high performance 2D graphics in Java?

解决方案

What you want to do is to create a canvas component with a BufferStrategy and render to that, the code below should show you how that works, I've extracted the code from my self written Engine over here.

Performance solely depends on the stuff you want to draw, my games mostly use images. With around 1500 of them I'm still above 200 FPS at 480x480. And with just 100 images I'm hitting 6k FPS when disabling the frame limiting.

A small game (this one has around 120 images at once at the screen) I've created can be found here (yes the approach below also works fine as an applet.)

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.GraphicsConfiguration;

import java.awt.GraphicsEnvironment;

import java.awt.Toolkit;

import java.awt.Transparency;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.image.BufferStrategy;

import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import javax.swing.WindowConstants;

public class Test extends Thread {

private boolean isRunning = true;

private Canvas canvas;

private BufferStrategy strategy;

private BufferedImage background;

private Graphics2D backgroundGraphics;

private Graphics2D graphics;

private JFrame frame;

private int width = 320;

private int height = 240;

private int scale = 1;

private GraphicsConfiguration config =

GraphicsEnvironment.getLocalGraphicsEnvironment()

.getDefaultScreenDevice()

.getDefaultConfiguration();

// create a hardware accelerated image

public final BufferedImage create(final int width, final int height,

final boolean alpha) {

return config.createCompatibleImage(width, height, alpha

? Transparency.TRANSLUCENT : Transparency.OPAQUE);

}

// Setup

public Test() {

// JFrame

frame = new JFrame();

frame.addWindowListener(new FrameClose());

frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

frame.setSize(width * scale, height * scale);

frame.setVisible(true);

// Canvas

canvas = new Canvas(config);

canvas.setSize(width * scale, height * scale);

frame.add(canvas, 0);

// Background & Buffer

background = create(width, height, false);

canvas.createBufferStrategy(2);

do {

strategy = canvas.getBufferStrategy();

} while (strategy == null);

start();

}

private class FrameClose extends WindowAdapter {

@Override

public void windowClosing(final WindowEvent e) {

isRunning = false;

}

}

// Screen and buffer stuff

private Graphics2D getBuffer() {

if (graphics == null) {

try {

graphics = (Graphics2D) strategy.getDrawGraphics();

} catch (IllegalStateException e) {

return null;

}

}

return graphics;

}

private boolean updateScreen() {

graphics.dispose();

graphics = null;

try {

strategy.show();

Toolkit.getDefaultToolkit().sync();

return (!strategy.contentsLost());

} catch (NullPointerException e) {

return true;

} catch (IllegalStateException e) {

return true;

}

}

public void run() {

backgroundGraphics = (Graphics2D) background.getGraphics();

long fpsWait = (long) (1.0 / 30 * 1000);

main: while (isRunning) {

long renderStart = System.nanoTime();

updateGame();

// Update Graphics

do {

Graphics2D bg = getBuffer();

if (!isRunning) {

break main;

}

renderGame(backgroundGraphics); // this calls your draw method

// thingy

if (scale != 1) {

bg.drawImage(background, 0, 0, width * scale, height

* scale, 0, 0, width, height, null);

} else {

bg.drawImage(background, 0, 0, null);

}

bg.dispose();

} while (!updateScreen());

// Better do some FPS limiting here

long renderTime = (System.nanoTime() - renderStart) / 1000000;

try {

Thread.sleep(Math.max(0, fpsWait - renderTime));

} catch (InterruptedException e) {

Thread.interrupted();

break;

}

renderTime = (System.nanoTime() - renderStart) / 1000000;

}

frame.dispose();

}

public void updateGame() {

// update game logic here

}

public void renderGame(Graphics2D g) {

g.setColor(Color.BLACK);

g.fillRect(0, 0, width, height);

}

public static void main(final String args[]) {

new Test();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值