使用变换矩阵进行形状旋转

时间:2019-11-22 13:52:25

标签: java arrays matrix graphics 2d

我正在尝试编写一个简单的程序,该程序会将随机生成的多边形旋转给定角度。但是,此算法不能执行必要的任务。有人可以解决这个问题吗?当您按下旋转按钮时,图形将消失。



public class ObjectFrame {


    public int[] x,y;

    private JFrame frame;
    public static double roundedCos(int n) {
        String formatString = "0.##";
        Locale currentLocale = Locale.getDefault();
        DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
        otherSymbols.setDecimalSeparator('.');
        DecimalFormat df = new DecimalFormat(formatString,otherSymbols);

        String roundedValue = df.format(Math.cos(n));
        Double result = Double.parseDouble(roundedValue);
        return result;
    }
    public static double roundedSin(int n) {
        String formatString = "0.##";
        Locale currentLocale = Locale.getDefault();
        DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
        otherSymbols.setDecimalSeparator('.');
        DecimalFormat df = new DecimalFormat(formatString,otherSymbols);

        String roundedValue = df.format(Math.sin(n));
        Double result = Double.parseDouble(roundedValue);
        return result;
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ObjectFrame window = new ObjectFrame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ObjectFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 500, 350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(10, 11, 320, 272);
        frame.getContentPane().add(panel);





        JSpinner spnAngle = new JSpinner();
        spnAngle.setModel(new SpinnerNumberModel(0, 0, 360, 1));
        spnAngle.setBounds(375, 184, 58, 20);
        frame.getContentPane().add(spnAngle);

        JSpinner spnPoly = new JSpinner();
        spnPoly.setModel(new SpinnerNumberModel(0, 0, 8, 1));
        spnPoly.setBounds(378, 44, 55, 20);
        frame.getContentPane().add(spnPoly);



        JLabel lblPoly = new JLabel("Set a poly");
        lblPoly.setBounds(375, 19, 74, 14);
        frame.getContentPane().add(lblPoly);

        JLabel lblAngle = new JLabel("Set an angle");
        lblAngle.setBounds(375, 159, 67, 14);
        frame.getContentPane().add(lblAngle);

        JButton btnDrawPoly = new JButton("Draw Polygon");
        btnDrawPoly.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {


                int polyCount = (int) spnPoly.getValue();
                Graphics g = panel.getGraphics();
                    if(polyCount<3) {JOptionPane.showMessageDialog(null, "Daudzsturim jabut vairak neka 2 punkti");return;}
                    else{
                        Random val = new Random();
                         x = new int[polyCount];
                         y = new int[polyCount];
                        for(int i=0;i<polyCount;i++) {
                            x[i] = val.nextInt(panel.getWidth());
                            y[i] = val.nextInt(panel.getHeight());
                            }
                        g.setColor(Color.WHITE);
                        g.fillRect(0, 0, panel.getWidth(), panel.getHeight());
                        g.setColor(Color.BLACK);
                        g.drawPolygon(x, y , polyCount);
                        isDrawed = true;


                }
            }

        });

        JButton btnRotatePoly = new JButton("Rotate Shape");
        btnRotatePoly.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int polyCount = (int) spnPoly.getValue();
                int angle = (int) spnAngle.getValue();

                double[] tmpX = new double[polyCount];
                double[] tmpY  = new double[polyCount];
                Graphics g = panel.getGraphics();           
                    for (int i = 0; i < x.length; i++) {
                        tmpX[i] = x[i];
                        tmpY[i] = y[i];                 
                        tmpX[i] = tmpX[i] * roundedCos(angle) - tmpY[i] * roundedSin(angle) ;
                        tmpY[i] = tmpX[i] * roundedSin(angle) + tmpY[i] * roundedCos(angle);
                        x[i] = (int)(tmpX[i]);
                        y[i] = (int)(tmpY[i]);
                    }

                g.setColor(Color.WHITE);
                g.fillRect(0, 0, panel.getWidth(), panel.getHeight());
                g.setColor(Color.BLACK);
                g.drawPolygon(x, y , polyCount);

            }
        });
        btnRotatePoly.setBounds(344, 225, 119, 37);
        frame.getContentPane().add(btnRotatePoly);

        btnDrawPoly.setBounds(350, 83, 113, 32);
        frame.getContentPane().add(btnDrawPoly);




    }

}

将旋转矩阵应用于循环中的坐标的位置可能有误,但我不知道在哪里

0 个答案:

没有答案