Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 利用变换矩阵进行形状旋转_Java_Arrays_Matrix_Graphics_2d - Fatal编程技术网

Java 利用变换矩阵进行形状旋转

Java 利用变换矩阵进行形状旋转,java,arrays,matrix,graphics,2d,Java,Arrays,Matrix,Graphics,2d,我正试图编写一个简单的程序,将随机生成的多边形旋转一个给定的角度。但是,该算法不执行必要的任务。有人能帮忙解决这个问题吗?当您按下旋转按钮时,图形将消失 public class ObjectFrame { public int[] x,y; private JFrame frame; public static double roundedCos(int n) { String formatString = "0.##"; Lo

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



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);




    }

}

公共类对象框架{
公共int[]x,y;
私有JFrame;
公共静态双循环COS(整数n){
字符串格式String=“0.##”;
Locale currentLocale=Locale.getDefault();
DecimalFormatSymbols otherSymbols=新的DecimalFormatSymbols(当前语言环境);
其他符号。设置小数分隔符('.');
DecimalFormat df=新的DecimalFormat(格式字符串、其他符号);
字符串roundedValue=df.format(Math.cos(n));
Double result=Double.parseDouble(roundedValue);
返回结果;
}
公共静态双循环SIN(整数n){
字符串格式String=“0.##”;
Locale currentLocale=Locale.getDefault();
DecimalFormatSymbols otherSymbols=新的DecimalFormatSymbols(当前语言环境);
其他符号。设置小数分隔符('.');
DecimalFormat df=新的DecimalFormat(格式字符串、其他符号);
字符串roundedValue=df.format(Math.sin(n));
Double result=Double.parseDouble(roundedValue);
返回结果;
}
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
ObjectFrame窗口=新建ObjectFrame();
window.frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建应用程序。
*/
公共对象框架(){
初始化();
}
/**
*初始化框架的内容。
*/
私有void初始化(){
frame=新的JFrame();
机架立根(100100500350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel面板=新的JPanel();
嵌板.立根(10,11,320,272);
frame.getContentPane().add(面板);
JSpinner spnAngle=新的JSpinner();
设置模型(新喷丝头编号模型(0,0,360,1));
立根点(375,184,58,20);
frame.getContentPane().add(spnAngle);
JSpinner spnPoly=新的JSpinner();
spnPoly.setModel(新喷丝头编号模型(0,0,8,1));
spnPoly.setBounds(378,44,55,20);
frame.getContentPane().add(spnPoly);
JLabel lblPoly=新JLabel(“设置多边形”);
lblPoly.立根(375,19,74,14);
frame.getContentPane().add(lblPoly);
JLabel lblAngle=新JLabel(“设置角度”);
lblAngle.挫折(375,159,67,14);
frame.getContentPane().add(lblAngle);
JButton btnDrawPoly=新JButton(“绘制多边形”);
btnDrawPoly.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
int polyCount=(int)spnPoly.getValue();
Graphics g=panel.getGraphics();

如果(polycount),我看到的第一个问题是,你试图使用字符串表示进行舍入。不要这样做,使用
Math.round()
。无论如何,你为什么要舍入呢?第二件事,你正在覆盖你的
x
坐标,然后用它来计算
y
。我可以问你在哪里使用矩阵吗?我想你会使用它