我在使用Java2D时遇到了奇怪的现象.我知道sun.java2d.opengl VM参数可以为2D启用3D加速,但即使使用它也有一些奇怪的问题.
以下是我运行的测试结果:
在JComponent
Image 1 = .bmp格式上绘制具有32x32像素图块的25x18地图,图像2 = A .png格式
120 FPS使用.BMP图像1
13 FPS使用.PNG图像2
12 FPS使用.BN图像1
700 FPS使用.PNG图像2
如果没有加速,我假设每次使用drawImage()我都会在软件中进行某种转换,并且在.PNG的情况下大大降低了FPS.但是,为什么加速时,结果会切换(而PNG实际上表现得更快)?!疯狂!
.BMP图像1被转换为TYPE_INT_RGB的图像类型..PNG图像2被转换为TYPE_CUSTOM的图像类型.为了在有和没有opengl加速的情况下获得一致的速度,我必须创建一个图像类型为TYPE_INT_ARGB的新BufferedImage,并将Image 1或Image 2绘制到这个新图像.
以下是运行的结果:
120 FPS使用.BMP图像1
120 FPS使用.PNG图像2
700 FPS使用.BN图像1
700 FPS使用.PNG图像2
我真正的问题是,我可以假设TYPE_INT_ARGB将是所有系统和平台的本机图像类型吗?我假设这个值可能不同.有没有办法让我获得原生值,以便我总能创建新的BufferedImages以获得最佳性能?
提前致谢...
Con*_*sty 59
我想我通过研究太多谷歌搜索并将点点滴滴放在一起找到了解决方案.
在这里,评论和所有:
private BufferedImage toCompatibleImage(BufferedImage image)
{
// obtain the current system graphical settings
GraphicsConfiguration gfxConfig = GraphicsEnvironment.
getLocalGraphicsEnvironment().getDefaultScreenDevice().
getDefaultConfiguration();
/*
* if image is already compatible and optimized for current system
* settings, simply return it
*/
if (image.getColorModel().equals(gfxConfig.getColorModel()))
return image;
// image is not optimized, so create a new image that is
BufferedImage newImage = gfxConfig.createCompatibleImage(
image.getWidth(), image.getHeight(), image.getTransparency());
// get the graphics context of the new image to draw the old image on
Graphics2D g2d = newImage.createGraphics();
// actually draw the image and dispose of context no longer needed
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
// return the new optimized image
return newImage;
}
Run Code Online (Sandbox Code Playgroud)
在我之前的文章中,GraphicsConfiguration保存了在系统上创建优化图像所需的信息.它似乎工作得很好,但我会认为Java会自动为你做这件事.显然你对Java不太满意.:)我想我最终回答了自己的问题.哦,好吧,希望它能帮助你们中的一些人,我曾经尝试过将Java用于2D游戏.
嗯,这是旧帖子,但我想分享我关于使用 Swing/AWT 直接绘制而不使用 BufferedImage 的发现。
直接绘制到int[]缓冲区时,可以更好地完成某种绘图,如 3D 。完成图像后,您可以使用ImageProducer实例(如MemoryImageSource)来生成图像。我假设您知道如何在没有 Graphics/Graphics2 帮助的情况下直接执行绘图。
/**
* How to use MemoryImageSource to render images on JPanel
* Example by A.Borges (2015)
*/
public class MyCanvas extends JPanel implements Runnable {
public int pixel[];
public int width;
public int height;
private Image imageBuffer;
private MemoryImageSource mImageProducer;
private ColorModel cm;
private Thread thread;
public MyCanvas() {
super(true);
thread = new Thread(this, "MyCanvas Thread");
}
/**
* Call it after been visible and after resizes.
*/
public void init(){
cm = getCompatibleColorModel();
width = getWidth();
height = getHeight();
int screenSize = width * height;
if(pixel == null || pixel.length < screenSize){
pixel = new int[screenSize];
}
mImageProducer = new MemoryImageSource(width, height, cm, pixel,0, width);
mImageProducer.setAnimated(true);
mImageProducer.setFullBufferUpdates(true);
imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer);
if(thread.isInterrupted() || !thread.isAlive()){
thread.start();
}
}
/**
* Do your draws in here !!
* pixel is your canvas!
*/
public /* abstract */ void render(){
// rubisch draw
int[] p = pixel; // this avoid crash when resizing
if(p.length != width * height) return;
for(int x=0; x < width; x++){
for(int y=0; y<height; y++){
int color = (((x + i) % 255) & 0xFF) << 16; //red
color |= (((y + j) % 255) & 0xFF) << 8; //green
color |= (((y/2 + x/2 - j) % 255) & 0xFF) ; //blue
p[ x + y * width] = color;
}
}
i += 1;
j += 1;
}
private int i=1,j=256;
@Override
public void run() {
while (true) {
// request a JPanel re-drawing
repaint();
try {Thread.sleep(5);} catch (InterruptedException e) {}
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// perform draws on pixels
render();
// ask ImageProducer to update image
mImageProducer.newPixels();
// draw it on panel
g.drawImage(this.imageBuffer, 0, 0, this);
}
/**
* Overrides ImageObserver.imageUpdate.
* Always return true, assuming that imageBuffer is ready to go when called
*/
@Override
public boolean imageUpdate(Image image, int a, int b, int c, int d, int e) {
return true;
}
}// end class
Run Code Online (Sandbox Code Playgroud)
请注意,我们需要MemoryImageSource和Image 的唯一实例。不要为每个帧创建新的 Image 或新的 ImageProducer,除非您已经调整了 JPanel 的大小。参见上面的init()方法。
在渲染线程中,询问repaint()。在 Swing 上,repaint()将调用重写的paintComponent(),在那里它调用您的render()方法,然后要求您的 imageProducer 更新图像。完成 Image 后,使用Graphics.drawImage()绘制它。
要获得兼容的 Image,请在创建Image时使用适当的ColorModel。我使用GraphicsConfiguration.getColorModel():
/**
* Get Best Color model available for current screen.
* @return color model
*/
protected static ColorModel getCompatibleColorModel(){
GraphicsConfiguration gfx_config = GraphicsEnvironment.
getLocalGraphicsEnvironment().getDefaultScreenDevice().
getDefaultConfiguration();
return gfx_config.getColorModel();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24417 次 |
| 最近记录: |