title: Java - Hexagon location in grid
body: <p>Im searching for an algorythem to get the location of a hexagon in a grid.
  I found this one but it doesnt seam to work:</p>
  
  <pre><code>for(int i = 0; i &lt; width; i++) {
          for(int j = 0; j &lt; height; j++) {
              grid[i][j] = new Hexagon(x+(j*((3*Hexagon.S)/2)), y+((j%2)*Hexagon.A)+(2*i*Hexagon.A));
          }
      }
  </code></pre>
  
  <p>The output is kind of strange:
  <a href="https://i.stack.imgur.com/4CtL4.png" rel="nofollow noreferrer">output</a></p>
  
  <p>This is the window-creating class(just a test class):</p>
  
  <pre><code>import java.awt.Container;
  
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class Grid extends JPanel {
  
      private static final long serialVersionUID = 1L;
  
      public static void main(String[] args) {
          int width = 2;
          int height = 4;
          int x = 100;
          int y = 100;
          Hexagon[][] grid = new Hexagon[width][height];
  
          JFrame f = new JFrame();
          Container cp = f.getContentPane();
  
  
          for(int i = 0; i &lt; width; i++) {
              for(int j = 0; j &lt; height; j++) {
                  grid[i][j] = new Hexagon(x+(j*((3*Hexagon.S)/2)), y+((j%2)*Hexagon.A)+(2*i*Hexagon.A));
                  cp.add(grid[i][j]);
              }
          }
  
          f.setLayout(null);
          f.setBounds(100, 100, 300, 300);
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.setVisible(true);
      }
  }
  </code></pre>
  
  <p>The <code>Hexagon.java</code> class:</p>
  
  <pre><code>import java.awt.BasicStroke;
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.Polygon;
  
  import javax.swing.JButton;
  
   public class Hexagon extends JButton {
  
      public static final int S = 50;
      public static final int A = (int) (Math.sqrt(3)*(S/2));
  
      private static final long serialVersionUID = 1L;
      private final int x, y;
      private final Polygon shape;
  
      public Hexagon(int x, int y) {
          this.x = x;
          this.y = y;
          this.shape = initHexagon();
          setSize(2*S, 2*A);
          setLocation(x-S, y-A);
          setContentAreaFilled(false);
      }
  
      private Polygon initHexagon() {
          Polygon p = new Polygon();
          p.addPoint(x+(S/2), y-A);
          p.addPoint(x+S, y);
          p.addPoint(x+(S/2), y+A);
          p.addPoint(x-(S/2), y+A);
          p.addPoint(x-S, y);
          p.addPoint(x-(S/2), y-A);
          return p;
      }
  
      protected void paintComponent(Graphics g) {
          g.setColor(Color.BLACK);
          g.drawPolygon(this.shape);
      } 
  
      protected void paintBorder(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          g2.setColor(Color.BLACK);
          g2.setStroke(new BasicStroke(4));
          g2.drawPolygon(this.shape);
      }
  
      public boolean contains(int x, int y) {
          return this.shape.contains(x, y);
      }
  }
  </code></pre>
  
  <p>As i said, this class worked just fine using non-rectangular shapes. 
  There was no clipping or such. </p>