title: Drawing a diamond with Polygon on the end of a Line2D
body: <p>I'm trying to draw a diamond (for a composition UML relationship). Right now, I am making a triangle like this:</p>
  
  <p><img src="https://i.stack.imgur.com/NeyB8.png" alt="enter image description here"></p>
  
  <p>but I want to make something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/sAFpI.png" alt="enter image description here"></p>
  
  <p>And I do the triangle with this code:</p>
  
  <pre><code>private void drawArrowHead(Graphics2D g2, Point tip, Point tail,
              Color color) {
      g2.setPaint(color);
      double dy = tip.y - tail.y;
      double dx = tip.x - tail.x;
      double theta = Math.atan2(dy, dx);
      double x, y, rho = theta + phi;
      Point p1 = new Point();
      Point p2 = new Point();
  
      p1.setLocation(tip.x - barb * Math.cos(rho), tip.y - barb * Math.sin(rho));
      rho = theta - phi;
      p2.setLocation(tip.x - barb * Math.cos(rho), tip.y - barb * Math.sin(rho));
  
      int[] xPoints = new int[5];
      int[] yPoints = new int[5];
  
      xPoints[0] = tip.x;
      xPoints[1] = p1.x;
      xPoints[2] = p2.x;
  
      yPoints[0] = tip.y;
      yPoints[1] = p1.y;
      yPoints[2] = p2.y;
  
      g2.setPaint(Color.BLACK);
      Shape shape = new Polygon(xPoints, yPoints, 3);
      g2.fill(shape);
  
      //tip.x - barb * Math.cos(rho);
      //y = tip.y - barb * Math.sin(rho);
  }
  </code></pre>
  
  <p>Does anyone have an idea on how to make a diamond out of this? Thanks :)</p>
  
  <pre><code>import java.awt.BasicStroke;
  import java.awt.Color;
  import java.awt.Cursor;
  import java.awt.EventQueue;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.Panel;
  import java.awt.Point;
  import java.awt.Polygon;
  import java.awt.RenderingHints;
  import java.awt.Shape;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.awt.geom.Line2D;
  import java.awt.geom.Point2D;
  import java.awt.geom.Rectangle2D;
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.swing.JFrame;
  import javax.swing.JMenuBar;
  import javax.swing.JMenuItem;
  import javax.swing.JPanel;
  import javax.swing.UIManager;
  import javax.swing.UnsupportedLookAndFeelException;
  
  public class DrawArrows {
  
      public static void main(String[] args) {
          new DrawArrows();
      }
  
      public DrawArrows() {
          EventQueue.invokeLater(new Runnable() {
              @Override
              public void run() {
                  try {
                      UIManager.setLookAndFeel(UIManager
                              .getSystemLookAndFeelClassName());
                  } catch (ClassNotFoundException | InstantiationException
                          | IllegalAccessException
                          | UnsupportedLookAndFeelException ex) {
                      ex.printStackTrace();
                  }
  
                  JFrame frame = new UMLWindow();
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setBounds(30, 30, 1000, 700);
                  frame.getContentPane().setBackground(Color.white);
                  frame.setVisible(true);
                  frame.setLocationRelativeTo(null);
              }
          });
      }
  
      public static class UMLWindow extends JFrame {
  
          Shapes shapeList = new Shapes();
          Panel panel;
  
          private static final long serialVersionUID = 1L;
  
          public UMLWindow() {
              addMenus();
              panel = new Panel();
          }
  
          public void addMenus() {
  
              getContentPane().add(shapeList);
  
              setSize(300, 200);
              setLocationRelativeTo(null);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
  
              JMenuItem lineMenuItem = new JMenuItem("New Line");
              lineMenuItem.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent event) {
                      System.out.println("adding line");
                      shapeList.addLine();
                  }
              });
  
              JMenuBar menubar = new JMenuBar();
  
              menubar.add(lineMenuItem);
  
              setJMenuBar(menubar);
  
          }
      }
  
      public static class Shapes extends JPanel {
  
          private static final long serialVersionUID = 1L;
  
          private List&lt;Line2D.Double&gt; lines = new ArrayList&lt;Line2D.Double&gt;();
          private Boolean drawing = false;
          private Point lineStartingPoint = new Point();
          private Point lineEndingPoint = new Point();
          private Line2D.Double linePath;
  
          double phi = Math.toRadians(40);
          int barb = 20;
  
          public Shapes() {
              MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
              addMouseListener(myMouseAdapter);
              addMouseMotionListener(myMouseAdapter);
              this.setOpaque(true);
              this.setBackground(Color.WHITE); // set canvas color
          }
  
          public void addLine() {
              drawing = true;
              repaint();
          }
  
          @Override
          protected void paintComponent(Graphics g) {
              super.paintComponent(g);
  
              Graphics2D g2 = (Graphics2D) g;
              g2.setStroke(new BasicStroke(2));
              if (drawing) {
                  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                          RenderingHints.VALUE_ANTIALIAS_ON);
                  g2.setStroke(new BasicStroke(2));
                  g2.drawLine(lineStartingPoint.x, lineStartingPoint.y,
                          lineEndingPoint.x, lineEndingPoint.y);
                  drawArrowHead(g2, lineEndingPoint, lineStartingPoint,
                          Color.BLACK);
              }
              for (Line2D line : lines) {
                  g2.setColor(Color.BLACK);
                  Point sw = new Point((int) line.getX1(), (int) line.getY1());
                  Point ne = new Point((int) line.getX2(), (int) line.getY2());
                  g2.draw(line);
                  drawArrowHead(g2, ne, sw, Color.BLACK);
              }
          }
  
          public Rectangle2D drawRect(int x, int y) {
              return new Rectangle2D.Double(x - 4, y - 4, 8, 8);
          }
  
          private void drawArrowHead(Graphics2D g2, Point tip, Point tail,
                  Color color) {
              g2.setPaint(color);
              double dy = tip.y - tail.y;
              double dx = tip.x - tail.x;
              double theta = Math.atan2(dy, dx);
              double x, y, rho = theta + phi;
              Point p1 = new Point();
              Point p2 = new Point();
  
              p1.setLocation(tip.x - barb * Math.cos(rho), tip.y - barb * Math.sin(rho));
              rho = theta - phi;
              p2.setLocation(tip.x - barb * Math.cos(rho), tip.y - barb * Math.sin(rho));
  
              int[] xPoints = new int[5];
              int[] yPoints = new int[5];
  
              xPoints[0] = tip.x;
              xPoints[1] = p1.x;
              xPoints[2] = p2.x;
  
              yPoints[0] = tip.y;
              yPoints[1] = p1.y;
              yPoints[2] = p2.y;
  
              g2.setPaint(Color.BLACK);
              Shape shape = new Polygon(xPoints, yPoints, 3);
              g2.fill(shape);
  
              //tip.x - barb * Math.cos(rho);
              //y = tip.y - barb * Math.sin(rho);
          }
  
          class MyMouseAdapter extends MouseAdapter {
              int currentIndex;
              Point2D.Double startPoint = new Point2D.Double();
              Point2D.Double endPoint = new Point2D.Double();
              Boolean resizing = false;
  
              @Override
              public void mousePressed(MouseEvent e) {
                  if (drawing) {
                      lineStartingPoint = e.getPoint();
                      lineEndingPoint = lineStartingPoint;
                  }
              }
  
              @Override
              public void mouseDragged(MouseEvent e) {
                  if (drawing) {
                      lineEndingPoint = e.getPoint();
                      repaint();
                      System.out.println(lines.size());
                  }
              }
  
              @Override
              public void mouseReleased(MouseEvent e) {
                  if (drawing) {
                      drawLine(e);
                  }
                  drawing = false;
  
              }
  
              @Override
              public void mouseMoved(MouseEvent e) {
  
              }
  
              public void drawLine(MouseEvent e) {
                  drawing = false;
                  lineEndingPoint = e.getPoint();
                  linePath = new Line2D.Double(lineStartingPoint.getX(),
                          lineStartingPoint.getY(), lineEndingPoint.getX(),
                          lineEndingPoint.getY());
                  lines.add(linePath);
                  repaint();
              }
          }
  
      }
  }
  </code></pre>