Draw Graphics2D on another Graphics2D

Can I do from one Graphics2D to another Graphics2D ?

Let me explain. I have printing problems when I show a JTextArea or JTextPanel screen that uses sun.java2d.SunGraphics2D , but when im prints its used sun.print.PeekGraphics and sun.awt.windows.WPathGraphics . The problem is with some fonts like Arial. In some sizes, the lines are cut. I tried many ways to render text when printing Graphics2D.drawString , SwingUtilities2.drawString , TextLayout.drawString , but in some cases the lines are still cut, or the lines are not cut, but some justification makes spaces in spaces.

So, my idea is to try to display components using sun.java2d.SunGraphics2D and β€œcopy” the surface to the printer using sun.print.PeekGraphics or sun.awt.windows.WPathGraphics .

Thanks in advance.

+4
source share
2 answers

Yes, perhaps this is how double buffering is achieved in many Java games. You need a GraphicsId GraphicsId () method that uses another Graphics2D object to draw. from my little game:

  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 that this is not the case above ... lemme think about it.

Edit2: It’s good that the above can still use the sample, but the sequence of steps for drawing from one Graphics2D to another should be as follows: 1. From the Graphics2D object to the Image / BufferedImage object using drawGraphics (). 2. From Image / BufferedImage above, extract its Graphics2D object using itscreateGraphics ().

+1
source

It looks like you can do one of two things:

  • create Graphics2D on the image, render, then draw the image in another Graphics2D

  • or create a Graphics2D from the original Graphics2D using the Graphics.create () methods, and then render.

+1
source

Source: https://habr.com/ru/post/1497806/


All Articles