java - Custom JPanel causing rendering problems in second custom JPanel -
bit of odd 1 here. have 2 classes extending jpanel
, overriding paintcomponent
in both. 1 implements runnable
(for animation purposes).
however, when used runnable
1 on top, wonderful "paint copy of mouse points at" in background of runnable
instance. see screenshots below:
the difference between 2 me using jpanel
in former , custom jpanel
background image in latter. code second jpanel
below:
package view.widgets; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.jpanel; public class paintedjpanel extends jpanel { private static final long serialversionuid = 1l; private bufferedimage backgroundimage = null; public paintedjpanel() { super(); } public paintedjpanel(file image) { super(); try { backgroundimage = imageio.read(image); } catch (ioexception e) { e.printstacktrace(); } } @override protected void paintcomponent(graphics g) { graphics2d g2d = (graphics2d) g; if(null != backgroundimage) { g2d.drawimage(backgroundimage, 0, 0, null); } } public bufferedimage getbackgroundimage() { return backgroundimage; } public void setbackgroundimage(bufferedimage backgroundimage) { this.backgroundimage = backgroundimage; } }
edit: editing in details because enter key shouldn't submit question when i'm adding tags.
finished editing @ 13:38.
ah, paintcomponent method is missing super's call. change
@override protected void paintcomponent(graphics g) { graphics2d g2d = (graphics2d) g; if(null != backgroundimage) { g2d.drawimage(backgroundimage, 0, 0, null); } }
to
@override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g; if(null != backgroundimage) { g2d.drawimage(backgroundimage, 0, 0, null); } }
as noted in comments question (before seeing code), without calling super, you're breaking painting chain, possibly resulting in side effects child component rendering.
Comments
Post a Comment