Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.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 旋转图形2d_Java_Swing_Rotation_Graphics2d - Fatal编程技术网

Java 旋转图形2d

Java 旋转图形2d,java,swing,rotation,graphics2d,Java,Swing,Rotation,Graphics2d,我正在尝试旋转图形,但由于某些原因,它无法工作。我在其他论坛上做了很多研究,但似乎无法解决这个问题 所以,这是我的程序工作的一部分 接收一个文件 创建缓冲图像 从buffereImage.createGraohics()创建图形2D 在显示缓冲图像的窗格中创建Jlabel。 然后我有了一个在图像上写文本的方法,还有一个保存图像的方法。 如果我写文本,然后保存,效果很好。 它使用: graphic2D.drawString("Hello, this is my test.",10,10); 我还

我正在尝试旋转图形,但由于某些原因,它无法工作。我在其他论坛上做了很多研究,但似乎无法解决这个问题

所以,这是我的程序工作的一部分

  • 接收一个文件
  • 创建缓冲图像
  • buffereImage.createGraohics()创建图形2D
  • 在显示缓冲图像的窗格中创建
    Jlabel
    。 然后我有了一个在图像上写文本的方法,还有一个保存图像的方法。 如果我写文本,然后保存,效果很好。 它使用:

    graphic2D.drawString("Hello, this is my test.",10,10);
    
    我还看到了JLabel的更新,因此我可以看到图像上的文本

    但是,如果我使用以下方法创建方法:

    graphics2D.rotate(45);
    
    我看绝对没有什么变化

    有人知道为什么吗

    这是我的方法:

    public void actionPerformed(ActionEvent e){     
        graphic2D.rotate(4.5);
        saveImage();
    }
    
    谢谢

        import java.io.File;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.imageio.*;
    import java.awt.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    class EditorView implements ActionListener, ChangeListener{
    
        JFrame editorFrame;
        JPanel container = new JPanel();
        JPanel toolbox = new JPanel();
        JPanel editorPanel = new JPanel();
        JScrollPane editorScrollPane;
        File imageFile;
        BufferedImage bi;
        ImageIcon ii;
        Graphics2D graphic2D;
        JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 10, 5);
    
        public EditorView(File file){
            this.imageFile = file;
    
            try{
            bi = ImageIO.read(imageFile);
            }catch(Exception e){}
    
            graphic2D = bi.createGraphics();
    
            createAndShowGUI();
    
        }
    
    
        void createAndShowGUI(){
    
            editorFrame = new JFrame("Editor");
            editorFrame.setSize(1000,650);  
            container.setLayout(new BoxLayout(container,BoxLayout.Y_AXIS));
            editorFrame.add(container);
            toolbox.setMaximumSize(new Dimension(Integer.MAX_VALUE,50));
            toolbox.setPreferredSize(new Dimension(Integer.MAX_VALUE,50));
            toolbox.setLayout(new GridLayout(2,10));
            JButton rotateLeftBtn = new JButton("Rotate Left");
            toolbox.add(rotateLeftBtn);
            rotateLeftBtn.addActionListener(this);
            toolbox.add(new JButton("Save"));
            toolbox.add(new JButton("Close"));
            toolbox.add(new JButton("Freeform"));
            toolbox.add(new JButton("Colour"));
            toolbox.add(new JButton("Text"));
            toolbox.add(new JButton("Square"));
            toolbox.add(new JButton("Circle"));
            toolbox.add(new JButton("Elipse"));
            toolbox.add(new JButton("Rotate Right"));
    
    
            slider.addChangeListener(this);
            toolbox.add(slider);
    
    
            container.add(toolbox);
    
            editorScrollPane = new JScrollPane(new JLabel(new ImageIcon(bi)));
    
            container.add(editorScrollPane);
            editorFrame.setVisible(true);
            editorFrame.validate();
            container.validate();
            editorPanel.validate();
    
            drawLabel();
            //editorScrollPane.repaint();
        }
    
        void drawLabel(){
            graphic2D.rotate(1);
            graphic2D.drawString("Hello, this is my test.",10,10);
    
        }
    
        void drawCircle(){
    
        }
    
        void saveImage(){
    
            try{
            ImageIO.write(bi,getFileExtension(), imageFile);
            }catch(Exception e){}
        }
    
        String getFileExtension(){
            int positionOfDot = imageFile.getName().lastIndexOf(".");
            String returner = null;
            if( positionOfDot !=-1){
                returner = imageFile.getName().substring((positionOfDot+1), imageFile.getName().length());
            }
            System.out.println(returner);
            return returner;
        }
    
        public void actionPerformed(ActionEvent e){ 
    
            graphic2D.rotate(1);
            saveImage();
    
        }
    
        public void stateChanged(ChangeEvent c){
            graphic2D.scale(slider.getValue()/5, slider.getValue()/5);
            editorScrollPane.repaint();
    
        }
    
    }
    

    graphics2D.rotate
    调用转换后续渲染,因此您需要在渲染文本之前重新绘制并放置rotate调用

    另见:


    此外,该方法要求输入以弧度为单位。

    graphics2D.rotate
    调用转换后续渲染,因此您需要在渲染文本之前重新绘制并放置rotate调用

    另见:

    此外,该方法要求输入以弧度为单位。

    对于

    graphics2D.rotate();
    
    方法,试试看

    graphics2D.rotate(Math.toRadians(45));
    
    为了

    graphics2D.rotate();
    
    方法,试试看

    graphics2D.rotate(Math.toRadians(45));
    

    您可以发布整个上下文(即
    旋转
    调用和
    抽绳
    )吗?您可以发布整个上下文(即
    旋转
    调用和
    抽绳
    )吗?谢谢您的帮助。我还是不知道该怎么做。这是我在应用程序中的一个框架的视图,该代码驻留在其中。你能告诉我哪里出了问题吗?非常感谢。(在问题中添加了代码)。这是我试图旋转的文件中拍摄的整个图像,包括添加到其中的任何内容(如标签)。目前,旋转是针对任何按钮执行的操作。那只是因为它还没有完全完成。好吧,在画正方形和旋转的过程中做了更多的混乱之后。。我发现我实际上可以旋转,但我加载的实际图像不会随着Graphic2D旋转。我在想,当我调用createGraphic时,图像会出现在graphic2d上?@user1068470:有一个完整的示例。谢谢您的帮助。我还是不知道该怎么做。这是我在应用程序中的一个框架的视图,该代码驻留在其中。你能告诉我哪里出了问题吗?非常感谢。(在问题中添加了代码)。这是我试图旋转的文件中拍摄的整个图像,包括添加到其中的任何内容(如标签)。目前,旋转是针对任何按钮执行的操作。那只是因为它还没有完全完成。好吧,在画正方形和旋转的过程中做了更多的混乱之后。。我发现我实际上可以旋转,但我加载的实际图像不会随着Graphic2D旋转。我在想,当我称之为createGraphic时,图像会出现在graphic2d上?@user1068470:有一个完整的例子。