java - BufferedImage isn't presented inside JPanel -
i've tried test technique presented in ->this answer <-.
private void jbutton1mouseclicked(java.awt.event.mouseevent evt) { bufferedimage img = new bufferedimage(plotpanel.getwidth(),plotpanel.getheight(),bufferedimage.type_4byte_abgr); graphics2d g2d = img.creategraphics(); g2d.setcolor(color.red); g2d.drawline(0, 0, plotpanel.getwidth(), plotpanel.getheight()); jlabel piclabel = new jlabel(new imageicon( img )); plotpanel.add(piclabel); plotpanel.revalidate(); plotpanel.repaint(); }
why plotpanel still remains intact?
update
here well-anticipated sscce:
package javaapplication10; import javax.swing.swingutilities; public class javaapplication10 { public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { new newjframe().setvisible(true); } }); } }
package javaapplication10; import java.awt.color; import java.awt.graphics2d; import java.awt.image.bufferedimage; import javax.swing.imageicon; import javax.swing.jlabel; public class newjframe extends javax.swing.jframe { /** * creates new form newjframe */ public newjframe() { initcomponents(); } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { plotpanel = new javax.swing.jpanel(); jbutton1 = new javax.swing.jbutton(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); plotpanel.setbackground(new java.awt.color(255, 255, 255)); javax.swing.grouplayout plotpanellayout = new javax.swing.grouplayout(plotpanel); plotpanel.setlayout(plotpanellayout); plotpanellayout.sethorizontalgroup( plotpanellayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 0, short.max_value) ); plotpanellayout.setverticalgroup( plotpanellayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 379, short.max_value) ); jbutton1.settext("jbutton1"); jbutton1.addmouselistener(new java.awt.event.mouseadapter() { public void mouseclicked(java.awt.event.mouseevent evt) { jbutton1mouseclicked(evt); } }); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(plotpanel, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addgroup(layout.createsequentialgroup() .addcomponent(jbutton1, javax.swing.grouplayout.preferred_size, 116, javax.swing.grouplayout.preferred_size) .addgap(0, 758, short.max_value))) .addcontainergap()) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addcomponent(plotpanel, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(jbutton1) .addcontainergap(124, short.max_value)) ); plotpanel.getaccessiblecontext().setaccessiblename("plotpanel"); plotpanel.getaccessiblecontext().setaccessibledescription(""); pack(); }// </editor-fold> private void jbutton1mouseclicked(java.awt.event.mouseevent evt) { bufferedimage img = new bufferedimage(plotpanel.getwidth(),plotpanel.getheight(),bufferedimage.type_4byte_abgr); graphics2d g2d = img.creategraphics(); g2d.setcolor(color.red); g2d.drawline(0, 0, plotpanel.getwidth(), plotpanel.getheight()); jlabel piclabel = new jlabel(new imageicon( img )); plotpanel.add(piclabel); plotpanel.revalidate(); plotpanel.repaint(); } /** * @param args command line arguments */ public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(newjframe.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(newjframe.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(newjframe.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(newjframe.class.getname()).log(java.util.logging.level.severe, null, ex); } java.awt.eventqueue.invokelater(new runnable() { public void run() { new newjframe().setvisible(true); } }); } private javax.swing.jbutton jbutton1; private javax.swing.jpanel plotpanel; }
make sure call revalidate
after adding jlabel
piclabel
:
plotpanel.revalidate();
also, you're using grouplayout
requires both horizontal , vertical sequential groups added layout new component visible. layout not easy code hand , produces lot of boiler-plate code. use more conventional layout managers become familiar code. see a visual guide layout managers
Comments
Post a Comment