如何获取在Frame by Graphics对象上绘制的所有笔划/矩形?

时间:2016-12-23 12:27:22

标签: java graphics2d

我对FramePDFRenderer库不熟悉。我正在尝试PDFBox(来自Graphics2D库)在import java.awt.*; import java.awt.event.*; import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; public class Example01 extends Frame { /** * Instantiates an Example01 object. **/ public static void main(String args[]) { new Example01(); } /** * Our Example01 constructor sets the frame's size, adds the * visual components, and then makes them visible to the user. * It uses an adapter class to deal with the user closing * the frame. **/ public Example01() { //Title our frame. super("Java 2D Example01"); //Set the size for the frame. setSize(1000,1000); //We need to turn on the visibility of our frame //by setting the Visible parameter to true. setVisible(true); //Now, we want to be sure we properly dispose of resources //this frame is using when the window is closed. We use //an anonymous inner class adapter for this. addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {dispose(); System.exit(0);} } ); } /** * The paint method provides the real magic. Here we * cast the Graphics object to Graphics2D to illustrate * that we may use the same old graphics capabilities with * Graphics2D that we are used to using with Graphics. **/ public void paint(Graphics g) { Graphics2D g2d = (Graphics2D)g; try (PDDocument document = PDDocument.load(new File("C:\\Users\\prabhjot.rai\\Desktop\\xceligent\\9542899.pdf"))) { PDFRenderer pdfRenderer = new PDFRenderer(document); pdfRenderer.renderPageToGraphics(1, g2d); System.out.println(g2d.getStroke()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 对象上绘制pdf。这是代码:

pdfRenderer.renderPageToGraphics(1, g2d)

这样做,创建一个框架,在其中我可以看到整个pdf。我试图在框架内绘制笔画 - 使用图形对象。我怎么能得到那些?我可以使用框架以外的任何库,因为我的方法Graphics2D只需要{{1}}个对象。

1 个答案:

答案 0 :(得分:0)

我担心这是不可能的。

Graphics2D是一个基于图像的图形工具,这意味着您拍摄的所有动作都会被绘制到图像中并被遗忘,只会存储生成的像素。

函数getStroke()仅返回当前笔划,当您使用任何绘图方法时将使用该笔划。

许多grahpics框架采用不同的方法(例如JavaFX),它支持以节点为基础绘制形状和图形,允许您分解和更改图像的部分(这会在每次更改后呈现实际图像)。