java 将java中的图像旋转指定的角度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14884480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rotate an image in java by the specified angle
提问by Manas Bajaj
Here's the function which draws a shape at the given coordinates:
这是在给定坐标处绘制形状的函数:
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);
}
I want to add one more variable to the above function called 'angle', so that the image is also rotated by the angle specified (drawTank(int x,int y,int angle).
我想在上述函数中再添加一个名为“angle”的变量,以便图像也旋转指定的角度(drawTank(int x,int y,int angle)。
Updated with example
更新示例
What I tried to do is that I initialized Graphics2D and changed my code respectively:
我试图做的是初始化 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)));
But, this doesn't actually do anything. :/
但是,这实际上没有任何作用。:/
采纳答案by MadProgrammer
Precedence matters...
优先级很重要...
In your second example, you're apply a rotation AFTER you've drawn everything. This is not how graphics works. You need to apply the transformation first, then everything that follows will use that transformation.
在您的第二个示例中,您将在绘制完所有内容后应用旋转。这不是图形的工作方式。您需要先应用转换,然后接下来的所有内容都将使用该转换。
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();
}
}
}
You might like to take a look at Transforming Shapes, Text and Imagesfor more information
您可能想查看转换形状、文本和图像以获取更多信息
回答by darkpbj
Here's the brute-force way to do it. Take a look at Java.lang.Math and java.awt.Graphics (which you already have an instance of) With this you can use the draw polygon function to compute the points of your rectangle, which you can do using the sin and cos functions in Java.lang.Math
这是执行此操作的蛮力方法。看看 Java.lang.Math 和 java.awt.Graphics(你已经有一个实例)有了这个,你可以使用绘制多边形函数来计算矩形的点,你可以使用 sin 和 cos Java.lang.Math 中的函数
Really, you'd only need to compute two points this way, as your starting point would be the 90degree vertex from which you would calculate the two adjoining coordinates with. From there it's just a matter of doing some addition and subtraction with the points that you have and your dimension values to get the last point kitty-corner to your starting point.
实际上,您只需要以这种方式计算两个点,因为您的起点将是 90 度顶点,您将从该顶点计算两个相邻坐标。从那里开始,只需对您拥有的点和尺寸值进行一些加法和减法,即可将最后一个点小猫角移到起点。
I'd figure it out for you, and write some example code, but then what fun would that leave you?
我会为你解决这个问题,并编写一些示例代码,但那会给你带来什么乐趣呢?