私はGraphics2D
とFrame
ライブラリで非常に新しいです。私はオブジェクトにpdfを描画するためにPDFRenderer
(PDFBox
ライブラリから)を試していました。コードは次のとおりです。グラフィックスオブジェクトでフレームに描画されたすべてのストローク/四角形を取得する方法は?
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)
にはGraphics2D
オブジェクトだけが必要なので、私はフレーム以外のライブラリを使用しています。