import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;
/**
 *
 * Beschreibung
 *
 * @version 1.0 vom 06.05.2019
 * @author 
 */

public class FigurenFrame extends JFrame {

    public FigurenFrame(final FigurenBase fb) { 
        super();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        int frameWidth = 862; 
        int frameHeight = 542;
        setSize(frameWidth, frameHeight);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (d.width - getSize().width) / 2;
        int y = (d.height - getSize().height) / 2;
        setLocation(x, y);
        setTitle("Figuren");
        setResizable(false);
        Container cp = getContentPane();
        cp.setLayout(null);

        final JFrame theFrame = this;
        javax.swing.border.Border blackBorder = BorderFactory.createLineBorder(Color.BLACK);
        final JSpinner spnObjectIdx = new JSpinner(new SpinnerNumberModel(0, 0, 0, 1));
        JRadioButton rbnKreis = new JRadioButton();
        JRadioButton rbnQuadrat = new JRadioButton();
        JRadioButton rbnMickyMaus = new JRadioButton();
        JTextField numRadius = new JTextField();
        JTextField numSeitenlaenge = new JTextField();
        JTextField numSize = new JTextField();
        JPanel pnlColor = new JPanel(null, true);
        JCheckBox chkRand = new JCheckBox();
        JPanel pnlColor2 = new JPanel(null, true);

        FigurenPanel pnlFiguren = new FigurenPanel(fb);
        pnlFiguren.setBounds(153,8,690,495);
        pnlFiguren.setBorder(blackBorder);
        pnlFiguren.addMouseListener(new MouseAdapter() { 
                public void mousePressed(MouseEvent me) { 
                    if (me.getButton() == MouseEvent.BUTTON1) {
                        int size = 10; 
                        int type = Figuren.TYP_KREIS;
                        if (rbnKreis.isSelected()) {
                            type = Figuren.TYP_KREIS;
                            try
                            {
                                size = Integer.parseInt(numRadius.getText());
                            }
                            catch(Exception x)
                            { System.out.println(x.getMessage()); }
                        } 
                        else if (rbnQuadrat.isSelected()){
                            type = Figuren.TYP_QUADRAT;
                            try
                            {
                                size = Integer.parseInt(numSeitenlaenge.getText());
                            }
                            catch(Exception x)
                            { System.out.println(x.getMessage()); }
                        } 
                        else {
                            type = Figuren.TYP_MICKYMAUS;
                            try
                            {
                                size = Integer.parseInt(numSize.getText());
                            }
                            catch(Exception x)
                            { System.out.println(x.getMessage()); }
                        } 
                        fb.neueFigur(me.getX(), me.getY(), type, size, pnlColor.getBackground(), chkRand.isSelected() ? pnlColor2.getBackground() : null);
                        pnlFiguren.invalidate();
                        pnlFiguren.repaint();

                    } 
                    else if (me.getButton() == MouseEvent.BUTTON3){
                        fb.figurLoeschen(me.getX(), me.getY());
                        pnlFiguren.invalidate();
                        pnlFiguren.repaint();
                    }
                    SpinnerNumberModel spnModel = (SpinnerNumberModel)spnObjectIdx.getModel();
                    if (fb.getAnzahlFiguren() >= 0)
                        spnModel.setMaximum(fb.getAnzahlFiguren()-1);
                    if ((Integer)spnModel.getValue() > (Integer)spnModel.getMaximum())
                        spnModel.setValue(spnModel.getMaximum());
                } 
            }); 
        cp.add(pnlFiguren);

        JPanel pnlGroup1 = new JPanel(null, true);
        pnlGroup1.setOpaque(false);
        pnlGroup1.setBorder(blackBorder);
        pnlGroup1.setBounds(8, 56, 137, 65);
        cp.add(pnlGroup1);

        pnlColor.setBounds(90, 8, 55, 33);
        pnlColor.setOpaque(true);
        pnlColor.setBackground(Color.RED);
        pnlColor.setBorder(blackBorder);
        pnlColor.addMouseListener(new MouseAdapter() { 
                public void mousePressed(MouseEvent me) { 
                    if (me.getButton() == MouseEvent.BUTTON1 &&
                    me.getID() == MouseEvent.MOUSE_PRESSED) {
                        Color ausgewaehlteFarbe = JColorChooser.showDialog(theFrame, "Füllfarbe wählen", pnlColor.getBackground());
                        if (ausgewaehlteFarbe != null) {
                            pnlColor.setBackground(ausgewaehlteFarbe);
                        } 

                    } 
                } 
            }); 
        cp.add(pnlColor);

        JLabel jLabel1 = new JLabel();
        jLabel1.setBounds(8, 16, 62, 20);
        jLabel1.setText("Farbe:");
        cp.add(jLabel1);

        rbnKreis.setBounds(8, 8, 100, 20);
        rbnKreis.setOpaque(false);
        rbnKreis.setText("Kreis");
        rbnKreis.setSelected(true);
        pnlGroup1.add(rbnKreis);

        JLabel jLabel2 = new JLabel();
        jLabel2.setBounds(8, 32, 54, 20);
        jLabel2.setText("Radius:");
        pnlGroup1.add(jLabel2);

        numRadius.setText("10");
        numRadius.setBounds(92, 32, 40, 20);
        pnlGroup1.add(numRadius);

        JPanel pnlGroup2 = new JPanel(null, true);
        pnlGroup2.setBounds(8, 128, 137, 65);
        pnlGroup2.setOpaque(false);
        pnlGroup2.setBorder(blackBorder);
        cp.add(pnlGroup2);

        rbnQuadrat.setBounds(8, 8, 100, 20);
        rbnQuadrat.setOpaque(false);
        rbnQuadrat.setText("Quadrat");
        pnlGroup2.add(rbnQuadrat);

        JLabel jLabel3 = new JLabel();
        jLabel3.setBounds(8, 32, 74, 20);
        jLabel3.setText("Seitenlänge:");
        pnlGroup2.add(jLabel3);

        numSeitenlaenge.setBounds(92, 32, 40, 20);
        numSeitenlaenge.setText("15");
        pnlGroup2.add(numSeitenlaenge);

        chkRand.setBounds(8, 198+7, 75, 20);
        chkRand.setText("mit Rand");
        cp.add(chkRand);

        pnlColor2.setBounds(90, 198, 55, 33);
        pnlColor2.setOpaque(true);
        pnlColor2.setBackground(Color.BLACK);
        pnlColor2.setBorder(blackBorder);
        pnlColor2.addMouseListener(new MouseAdapter() { 
                public void mousePressed(MouseEvent me) { 
                    if (me.getButton() == MouseEvent.BUTTON1 &&
                    me.getID() == MouseEvent.MOUSE_PRESSED) {
                        Color ausgewaehlteFarbe = JColorChooser.showDialog(theFrame, "Randfarbe wählen", pnlColor.getBackground());
                        if (ausgewaehlteFarbe != null) {
                            pnlColor2.setBackground(ausgewaehlteFarbe);
                        } 

                    } 
                } 
            }); 
        cp.add(pnlColor2);

        JPanel pnlGroup3 = new JPanel(null, true);
        pnlGroup3.setBounds(8, 224+17, 137, 65);
        pnlGroup3.setOpaque(false);
        pnlGroup3.setBorder(blackBorder);
        cp.add(pnlGroup3);

        rbnMickyMaus.setBounds(8, 8, 100, 20);
        rbnMickyMaus.setOpaque(false);
        rbnMickyMaus.setText("Micky Maus");
        pnlGroup3.add(rbnMickyMaus);

        JLabel jLabel5 = new JLabel();
        jLabel5.setBounds(8, 32, 54, 20);
        jLabel5.setText("Größe:");
        pnlGroup3.add(jLabel5);

        numSize.setBounds(92, 32, 40, 20);
        numSize.setText("20");
        pnlGroup3.add(numSize);

        ButtonGroup group = new ButtonGroup();
        group.add(rbnQuadrat);
        group.add(rbnKreis);
        group.add(rbnMickyMaus);

        JPanel pnlGroup4 = new JPanel(null, true);
        pnlGroup4.setBounds(8, pnlGroup3.getY()+pnlGroup3.getHeight()+8,
            pnlGroup3.getWidth(), 230);
        pnlGroup4.setOpaque(false);
        pnlGroup4.setBorder(blackBorder);
        cp.add(pnlGroup4);

        JLabel lblNumObject = new JLabel("Verschieben");
        lblNumObject.setBounds(8,8,100,16);
        pnlGroup4.add(lblNumObject);

        JLabel lblObjectIdx = new JLabel("Objekt Nr.");
        lblObjectIdx.setBounds(8, 28, 70, 16);
        pnlGroup4.add(lblObjectIdx);

        spnObjectIdx.setBounds(79, 28, 50, 20);
        pnlGroup4.add(spnObjectIdx);

        JButton btnMoveUp = new JButton();
        btnMoveUp.setBounds(56, 56, 25, 25);
        btnMoveUp.setToolTipText("5 Pixel nach oben");
        btnMoveUp.setIcon(loadImageIcon(3));
        btnMoveUp.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    Integer index = (Integer)spnObjectIdx.getValue();
                    fb.verschiebeUm(index, 0, -5);
                    pnlFiguren.invalidate();
                    pnlFiguren.repaint();
                }
            });
        pnlGroup4.add(btnMoveUp);

        JButton btnMoveLeft = new JButton();
        btnMoveLeft.setBounds(56-25-3, 56+25+3, 25, 25);
        btnMoveLeft.setToolTipText("5 Pixel nach links");
        btnMoveLeft.setIcon(loadImageIcon(2));
        btnMoveLeft.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    Integer index = (Integer)spnObjectIdx.getValue();
                    fb.verschiebeUm(index, -5, 0);
                    pnlFiguren.invalidate();
                    pnlFiguren.repaint();
                }
            });
        pnlGroup4.add(btnMoveLeft);

        JButton btnMoveRight = new JButton();
        btnMoveRight.setBounds(56+25+3, 56+25+3, 25, 25);
        btnMoveRight.setToolTipText("5 Pixel nach rechts");
        btnMoveRight.setIcon(loadImageIcon(0));
        btnMoveRight.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    Integer index = (Integer)spnObjectIdx.getValue();
                    fb.verschiebeUm(index, 5, 0);
                    pnlFiguren.invalidate();
                    pnlFiguren.repaint();
                }
            });
        pnlGroup4.add(btnMoveRight);

        JButton btnMoveDown = new JButton();
        btnMoveDown.setBounds(56, 56+2*(25+3), 25, 25);
        btnMoveDown.setToolTipText("5 Pixel nach unten");
        btnMoveDown.setIcon(loadImageIcon(1));
        btnMoveDown.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    Integer index = (Integer)spnObjectIdx.getValue();
                    fb.verschiebeUm(index, 0, 5);
                    pnlFiguren.invalidate();
                    pnlFiguren.repaint();
                }
            });
        pnlGroup4.add(btnMoveDown);

        JLabel lblNach = new JLabel("nach");
        lblNach.setBounds(8, 56+3*(25+3)+5, 100, 16);
        pnlGroup4.add(lblNach);

        JLabel lblX = new JLabel("x:");
        lblX.setBounds(8, lblNach.getY() + 24, 25, 16);
        pnlGroup4.add(lblX);

        JTextField numX = new JTextField();
        numX.setBounds(31, lblX.getY(), 31, 20);
        numX.setText("150");
        pnlGroup4.add(numX);

        JLabel lblY = new JLabel("y:");
        lblY.setBounds(70, lblX.getY(), 20, 16);
        pnlGroup4.add(lblY);

        JTextField numY = new JTextField();
        numY.setBounds(31+62, lblX.getY(), 31, 20);
        numY.setText("273");
        pnlGroup4.add(numY);

        JButton btnMoveTo = new JButton("Verschieben");
        btnMoveTo.setBounds(8, lblX.getY() + 28, 121, 20);
        btnMoveTo.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    Integer index = (Integer)spnObjectIdx.getValue();
                    int x = 100;
                    try
                    {
                        x = Integer.parseInt(numX.getText());
                    }
                    catch(Exception ex) {}
                    int y = 100;
                    try
                    {
                        y = Integer.parseInt(numY.getText());
                    }
                    catch(Exception ex) {}
                    fb.verschiebeNach(index, x, y);
                    pnlFiguren.invalidate();
                    pnlFiguren.repaint();
                }
            });
        pnlGroup4.add(btnMoveTo);

        setMinimumSize(new Dimension(600, 595));
        addComponentListener(new ComponentAdapter() {
                public void componentResized(ComponentEvent e) {
                    pnlFiguren.setBounds(153,8,getWidth()-178,getHeight()-58);
                }
            });

        setResizable(true);
        setVisible(true);
    } 

    private ImageIcon loadImageIcon(int nRotate) {
        ImageIcon imgIcon = null;
        try {
            BufferedImage img; //= javax.imageio.ImageIO.read(new java.io.File("arrow.png"));
            java.io.InputStream input = FigurenFrame.class.getResourceAsStream("arrow.png");
            if (input != null) {
//                input = FigurenFrame.class.getClassLoader().getResourceAsStream("arrow.png");
                img = javax.imageio.ImageIO.read(input);
            }
            else
            {
                img = javax.imageio.ImageIO.read(new java.io.File("arrow.png"));
            }


            final double rads = Math.toRadians(90*nRotate);
            final double sin = Math.abs(Math.sin(rads));
            final double cos = Math.abs(Math.cos(rads));
            final int w = (int) Math.floor(img.getWidth() * cos + img.getHeight() * sin);
            final int h = (int) Math.floor(img.getHeight() * cos + img.getWidth() * sin);
            final BufferedImage rotatedImage = new BufferedImage(w, h, img.getType());
            final java.awt.geom.AffineTransform at = new java.awt.geom.AffineTransform();
            at.translate(w / 2, h / 2);
            at.rotate(rads,0, 0);
            at.translate(-img.getWidth() / 2, -img.getHeight() / 2);
            final AffineTransformOp rotateOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
            rotateOp.filter(img,rotatedImage);
            Image newimg = rotatedImage.getScaledInstance(22, 22, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
            imgIcon = new ImageIcon(newimg);
        } catch (Exception ex) {

        }
        return imgIcon;
    }

} 
