i have jpanel
painted background image , layout manager holding other smaller images, of inside jframe
. background image pretty big , want able have maintain aspect ratio whether on big or small monitor.
eventually, want able have layoutmanager
, smaller images in cells "glued" background picture.
i looked around resources , seems many examples use bufferedimage
not; pose problem? i'll post code below painting image, if lack information please let me know.
public class monitorpanel extends jpanel { image img; public monitorpanel() throws malformedurlexception { //add components try { img = imageio.read(new file("src/customer_vlans.jpg")); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } protected void paintcomponent(graphics g) { //paint background image super.paintcomponent(g); //g.drawimage(img, 0, 0, getwidth(), getheight(), this); g.drawimage(img, 0, 0, this); } }
edit: should mention know aspect ratio formula: original height / original width x new width = new height however, not know how use correctly advantage.
well, quickest , easiest solution use image.getscaledinstance
g.drawimage(img.getscaledinstance(newwidth, -1, image. scale_smooth), x, y, this);
if wondering negative number, java docs say:
if either width or height negative number value substituted maintain aspect ratio of original image dimensions. if both width , height negative, original image dimensions used.
update
just side note (my google playing up).
getscaledinstance
neither fastest or highest quality approach, easiest.
take read through the perils of image.getscaledinstance more ideas
update
scaling image fit area more complicated scaling aspect ratio. have make choice on if want image "fit" within area (possibly leaving blank areas around it) or on "fill" area (so it's smallest dimension fits largest dimension of area).
fit & fill
basically, work scale factors
this returns scaling factor particular size. use make decisions factor want use based algorithm need
public static double getscalefactor(int imastersize, int itargetsize) { double dscale = 1; if (imastersize > itargetsize) { dscale = (double) itargetsize / (double) imastersize; } else { dscale = (double) itargetsize / (double) imastersize; } return dscale; }
it's used these 2 methods. take 2 dimension
s. original , target.
public static double getscalefactortofit(dimension original, dimension tofit) { double dscale = 1d; if (original != null && tofit != null) { double dscalewidth = getscalefactor(original.width, tofit.width); double dscaleheight = getscalefactor(original.height, tofit.height); dscale = math.min(dscaleheight, dscalewidth); } return dscale; } public static double getscalefactortofill(dimension mastersize, dimension targetsize) { double dscalewidth = getscalefactor(mastersize.width, targetsize.width); double dscaleheight = getscalefactor(mastersize.height, targetsize.height); double dscale = math.max(dscaleheight, dscalewidth); return dscale; }
it's relatively simple pass image (either directly or via support method). example, call within paint
method
double factor getscaledfactortofit(new dimension(image.getwidth(), image.getheight()), getsize()); int scaledwidth = image.getwidth() * scale; int scaledheight *= image.getwidth() * scale;
this automatically take care of aspect ratio ;)
updated expanded example
public double getscalefactor(int imastersize, int itargetsize) { double dscale = 1; if (imastersize > itargetsize) { dscale = (double) itargetsize / (double) imastersize; } else { dscale = (double) itargetsize / (double) imastersize; } return dscale; } public double getscalefactortofit(dimension original, dimension tofit) { double dscale = 1d; if (original != null && tofit != null) { double dscalewidth = getscalefactor(original.width, tofit.width); double dscaleheight = getscalefactor(original.height, tofit.height); dscale = math.min(dscaleheight, dscalewidth); } return dscale; } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); double scalefactor = math.min(1d, getscalefactortofit(new dimension(image.getwidth(), image.getheight()), getsize())); int scalewidth = (int) math.round(image.getwidth() * scalefactor); int scaleheight = (int) math.round(image.getheight() * scalefactor); image scaled = image.getscaledinstance(scalewidth, scaleheight, image.scale_smooth); int width = getwidth() - 1; int height = getheight() - 1; int x = (width - scaled.getwidth(this)) / 2; int y = (height - scaled.getheight(this)) / 2; g.drawimage(scaled, x, y, this); }
Comments
Post a Comment