5

I want to position 10 JPanels in a Circle. Every Panel has the same size and the length between two Panels should be the same. So the easiest way i thought is to grab a null-Layout and compute the bounding box by hand via polarcoordiantes:

JPanel panel = new JPanel(null);

int r = 100;
int phi = 90;

for (int i = 0; i < 10; i++) {
    JPanel x = new JPanel();
    x.setBackground(Color.red);
    x.setBounds((int) (r * Math.sin(phi)) + 100, (int) (r * Math.cos(phi)) + 100, 4, 4);

    panel.add(x);
    phi = (phi + 36) % 360;
}

But that doesnt work! Some items are on the circle, some of them are pixels off... i have a bsolutly no idea why?! I also cant find a LayoutManager that can do that for me, so what to do?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
reox
  • 5,036
  • 11
  • 53
  • 98

2 Answers2

6

While X-Zero was giving the correct answer (1+ to his post), I created an SSCCE:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;

public class PanelsOnCircle extends JPanel {
   private static final int RADIUS = 100;
   private static final int GAP = 20;
   private static final int PREF_W = 2 * RADIUS + 2 * GAP;
   private static final int PREF_H = PREF_W;
   private static final int SLICES = 10;
   private static final int SIDE = 4;

   public PanelsOnCircle() {
      JPanel panel = new JPanel(null);

      for (int i = 0; i < SLICES; i++) {
         double phi = (i * Math.PI * 2) / SLICES; 
         JPanel smallPanel = new JPanel();
         smallPanel.setBackground(Color.red);
         int x = (int) (RADIUS * Math.sin(phi) + RADIUS - SIDE / 2) + GAP;
         int y = (int) (RADIUS * Math.cos(phi) + RADIUS - SIDE / 2) + GAP;
         smallPanel.setBounds(x, y, SIDE, SIDE);

         panel.add(smallPanel);
      }

      setLayout(new BorderLayout());
      add(panel);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      PanelsOnCircle mainPanel = new PanelsOnCircle();

      JFrame frame = new JFrame("PanelsOnCircle");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
5

Your code is good, but you've missed one very important piece of information - the trigonometric functions expect angles in radians not degrees.

Wrap the evaluation of phi in Math.toRadians(double), and you'll get the layout you expect.

(On a side note, I've been thinking about how to do something like this, thanks for the example)

Clockwork-Muse
  • 12,806
  • 6
  • 31
  • 45
  • or phi * (pi/180) :) ahhhh that, i made this mistake in mostly every programminglanguage ive written in :D – reox Jan 13 '12 at 23:31