Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The basic approach is to let your custom component implement Scrollable and code the getTracksViewportWidth/-Height as needed.</p> <p><strong>Edit</strong></p> <blockquote> <p>implement them to return true (thus disabling scrollbars) until I reach my minimum scale, then return false?</p> </blockquote> <p>exactly, that was the idea - but didn't work out as I expected: at the "turning point" when reaching the min the image is scaled to its preferred, even with an self-adjusting getPrefScrollable (which doesn't seem to have any effect, it's called once at the start)</p> <p><strong>Edit 2</strong></p> <p>With the help of the OP:</p> <blockquote> <p>update the view's preferredSize</p> </blockquote> <p>finally got it: (my initial attempt had it upside down ;-) keep the prefScrollable to the "real" pref and let pref return either minimum or pref, depending on whether or not the scrollbar should be visible. </p> <pre><code>public static class JImagePanel extends JPanel implements Scrollable { private BufferedImage image; public JImagePanel(BufferedImage image) { this.image = image; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); BufferedImage scaled = //new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); GraphicsUtilities.createCompatibleImage(getWidth(), getHeight()); Graphics2D g2 = scaled.createGraphics(); g2.drawImage(image, 0, 0, getWidth(), getHeight(), null); g.drawImage(scaled, 0, 0, this); g2.dispose(); } /** * This method is used for laying out this container * inside the Viewport: let it return the "real" pref * or min, depending on whether or not the scrollbars * are showing. */ @Override public Dimension getPreferredSize() { Dimension size = getImageSize(); if (!getScrollableTracksViewportWidth()) { size.width = getMinimumSize().width; } if (!getScrollableTracksViewportHeight()) { size.height = getMinimumSize().height; } return size; } @Override public Dimension getMinimumSize() { Dimension min = getImageSize(); min.height /= 2; min.width /= 2; return min; } /** * This is used for laying out the scrollPane. Keep * it fixed to "real" pref size. */ @Override public Dimension getPreferredScrollableViewportSize() { return getImageSize(); } /** * The unscaled image size (aka: "real" pref) */ protected Dimension getImageSize() { return new Dimension(image.getWidth(), image.getHeight()); } @Override public boolean getScrollableTracksViewportWidth() { return getParent() instanceof JViewport &amp;&amp; getParent().getWidth() &gt;= getMinimumSize().width; } @Override public boolean getScrollableTracksViewportWidth() { return getParent() instanceof JViewport &amp;&amp; getParent().getWidth() &gt;= getMinimumSize().width; } @Override public boolean getScrollableTracksViewportHeight() { return getParent() instanceof JViewport &amp;&amp; getParent().getHeight() &gt;= getMinimumSize().height; } @Override public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { return 10; } @Override public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { return 100; } } </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