java - Drawing partially transparent PNG onto JPanel -


how go drawing partially transparent image onto jpanel overriding void paint (graphics g) method? i've tried obvious way, load image , use ((graphics2d)g).drawimage(...) didn't work, , internet isn't telling me much.

jlabel capable of doing this, if want else panel/image, using paintcomponent , graphics#drawimage option.

using base...

enter image description here

i able render this...

enter image description here

import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.lineargradientpaint; import java.awt.point; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class transparentpng {      public static void main(string[] args) {         new transparentpng();     }      public transparentpng() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.setlayout(new borderlayout());                 frame.add(new testpane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public class testpane extends jpanel {          private bufferedimage img;          public testpane() {             try {                 img = imageio.read(new file("pony.png"));             } catch (ioexception ex) {                 ex.printstacktrace();             }         }          @override         public dimension getpreferredsize() {             return img == null ? new dimension(200, 200) : new dimension(img.getwidth(), img.getheight());         }          @override         protected void paintcomponent(graphics g) {             super.paintcomponent(g);             graphics2d g2d = (graphics2d) g.create();             lineargradientpaint lgp = new lineargradientpaint(                     new point(0, 0),                     new point(0, getheight()),                     new float[]{0f, 1f},                     new color[]{color.green, color.yellow});             g2d.setpaint(lgp);             g2d.fillrect(0, 0, getwidth(), getheight());             if (img != null) {                 int x = (getwidth() - img.getwidth()) / 2;                 int y = (getheight() - img.getheight()) / 2;                 g2d.drawimage(img, x, y, this);             }             g2d.dispose();         }     } } 

check out how use labels , performing custom painting


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -