Java中的专用绘图表面? [英] Dedicated drawing surface in Java?

查看:17
本文介绍了Java中的专用绘图表面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Java 进行高效的 2D 绘图.我想要某种可以自由绘制的表面,而不必让视图层次结构遍历和更新,这可能会导致卡顿.

I want to do efficient 2D drawing in Java. I would like to have some kind of surface which I may draw freely without having to let the view hierarchy traverse and update, which may cause stutter.

我一开始使用了 JPanel 并调用了 repaint() 但我发现它不是最佳的(这就是我问的原因).我使用过的最接近的是 Android 的 SurfaceView 它给了我一个专用的绘图表面.

I have used a JPanel at first and called repaint() but I find it not to be optimal (and it is the reason I ask). The closest thing I have worked with is the Android's SurfaceView and it gives me a dedicated drawing surface.

要实现这个专用的绘图表面,我需要使用 OpenGL 还是有任何等效的 SurfaceView?

To achieve this dedicated drawing surface, do I need to use OpenGL or is there any equivalent SurfaceView?

推荐答案

如果您不需要加速图形,您可以使用 Graphics2DBufferedImage 上绘制.在 BufferedImage 中有数据后,您可以简单地将 BufferedImage 绘制到组件上.这将避免您正在谈论的任何类型的闪烁.

If you don't need Accelerated Graphics, you can draw onto a BufferedImage with Graphics2D. Once you have your data in the BufferedImage, you can simply paint the BufferedImage onto the component. This will avoid any sort of flickering you are talking about.

创建 BufferedImage 很容易:

Creating the BufferedImage is easy:

int w = 800;
int h = 600;
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

然后您可以使用图形上下文(可能在您自己的渲染函数中)在其上绘制对象:

Then you can draw objects onto it with a graphics context (Possibly in your own render function):

Graphics2D g = bi.createGraphics();
g.drawImage(img, 0, 0, null);
//img could be your sprites, or whatever you'd like
g.dipose();//Courtesy of MadProgrammer
//You own this graphics context, therefore you should dispose of it.

然后当你重绘你的组件时,你将 BufferedImage 作为一个整体绘制到它上面:

Then when you repaint your component, you draw the BufferedImage onto it as one piece:

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(bi, 0, 0, null);
}

这有点像使用 BufferedImage 作为后台缓冲区,然后在完成绘制后,将其重新绘制到组件上.

Its sort of like using BufferedImage as a back buffer, and then once you are done drawing to it, you repaint it onto the component.

这篇关于Java中的专用绘图表面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆