0

I have problem with creating own swing component. I want to create component that will render some image and it will fit current window (or container) size proportionally.

The problem is that I cant know real available space for component in container (without insents, borders, may be something else...).

This example shows the problem:

public class Main
{
    static class MyComponent extends JComponent
    {
        @Override
        public Dimension getPreferredSize()
        {
            if(isShowing())
            {
                int width = getParent().getSize().width - 4;
                Dimension dimension = new Dimension(width, (int)(width / 0.8));
                System.out.println(dimension);
                return dimension;
            }
            return super.getPreferredSize();
        }

        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);

            g.setColor(Color.WHITE);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.RED);
            g.fillOval(0, 0, getWidth(), getHeight());
        }
    }

    public static void main(String... args)
    {
        JFrame frame = new JFrame("Test");
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(Color.DARK_GRAY);
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(2, 2, 2, 2);
        c.gridx = c.gridy = 0;
        panel.add(new MyComponent(), c);
        c.gridy++;
        panel.add(new MyComponent(), c);
        frame.add(new JScrollPane(panel));
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }
}

If you remove 4 (left & right from insets) from getParent().getSize().width - 4 expression and launch, you will see that component(s) will increase its size infinitely.

In the above code I get full width of container, but I need available width so that component will not trigger container to resize again and again.

Or is there another way to fit component normally?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
cybersoft
  • 1,453
  • 13
  • 31
  • 1
    It's not the component you want to keep in proportions, it's the image, allow rage component to be what ever size the parent wants it to be and scale the image to fit within the bounds. Your component should just return suitable sizing hints – MadProgrammer Jan 09 '16 at 20:35
  • 1
    See [this](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928), [this](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003) and [this](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) for examples – MadProgrammer Jan 09 '16 at 20:40
  • @MadProgrammer And how to do these hints? As I see the problem is in JScrollPane - it tries to accomodate components by width. If I allow to size component by parent, component will have size without source image proportions and will render only part of image. – cybersoft Jan 10 '16 at 17:20
  • Not so good solution... But I made via getting layout manager and subtract insets from width if it's GridBagLayout. – cybersoft Jan 10 '16 at 18:21
  • Scale the image to the fit within the proportions of the component itself. If the component is within a `JScrollPane` then no amount of "hackery" is going to help, as the container itself will be set to it's preferredSize anyway – MadProgrammer Jan 10 '16 at 19:39
  • @MadProgrammer, OK, thanks for your help. – cybersoft Jan 10 '16 at 19:47

0 Answers0