java - Width and height of a JPanel are 0 (Specific Situation) -
please pardon me if hard follow, have specific problem need help solving. have done ton of research, , have tried numerous solutions none of them working.
my issue have imagepanel
class extending jpanel
(code below), class needs utilize width , height scale images (i making programme users can create custom tutorials including images). when instantiate error saying width , height must nonzero. understand because layout manager has not yet passed imagepanel
preferred size, not know how size panel. imagepanel
within jpanel
within jsplitpane
within of jscrollpane
within of jpanel
within of jtabbedpane
within of jsplitpane
within of jframe
. graphical representation of in decreasing container order follows:
the code imagepanel follows:
import java.awt.graphics; import java.awt.image; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jpanel; public class imagepanel extends jpanel{ private bufferedimage i; private imageicon miniature; private image paint = null; public void createimage(string path){ seek { = imageio.read(new file(path)); } grab (ioexception ex) { ex.printstacktrace(); } if(i != null){ int width = (int)((double)i.getwidth() * ((double)getwidth()/i.getwidth())); int height = (int)((double)i.getheight()*i.getheight()/i.getwidth()*((double)this.getheight()/i.getheight())); miniature = new imageicon(i.getscaledinstance(width, height, image.scale_smooth)); paint = miniature.getimage(); } } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); if (paint!=null){ g.drawimage(paint, 0, 0, null);} } }
how can proper size imagepanel
. image alter size size of jframe
, why don't utilize setpreferedsize();
.
there @ to the lowest degree 2 ways might achieved, first allow paintcomponent
check state of paint
, rescale image appropriatly when null
@override protected void paintcomponent(graphics g) { super.paintcomponent(g); if (i != null && paint == null){ generatescaledinstance(); } if (paint != null) { g.drawimage(paint, 0, 0, this); } }
this work because paintcomponent
should never called unless component has size greater 0
, attached native peer (on screen).
this not great thought scaling can take time , don't want slow downwards paint process if can avoid it.
you utilize componentlistener
attached imagepanel
, monitor componentresized
event
addcomponentlistener(new componentadapter() { @override public void componentresized(componentevent e) { if (i != null) { generatescaledinstance(); } } });
this might called number of times in quick succession, careful.
in case, tend utilize javax.swing.timer
set little delay coalse updates downwards few calls possible, example...
private timer resizetimer; //... // in classes constructor resizetimer = new timer(250, new actionlistener() { public void actionperformed(actionevent evt) { // perform resizing of image... generatescaledinstance(); } }); // don't want repeating event... resizetimer.setrepeats(false); //... public void componentresized(componentevent evt) { resizetimer.restart(); }
this allows componentresized
called number of times in quick succession, if time between exceeds 250 milliseconds, generatescaledinstance
can called, example...
you should provide preferredsize
value of non 0
size default (remember, default preferred size of panel 0x0
). depending on layout manager, ignored, used basis layout managers...
java swing layout jpanel
No comments:
Post a Comment