It turned out that it was me that was thinking in the wrong coordinate system. It always worked as intended but I was drawing the rectangles basically off-screen. See my other stackoverflow question.
Note: I was asking for deletion of this question but the moderator decided this question should stay.
I am having a PdfPage extends JPanel
that represents a PDF-page which gets drawn as an Image
. First I load the images (rendered with PDFBox) and add it to a simple pager I wrote. Then I'm adding Highlight
objects to each PdfPage
using PdfPage.add(JComponent component)
where each Highlight
is supposed to annotate an error in my document.
My problem is that only the last added Highlight
gets painted and the others are invisible ..
public DatasheetReviserRenderer(File file, List<DatasheetError> datasheetErrorList, int resolution) {
// ..
this.pdfViewer = new PdfViewer(file, resolution);
PdfPage[] pdfPages = this.pdfViewer.getPdfPages();
List<Highlight> highlights = new ArrayList<Highlight>();
for (DatasheetError datasheetError : datasheetErrorList) {
int pageNumber = datasheetError.getPage();
Highlight highlight = createErrorHighlight(datasheetError);
highlights.add(highlight);
PdfPage pdfPage = pdfPages[pageNumber];
pdfPage.add(highlight);
}
this.pdfViewer.setVisible(true);
}
private Highlight createErrorHighlight(DatasheetError datasheetError) {
// ..
return new Highlight(rectangle, new Color(0.5f, 0.1f,0.3f, 0.4f));
}
This is how PdfPage.java looks like:
public class PdfPage extends JPanel {
private static final long serialVersionUID = 7756137054877582063L;
final Image pageImage;
public PdfPage(Image pageImage) {
super(new BorderLayout());
// ..
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
paintPdfPage(g);
}
private void paintPdfPage(Graphics g) {
// just draws pageImage
}
}
And here is Highlight.java:
public class Highlight extends JComponent {
private static final long serialVersionUID = -5376556610591196188L;
/** The rectangle that represents the highlight. */
private Rectangle rectangle;
/** Border is invisible per default. */
private Color borderColor = new Color(0, 0, 0, 0);
/** The highlight color. */
private Color fillColor;
public Highlight(Rectangle rectangle, Color fillColor) {
this.setBounds(rectangle);
this.rectangle = rectangle;
this.fillColor = fillColor;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(this.fillColor);
g.fillRect(this.rectangle.x, this.rectangle.y, this.rectangle.width, this.rectangle.height);
g.setColor(this.borderColor);
g.drawRect(this.rectangle.x, this.rectangle.y, this.rectangle.width, this.rectangle.height);
}
}
Why aren't all Highlight
objects painted?