1. <legend id='SCFQD'><style id='SCFQD'><dir id='SCFQD'><q id='SCFQD'></q></dir></style></legend>

      <i id='SCFQD'><tr id='SCFQD'><dt id='SCFQD'><q id='SCFQD'><span id='SCFQD'><b id='SCFQD'><form id='SCFQD'><ins id='SCFQD'></ins><ul id='SCFQD'></ul><sub id='SCFQD'></sub></form><legend id='SCFQD'></legend><bdo id='SCFQD'><pre id='SCFQD'><center id='SCFQD'></center></pre></bdo></b><th id='SCFQD'></th></span></q></dt></tr></i><div id='SCFQD'><tfoot id='SCFQD'></tfoot><dl id='SCFQD'><fieldset id='SCFQD'></fieldset></dl></div>

    2. <tfoot id='SCFQD'></tfoot>

        <small id='SCFQD'></small><noframes id='SCFQD'>

          <bdo id='SCFQD'></bdo><ul id='SCFQD'></ul>

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

        Rotate an image in java by the specified angle(将java中的图像旋转指定角度)

        1. <i id='WU9qt'><tr id='WU9qt'><dt id='WU9qt'><q id='WU9qt'><span id='WU9qt'><b id='WU9qt'><form id='WU9qt'><ins id='WU9qt'></ins><ul id='WU9qt'></ul><sub id='WU9qt'></sub></form><legend id='WU9qt'></legend><bdo id='WU9qt'><pre id='WU9qt'><center id='WU9qt'></center></pre></bdo></b><th id='WU9qt'></th></span></q></dt></tr></i><div id='WU9qt'><tfoot id='WU9qt'></tfoot><dl id='WU9qt'><fieldset id='WU9qt'></fieldset></dl></div>

            <tbody id='WU9qt'></tbody>
          • <small id='WU9qt'></small><noframes id='WU9qt'>

            <legend id='WU9qt'><style id='WU9qt'><dir id='WU9qt'><q id='WU9qt'></q></dir></style></legend><tfoot id='WU9qt'></tfoot>
            • <bdo id='WU9qt'></bdo><ul id='WU9qt'></ul>

                  本文介绍了将java中的图像旋转指定角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  我想在上面的函数中再添加一个名为'angle'的变量,这样图像也可以旋转指定的角度(drawTank(int x,int y,int angle).

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

                  更新示例

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

                  What I tried to do is that I initialized Graphics2D and changed my code respectively:

                  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. :/

                  推荐答案

                  优先级很重要...

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

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

                  您可能想看看 Transforming Shapes, Text and图片了解更多信息

                  You might like to take a look at Transforming Shapes, Text and Images for more information

                  这篇关于将java中的图像旋转指定角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                  Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                  About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                  What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                  Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                  Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)

                        <tbody id='c1J8h'></tbody>
                      <i id='c1J8h'><tr id='c1J8h'><dt id='c1J8h'><q id='c1J8h'><span id='c1J8h'><b id='c1J8h'><form id='c1J8h'><ins id='c1J8h'></ins><ul id='c1J8h'></ul><sub id='c1J8h'></sub></form><legend id='c1J8h'></legend><bdo id='c1J8h'><pre id='c1J8h'><center id='c1J8h'></center></pre></bdo></b><th id='c1J8h'></th></span></q></dt></tr></i><div id='c1J8h'><tfoot id='c1J8h'></tfoot><dl id='c1J8h'><fieldset id='c1J8h'></fieldset></dl></div>
                      <legend id='c1J8h'><style id='c1J8h'><dir id='c1J8h'><q id='c1J8h'></q></dir></style></legend>
                          <bdo id='c1J8h'></bdo><ul id='c1J8h'></ul>

                            <small id='c1J8h'></small><noframes id='c1J8h'>

                          • <tfoot id='c1J8h'></tfoot>