简体   繁体   English

将Graphics2D绘制到另一个Graphics2D

[英]Draw Graphics2D to another Graphics2D

It is possible to draw from one Graphics2D to another Graphics2D ? 可以从一个Graphics2D绘制到另一个Graphics2D吗?

Let me explain. 让我解释。 I have printing issues, when i display a JTextArea or JTextPanel in screen, internaly its used sun.java2d.SunGraphics2D , but when im printing its used sun.print.PeekGraphics and sun.awt.windows.WPathGraphics . 我有打印问题,当我在屏幕上显示JTextAreaJTextPanel时,内部使用sun.java2d.SunGraphics2D ,但是当我打印其使用的sun.print.PeekGraphicssun.awt.windows.WPathGraphics The problem is with some kind of Fonts, like Arial. 问题出在某些字体上,比如Arial。 In some sizes lines are cut. 在某些尺寸中,线条被切割。 I have tryed a lot of ways to render the text in printing, Graphics2D.drawString , SwingUtilities2.drawString , TextLayout.drawString , but in some cases lines are still cut, or lines are not cut but some kind of justification makes disapear white spaces. 我已经尝试了很多方法来渲染打印中的文本, Graphics2D.drawStringSwingUtilities2.drawStringTextLayout.drawString ,但在某些情况下仍然会剪切线条,或者没有剪切线条,但是某种理由会使消失的白色空间变得消失。

So my idea is try to render components with sun.java2d.SunGraphics2D and "copy" the surface to the printer via sun.print.PeekGraphics or sun.awt.windows.WPathGraphics . 所以我的想法是尝试使用sun.java2d.SunGraphics2D渲染组件,并通过sun.print.PeekGraphicssun.awt.windows.WPathGraphics将表面“复制”到打印机。

Thanks in advance. 提前致谢。

Yes its possible, thats how double buffering is achieved in a lot of Java Games. 是的它可能,这就是如何在许多Java游戏中实现双缓冲。 What you need is the Graphics2D's drawImage() method which takes in another Graphics2D object to draw in. Eg from a small game of mine: 你需要的是Graphics2D的drawImage()方法,它接受另一个Graphics2D对象来绘制。例如,从我的一个小游戏:

   private Main(){
        ...
        /* Create the backbuffer as a BufferedImage object */
        this.doubleBuffer = new BufferedImage(this.WIDTH, this.HEIGHT, BufferedImage.TYPE_INT_RGB);
        /* create a Graphics 2D object to draw INTO this backbuffer */
        this.doubleBufferG2D = (Graphics2D) doubleBuffer.createGraphics();
        ...
    }

Somewhere else: 别的地方:

/*Now lets draw the backbuffer INTO the screen */
g2d.drawImage(doubleBuffer, null , 0, 0);

Edit: heh I realized its not exactly as above...lemme think on it. 编辑:嘿,我意识到它不完全如上所述...... lemme想一想。

Edit2: Alright the above can still be used a sample, but the sequence of steps to draw from one Graphics2D to another should be as such: 1. From a Graphics2D object to an Image/BufferedImage object using drawGraphics(). Edit2:好的,上面仍然可以使用一个样本,但是从一个Graphics2D到另一个Graphics2D绘制的步骤顺序应该是这样的:1。使用drawGraphics()从Graphics2D对象到Image / BufferedImage对象。 2. From the Image/BufferedImage above, extract its member Graphics2D object by using itscreateGraphics(). 2.从上面的Image / BufferedImage中,使用itscreateGraphics()提取其成员Graphics2D对象。

Looks like you can do one of two things: 看起来你可以做以下两件事之一:

  • create a Graphics2D on an image, do your rendering, then draw the image into another Graphics2D 在图像上创建Graphics2D,进行渲染,然后将图像绘制到另一个Graphics2D中

  • or create Graphics2D from original Graphics2D using Graphics.create() methods and then do you rendering. 或使用Graphics.create()方法从原始Graphics2D创建Graphics2D,然后进行渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM