package diamond01;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class code {

    public static void main(String[] args) {
        new code();
    }

    public code() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Diamond diamond;

        public TestPane() {
            diamond = new Diamond(100, 100);
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - diamond.getBounds().width) / 2;
            int y = (getHeight()- diamond.getBounds().height) / 2;
            AffineTransform at = AffineTransform.getTranslateInstance(x, y);
            Shape shape = at.createTransformedShape(diamond);
            g2d.setColor(Color.YELLOW);
            g2d.fill(shape);
            g2d.setColor(Color.RED);
            g2d.draw(shape);
            g2d.dispose();
            
            int  x1 = 0, x2 = 0,  y1 = 0, y2 = 0;
            
            x1 = 100;
            y1 = 100;
            x2 = 200;
            y2 = 200;
            
            x = (x1+x2)/2;
            y = (y1+y2)/2;
            g.drawLine(x1, y , x , y1);
            //g.drawLine(x , y1, x2, y );
            //g.drawLine(x2, y , x , y2);
            //g.drawLine(x , y2, x1, y );
            
            
        }
 /*       
        public void drawDiamond(Graphics g, int x1, int y1, int x2, int y2)
        {
            x = (x1+x2)/2;
            y = (y1+y2)/2;
            g.drawLine(x1, y , x , y1);
            g.drawLine(x , y1, x2, y );
            g.drawLine(x2, y , x , y2);
            g.drawLine(x , y2, x1, y );
        }
*/
    }

    public class Diamond extends Path2D.Double {

        public Diamond(double width, double height) {
            moveTo(0, height / 2);
            lineTo(width / 2, 0);
            lineTo(width, height / 2);
            lineTo(width / 2, height);
            closePath();
        }

    }

}