-1

I have to display dot shaped status viewer, these dots will be displayed in a JPanel.

I am searching a way to make it possible that the number of row and columns are computed to make the best use of the space available in the panel.

The number of dots is not defined, but once the panel is displayed the number of dots will not be changed (the number of dots is set in the constructor of the panel).

The bounding box of the dot will be square. And the size of a dot might not exceed a specified size (like 20 pixels) but might shrink if needed.

I don't know if the explanation is good enough. Sorry.

Example for a near square panel size :

enter image description here

If stretched horizontally :

enter image description here

If stretched (to the max) vertically :

enter image description here

Ideally, the dots could shrink :

enter image description here

iXô
  • 1,133
  • 1
  • 16
  • 39
  • 1
    Use a `GridLayout`. *"I am searching a way to make it possible that the number of row and columns are computed to make the best use of the space available in the panel."* Define 'best use'? I would imagine that the grid layout should have a roughly square shape, which could be achieved by giving the layout the integer value of the square root of the number of components, as either the number of rows or columns in the layout. BTW - just noticed the [tag:java-2d] tag. Are the dots custom painted, or displayed in components? I was assuming components in my comment. – Andrew Thompson May 11 '17 at 13:12
  • By "best use" I mean that if the shape is mostly horizontally this may lead to have 1 row, but if the resize the component, maybe it will split in half the dots and create a new line, and if we continue, maybe this could be 3 lines etc... My dots are custom painted in a JComponents. – iXô May 11 '17 at 13:28
  • A simple drawing or mock-up of what you desire would probably be helpful. – VGR May 11 '17 at 13:37
  • @VGR I wanted to do so earlier, sorry – iXô May 11 '17 at 14:41
  • Okay, those images help a lot. Under what circumstances would the size of each circle shrink? – VGR May 11 '17 at 14:43
  • Yes, if possible, but if it is to difficult, we can assume a dot have a fixed size. – iXô May 11 '17 at 14:46

1 Answers1

0

Ok, so it is not perfect, but here is how I have done it :

In my panel, I have added a component listener :

addComponentListener(new ChannelsStateViewerComponentListener());

Then in the listener I have implemented the componentResized method :

public void componentResized(final ComponentEvent e)
{
    final double ratio = (double)getWidth() / (double)getHeight();

    layout.setRows((int) Math.sqrt(nbChannel / ratio));
    layout.setColumns(0);
}

That's all. Thanks for Andrew for giving me the sqrt trick.

iXô
  • 1,133
  • 1
  • 16
  • 39