专用拉丝表面的Java? [英] Dedicated drawing surface in Java?
问题描述
我想要做高效的2D工程图中的Java。我想有某种表面,我可以自由绘制,而不必让视图层次遍历和更新,这可能会导致口吃。
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在第一,被称为重绘(),但我觉得它不是最优(而这是我问的原因)。我合作过的最接近的是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
?
推荐答案
如果你不需要图形加速,你可以画到的BufferedImage
与的Graphics2D
。一旦你有你的数据在的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屋!