java - Painting multiple JComponent on JPanel not working -
i don't this. want add highlight extends jcomponent
objects pdfpage extends jpanel
highlight
components simple not painted. can see paint()
, paintcomponent()
methods getting called in correct order not show. thing fixes problem add:
for(component component : this.getcomponents()) { component.paint(g); }
into pdfpanel.paint()
method not how want that. want pdfpage extends jpanel
render jcomponent
i'm adding not override paint()
if possible.
this how add highlight
components pdfpage
panels:
for (datasheeterror datasheeterror : datasheeterrorlist) { int pagenumber = datasheeterror.getpagenumber(); highlight highlight = createerrorhighlight(datasheeterror); pdfpage pdfpage = pdfpages[pagenumber]; pdfpage.add(highlight); }
this how pdfpage
looks like. note not using layoutmanager
calling super(null);
:
public class pdfpage extends jpanel { private static final long serialversionuid = 7756137054877582063l; final image pageimage; public pdfpage(image pageimage) { // no need 'layoutmanager' super(null); this.pageimage = pageimage; rectangle bounds = new rectangle(0, 0, pageimage.getwidth(null), pageimage.getheight(null)); this.setbounds(bounds); this.setlayout(null); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); paintpdfpage(g); } private void paintpdfpage(graphics g) { // transparent background see if `highlight` gets painted g.setcolor(new color(1.0f, 1.0f, 1.0f, 0.0f)); g.fillrect(0, 0, getwidth(), getheight()); } }
in highlight.java can see call this.setbounds(bounds);
public class highlight extends jcomponent { private static final long serialversionuid = -1010170342883487727l; private color bordercolor = new color(0, 0, 0, 0); private color fillcolor; public highlight(rectangle bounds, color fillcolor) { this.fillcolor = fillcolor; this.setbounds(bounds); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); rectangle bounds = this.getbounds(); g.setcolor(this.fillcolor); g.fillrect(bounds.x, bounds.y, bounds.width, bounds.height); g.setcolor(this.bordercolor); g.drawrect(bounds.x, bounds.y, bounds.width, bounds.height); } }
looks problem coordinate space
protected void paintcomponent(graphics g) { ... rectangle bounds = this.getbounds(); g.setcolor(this.fillcolor); g.fillrect(bounds.x, bounds.y, bounds.width, bounds.height); ... }
the getbounds() returns component's rectanle on parent container. when can call g.fillrect(0, 0, bounds.width, bounds.height);
Comments
Post a Comment