Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.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_Image_Rotation_Graphics2d - Fatal编程技术网

以指定角度旋转java中的图像

以指定角度旋转java中的图像,java,image,rotation,graphics2d,Java,Image,Rotation,Graphics2d,以下是在给定坐标处绘制形状的函数: public void drawTank(int x,int y){ int h = 50; int w = 50; graphic.setColor(Color.darkGray); graphic.drawRect(x, y, h, w); graphic.fillRect(x, y, h, w); graphic.setColor(Color.GRAY); graphic.drawRect(x+50, y+20

以下是在给定坐标处绘制形状的函数:

public void drawTank(int x,int y){
   int h = 50;
   int w = 50;

   graphic.setColor(Color.darkGray);
   graphic.drawRect(x, y, h, w);
   graphic.fillRect(x, y, h, w);
   graphic.setColor(Color.GRAY);
   graphic.drawRect(x+50, y+20, 35, 10);
   graphic.fillRect(x+50, y+20, 35, 10);
}
我想在上述函数中添加一个名为“angle”的变量,以便图像也按指定的角度旋转(drawTank(intx,inty,intangle)

用示例更新

我试图做的是分别初始化Graphics2D和更改代码:

g2D.setColor(Color.darkGray);
g2D.drawRect(x, y, h, w);
g2D.fillRect(x, y, h, w);
g2D.setColor(Color.red);
g2D.drawRect(x+50, y+20, 35, 10);
g2D.fillRect(x+50, y+20, 35, 10);
g2D.rotate((Math.toRadians(angle)));

但是,这实际上没有任何作用。///p>这是一种蛮力方法。看看Java.lang.Math和Java.awt.Graphics(您已经有了一个实例),您可以使用draw polygon函数来计算矩形的点,这可以使用Java.lang.Math中的sin和cos函数来完成

实际上,你只需要用这种方法计算两个点,因为你的起点是90度的顶点,从这里你可以计算两个相邻的坐标。从这里开始,你只需要对你拥有的点和你的尺寸值做一些加减运算,就可以得到你的sta的最后一个点kitty角点

我会帮你找到答案,并编写一些示例代码,但那会给你带来什么乐趣呢?

优先级很重要

在第二个示例中,在绘制完所有内容后应用旋转。这不是图形的工作方式。您需要首先应用转换,然后接下来的所有内容都将使用该转换

public class TestRotateImage {

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

    public TestRotateImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

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

    public class TestPane extends JPanel {

        private JSlider slider;
        private Rectangle rectangle;

        public TestPane() {
            setLayout(new BorderLayout());
            rectangle = new Rectangle(0, 0, 100, 100);
            slider = new JSlider();
            slider.setMinimum(0);
            slider.setMaximum(360);
            slider.setMinorTickSpacing(5);
            slider.setMajorTickSpacing(10);
            slider.setValue(0);
            add(slider, BorderLayout.SOUTH);
            slider.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    repaint();
                }
            });
        }

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

        public double getAngle() {

            return Math.toRadians(slider.getValue());

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();

            g2d.setColor(Color.RED);
            g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
            g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);

            g2d.setColor(Color.BLACK);
            int x = (getWidth() - rectangle.width) / 2;
            int y = (getHeight() - rectangle.height) / 2;
            AffineTransform at = new AffineTransform();
            at.setToRotation(getAngle(), x + (rectangle.width / 2), y + (rectangle.height / 2));
            at.translate(x, y);
            g2d.setTransform(at);
            g2d.draw(rectangle);
            g2d.dispose();

        }

    }

}


您可以查看更多信息

1),如果图形变量是<代码> GraceS2D2>代码>对象,请考虑在图形对象上使用<代码> AffixToels/COD>。2) 此方法不应该有一个
Graphics
或更好的
Graphics2D
参数,以便可以从JComponent的
paintComponent(Graphics g)
方法(如果是Swing)传入当前活动的图形对象吗?是的,复制图形,这样就不必重新设置转换。@hoverCraftFullOfels是的,+1表示懒惰;)