Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, the quickest and easiest solution is to use <code>Image.getScaledInstance</code></p> <pre><code>g.drawImage(img.getScaledInstance(newWidth, -1, Image. SCALE_SMOOTH), x, y, this); </code></pre> <p>If your wondering about the negative number, the java docs say:</p> <blockquote> <p>If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width and height are negative, then the original image dimensions are used.</p> </blockquote> <p><strong>UPDATE</strong></p> <p>Just as a side note (my Google was playing up).</p> <p><code>getScaledInstance</code> is neither the fastest or highest quality approach, but it is the easiest.</p> <p>Take a read through <a href="http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html" rel="noreferrer">The Perils of Image.getScaledInstance</a> for some more ideas</p> <p><strong>UPDATE</strong></p> <p>Scaling an image to fit an area is slightly more complicated then simply scaling the aspect ratio. You have to make a choice over if you want the image to "fit" within the area (possibly leaving blank areas around it) or over "fill" the area (so that it's smallest dimension fits the largest dimension of the area).</p> <p><img src="https://i.stack.imgur.com/PMPbZ.png" alt="Fit"><img src="https://i.stack.imgur.com/3T9Gt.png" alt="Fill"></p> <p>Fit &amp; Fill</p> <p>Basically, I work with scale factors</p> <p>This returns the scaling factor for a particular size. I use this to make decisions about which factor I want to use based which algorithm I need</p> <pre><code>public static double getScaleFactor(int iMasterSize, int iTargetSize) { double dScale = 1; if (iMasterSize &gt; iTargetSize) { dScale = (double) iTargetSize / (double) iMasterSize; } else { dScale = (double) iTargetSize / (double) iMasterSize; } return dScale; } </code></pre> <p>It's used by these two methods. They simply take two <code>Dimension</code>s. The original and the target.</p> <pre><code>public static double getScaleFactorToFit(Dimension original, Dimension toFit) { double dScale = 1d; if (original != null &amp;&amp; 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; } </code></pre> <p>It's relatively simple to pass an image into (either directly or via a support method). So for example, you could call this from within your <code>paint</code> method</p> <pre><code>double factor getScaledFactorToFit(new Dimension(image.getWidth(), image.getHeight()), getSize()); int scaledWidth = image.getWidth() * scale; int scaledHeight *= image.getWidth() * scale; </code></pre> <p>This will automatically take care of the aspect ratio for you ;)</p> <p><strong>UPDATED with expanded example</strong></p> <pre><code>public double getScaleFactor(int iMasterSize, int iTargetSize) { double dScale = 1; if (iMasterSize &gt; 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 &amp;&amp; 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload