需要解决旋转图像问题的帮助 [英] Need Help In Solving Rotation Image Problem

查看:60
本文介绍了需要解决旋转图像问题的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我。我花了数小时试图解决它,但没有用。

I need your help please. I have spent hours trying to solve it but not working.

当用户单击按钮时,我正在旋转一个图像。

I have an image i am rotating when the user clicks on a button. But it is not working.

我希望看到图像逐渐旋转直到停止,但没有旋转。
这就是它的作用。单击按钮后,我看不到它在旋转。但是当我最小化和最大化主窗口时,
我看到图像只是像这样快速旋转(翻转)。它可以旋转,但是我看不到它在旋转。在最小化和最大化主窗口之后,它只是旋转一秒钟。

I would like to see the image rotating gradually till it stops but it doesn't. This it what it does. After i click the button, i don't see it rotating. But when i minimize and maximize the main window, i see the image just rotate(flip) fast like that. It does the rotation but i don't see it as it is doing. It just rotate in a second after minimize and maximize the main window.

我认为问题在于在旋转时更新GUI,但我不知道如何修理它。

I thimk the problem deals with updating the GUI as it is rotating but i don't know how to fix it.

这些是代码。

public class KrusPanel extends JPanel{ 
     private Image crossingImage;
     private int currentRotationAngle;
     private int imageWidth;
     private int imageHeight;
     private AffineTransform affineTransform;
     private boolean clockwise; 
     private static int ROTATE_ANGLE_OFFSET = 2;

     private int xCoordinate;
     private int yCoordinate;

     private javax.swing.Timer timer;

     private void initialize(){

      this.crossingImage = Toolkit.getDefaultToolkit().getImage("images/railCrossing3.JPG");
      this.imageWidth = this.getCrossingImage().getWidth(this);
      this.imageHeight = this.getCrossingImage().getHeight(this);
      this.affineTransform = new AffineTransform();
      this.setCurrentRotationAngle(90);
      timer = new javax.swing.Timer(20, new MoveListener());

     } 

 public KrusPanel (int x, int y) {

  this.setxCoordinate(x);
  this.setyCoordinate(y);
  this.setPreferredSize(new Dimension(50, 50));
  this.setBackground(Color.red);
  TitledBorder border = BorderFactory.createTitledBorder("image");
  this.setLayout(new FlowLayout());
  this.initialize();

 }


 public void paintComponent(Graphics grp){ 

           Rectangle rect = this.getBounds();
           Graphics2D g2d = (Graphics2D)grp;
           g2d.setColor(Color.BLACK);
           this.getAffineTransform().setToTranslation(this.getxCoordinate(), this.getyCoordinate());

           this.getAffineTransform().rotate(Math.toRadians(this.getCurrentRotationAngle()), this.getCrossingImage().getWidth(this) /2, 
                                   this.getCrossingImage().getHeight(this)/2);

          g2d.drawImage(this.getCrossingImage(), this.getAffineTransform(), this);
      }



 public void rotateCrossing(){
             this.currentRotationAngle += ROTATE_ANGLE_OFFSET;

             int test = this.currentRotationAngle % 90;
             if(test == 0){
              this.setCurrentRotationAngle(this.currentRotationAngle);
              timer.stop();

             }


         repaint(); 
 }



  private class MoveListener implements ActionListener {

         public void actionPerformed(ActionEvent e) {

            rotateCrossing();
            repaint();

     }

  }

//  There are getters and setters method here but i have removed them to make the code shorter. 



}




Next 2 Classes

// I have removed many thins in this class so simplicity. This class is consists of Tiles of BufferdImage and the 
// KrusPanel class is at the array position [2][2]. It is the KrusPanel class that i want to rotate.


public class SeePanel extends JPanel{

      private static KrusPanel crossing;



      private void initializeComponents(){

   timer = new javax.swing.Timer(70, new MoveListener());
   this.crossing = new CrossingPanel(261,261);
  }






    public SeePanel(){ 
  this.initializeComponents();

     }


     public void paintComponent(Graphics comp){
   super.paintComponent(comp); 
  comp2D = (Graphics2D)comp;
  BasicStroke pen = new BasicStroke(15.0F, BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND);
     comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
     comp2D.setPaint(Color.red);
     comp2D.setBackground(Color.WHITE);
     comp2D.drawImage(img, 0, 0, null);
     comp2D.draw(this.horizontalRail); 
     this.crossing.paintComponent(comp2D); 
     this.rob.drawRobot(comp2D);

 }


  public static void rotateCrossing(){

   this.crossing.getTimer().start();
   repaint();


// i tried below code also it didn't work. so i put them in comments
    /*
   Runnable rotateCrossing1 = new Runnable(){  // to be removed
         public void run() {
        crossing.getTimer().start();
       }
      };
      SwingUtilities.invokeLater(rotateCrossing1);
    */

  }

}





// MAIN CLASS
// This is the GUI class which consists of buttons and others

public class MainAPP{

   SeePanel = new SeePanel();
  // Buttons declarations here and others

public MainAPP(){
  // all listener registers here
}


// Here i call the rotateCrossing() of the SeePanel class but it is not working as i want.
 // I would like to see the image rotating gradually till it stops but it doesn't. 
//This it what it does. After i click the button, i don't see it rotating. But when i minimize and maximize the main window,
// i see the image  just rotate(flip) fast like that.

public void actionPerformed(ActionEvent e){

    SeePanel.rotateCrossing();                  
}


    public static main(string[] str){

    }


}

请帮我修复它。

谢谢

推荐答案

似乎应该可以正常工作。我整理了一下代码,对我来说还不错:

It seems like it should work fine. I cleaned up your code a bit and it worked fine for me:

public class KrusPanel extends JPanel {
private Image crossingImage;
private int currentRotationAngle;
private AffineTransform affineTransform;
private static int ROTATE_ANGLE_OFFSET = 2;

private int xCoordinate;
private int yCoordinate;

private Timer timer;

private void initialize() {

    crossingImage = Toolkit.getDefaultToolkit().getImage("images/railCrossing3.jpg");
    crossingImage.getWidth(this);
    crossingImage.getHeight(this);
    affineTransform = new AffineTransform();
    currentRotationAngle = 90;
    timer = new Timer(20, new MoveListener());

    addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            start();
        }
    });

}

public KrusPanel(int x, int y) {

    xCoordinate = x;
    yCoordinate = y;
    setPreferredSize(new Dimension(50, 50));
    setBackground(Color.red);
    setLayout(new FlowLayout());
    initialize();
}

@Override
public void paintComponent(Graphics grp) {

    Graphics2D g2d = (Graphics2D) grp;
    g2d.setColor(Color.BLACK);
    affineTransform = AffineTransform.getTranslateInstance(xCoordinate, yCoordinate);

    affineTransform.rotate(Math.toRadians(currentRotationAngle), crossingImage.getWidth(this) / 2, crossingImage.getHeight(this) / 2);

    g2d.drawImage(crossingImage, affineTransform, this);
}

public void rotateCrossing() {

    currentRotationAngle += ROTATE_ANGLE_OFFSET;

    if (currentRotationAngle % 90 == 0) {
        timer.stop();
    }

    repaint();
}

private class MoveListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        rotateCrossing();
        // repaint();

    }

}

void start() {
    if (timer != null) {
        timer.start();
    }
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(320, 240);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new KrusPanel(50, 50));

    frame.setVisible(true);
}
}

我单击图像,每次旋转90度

I click the image and it rotates 90 degrees each time.

这篇关于需要解决旋转图像问题的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆