Java 旋转不是我想要的

Java 旋转不是我想要的,java,image-processing,rotation,Java,Image Processing,Rotation,我有一个类,当我给一个图像一定的旋转度时,它会旋转一个图像,但是它没有按照我想要的那样工作。问题是这样的,例如,如果我旋转90度,旋转交叉()中的变量“currentRotationAngle”在旋转后逐渐设置为90。这是因为我总是按偏移量(10)增加该变量 下次我想旋转180度,因为变量已经是90度了,它只会再旋转90度(90+90=180),而不是整个旋转180度。我试图通过在“stop()”方法中将“currentRotationAngle”设置为0来修复它,只要旋转完成,它就会让旋转返回

我有一个类,当我给一个图像一定的旋转度时,它会旋转一个图像,但是它没有按照我想要的那样工作。问题是这样的,例如,如果我旋转90度,旋转交叉()中的变量“currentRotationAngle”在旋转后逐渐设置为90。这是因为我总是按偏移量(10)增加该变量

下次我想旋转180度,因为变量已经是90度了,它只会再旋转90度(90+90=180),而不是整个旋转180度。我试图通过在“stop()”方法中将“currentRotationAngle”设置为0来修复它,只要旋转完成,它就会让旋转返回到起始位置

这就是我想要的,当我给它度的时候,我想要它从最后一个旋转位置开始精确地旋转到那个度。请看下面的课程

public class CrossingPanel extends JPanel{

    private static final long serialVersionUID = 1L;

    // private data members
     private Image crossingImage;
     private int currentRotationAngle;
     private int imageWidth;
     private int imageHeight;
     private AffineTransform affineTransform;
     private boolean clockwise; 
     private static int ROTATE_ANGLE_OFFSET = 10;

     private boolean finishRotation = false;
     private int degreesToTurn;

     private static int LENGTH = 75;
    private static int RAIL_LENGTH = 130;
    private int ARROWLENGTH = 4;
    private static int TICKSIZE = 10;
    private GeneralPath path;
     private int xCoordinate;
     private int yCoordinate;

     private javax.swing.Timer timer;

     private void initialize(){
        //this.crossingImage = getImage("images/railCrossing.JPG");
         this.crossingImage = Toolkit.getDefaultToolkit().getImage("images/crossingsHorizontal.JPG");
        // MediaTracker mt = new MediaTracker(this);
        // mt.addImage(this.crossingImage, 1);
         this.path = new GeneralPath();
         this.imageWidth = this.getCrossingImage().getWidth(this);
         this.imageHeight = this.getCrossingImage().getHeight(this);
         this.affineTransform = new AffineTransform();
         this.setCurrentRotationAngle(0);
         this.setDegreesToTurn(0);
         timer = new javax.swing.Timer(20, new MoveListener());
           // timer.start();
     } 

    public CrossingPanel(int x, int y/*, BufferedImage img*/) {

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

    }


    public GeneralPath getPath() {
        return path;
    }
    public void setPath(GeneralPath path) {
        this.path = path;
    }

    private void constructInterfaceComponents(){
    }

    private Shape createVerticalRail(){
        //System.out.print("dddd: " + this.LENGTH + "\n");
        this.getPath().moveTo(0, LENGTH);
        this.getPath().lineTo(RAIL_LENGTH,75);
        float cm = 72 / 2.54f;
        float lengthCentimeter = RAIL_LENGTH;

        for (float i = 260.0f, j = 340; i >= 0; i -= 15.0f, j += 20) {
              float tick = i * cm;
            //  path.moveTo(340, i);
            //  path.lineTo(360, i);
        }

        this.getPath().closePath();
        return this.getPath();
    }

    public void paintComponent(Graphics grp){ 

        Rectangle rect = this.getBounds();
        Graphics2D g2d = (Graphics2D)grp;

        //set the background color to black
        g2d.setColor(Color.BLACK);

       // g2d.draw(this.createVerticalRail());

        //set the translation to the mid of the component

        this.getAffineTransform().setToTranslation(this.getxCoordinate(), this.getyCoordinate());

          //rotate with the rotation point as the mid of the image
        this.getAffineTransform().rotate(Math.toRadians(this.getCurrentRotationAngle()), this.getCrossingImage().getWidth(this) /2, 
                                         this.getCrossingImage().getHeight(this)/2);

        //draw the image using the AffineTransform
        g2d.drawImage(this.getCrossingImage(), this.getAffineTransform(), this);
    }


    public void rotateCrossing(int degrees){
        // this condition is there to avoid division of zero
            // and also there is no need to rotate 0 degrees
            if(degrees == 0){
            this.stop();
            return;
        }
        currentRotationAngle += ROTATE_ANGLE_OFFSET;

            if (currentRotationAngle % degrees == 0) {
                timer.stop();
                this.finishRotation = true;
            }

         repaint(); 
    }



     public int getDegreesToTurn() {
        return degreesToTurn;
    }

    public void setDegreesToTurn(int degreesToTurn) {
        this.degreesToTurn = degreesToTurn;
    }



    private class MoveListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {

                rotateCrossing(degreesToTurn);


        }

     }


     void start(int degreesToTurn) {
            if (timer != null) {
                timer.start();
            }
            this.setDegreesToTurn(degreesToTurn);
        }

     void stop() {
            timer.stop();
            this.setCurrentRotationAngle(0);

        }






    public Image getCrossingImage() {
        return crossingImage;
    }
    public void setCrossingImage(Image crossingImage) {
        this.crossingImage = crossingImage;
    }

    public int getCurrentRotationAngle() {
        return currentRotationAngle;
    }
    public void setCurrentRotationAngle(int currentRotationAngle) {
        this.currentRotationAngle = currentRotationAngle;
    }

    public int getImageWidth() {
        return imageWidth;
    }
    public void setImageWidth(int imageWidth) {
        this.imageWidth = imageWidth;
    }

    public int getImageHeight() {
        return imageHeight;
    }
    public void setImageHeight(int imageHeight) {
        this.imageHeight = imageHeight;
    }

    public AffineTransform getAffineTransform() {
        return affineTransform;
    }
    public void setAffineTransform(AffineTransform affineTransform) {
        this.affineTransform = affineTransform;
    }

    public boolean isClockwise() {
        return clockwise;
    }
    public void setClockwise(boolean clockwise) {
        this.clockwise = clockwise;
    }

    public int getxCoordinate() {
        return xCoordinate;
    }
    public void setxCoordinate(int xCoordinate) {
        this.xCoordinate = xCoordinate;
    }

    public int getyCoordinate() {
        return yCoordinate;
    }
    public void setyCoordinate(int yCoordinate) {
        this.yCoordinate = yCoordinate;
    }

    public javax.swing.Timer getTimer() {
        return timer;
    }
    public void setTimer(javax.swing.Timer timer) {
        this.timer = timer;
    }

    public  boolean isFinishRotation() {
        return finishRotation;
    }

    public void setFinishRotation(boolean finishRotation) {
        this.finishRotation = finishRotation;
    }



}
我想找个人帮我修一下


谢谢大家。

这个方法很奇怪,度参数的用途是什么? 我假设您使用相同的度值多次调用此方法。在这种情况下,应使用度的绝对值(270而不是180)

我建议使用以下代码:

private class MoveListener implements ActionListener {
    private int cyclesToGo;
    public MoveListener(){
        cyclesToGo = CrossingPanel.this.degreesToTurn / ROTATE_ANGLE_OFFSET; // how many time to call repaint
    }   

        public void actionPerformed(ActionEvent e) {
            if (cyclesToGo-- > 0) rotateCrossing();
            else {
                CrossingPanel.this.stop(); // call outer class methods
                 CrossingPanel.this.finishRotation = true; // call outer class methods
           }
       }

}
   // this method increments currentRotationAngle and calles repaint
   public void rotateCrossing(){
        currentRotationAngle += ROTATE_ANGLE_OFFSET;
        repaint(); 
   }

请我添加了“旋转”标签,因为我真正关心的是旋转,而不是处理旋转的图像。我在问你是否可以再次添加“旋转”标记。感谢degrees参数帮助第二个if()条件在旋转该度后停止。“currentRotationAngle”(当前旋转角度)将逐渐增加偏移量,因此,当取带度数的模时,它将在旋转该度数后停止旋转。例如,如果我想旋转90,并且“currentRotationAngle”已经由偏移量增加到90,那么两者的模都是0,那么旋转将停止。我希望这里很清楚,但是如果你想旋转90度,你必须调用(int I=0;快速修复方法是将setDegreesTourn主体的设置更改为this.DegreesTourn=DegreesTourn+currentRotationAngle;(currentRotationAngle%度==0)更改为(currentRotationAngle==degrees)。但我会重新考虑整个设计。不。当我第一次调用rotateCrossing(90)时,它旋转得很好。但是“currentRotationAngle”变量现在通过该方法中的语句-currentRotationAngle+=ROTATE_ANGLE_OFFSET;-增加到90。所以下次当你想要它旋转时,让它旋转180,因为“currentRotationAngle”变量已经是90,它将继续增加偏移量直到180,这意味着它将额外旋转90,而不是完全旋转180。我所寻找的是能够旋转到与前一次旋转无关的程度。您建议的for()循环将“currentRotationAngle”变量增加到我认为90*9。
private class MoveListener implements ActionListener {
    private int cyclesToGo;
    public MoveListener(){
        cyclesToGo = CrossingPanel.this.degreesToTurn / ROTATE_ANGLE_OFFSET; // how many time to call repaint
    }   

        public void actionPerformed(ActionEvent e) {
            if (cyclesToGo-- > 0) rotateCrossing();
            else {
                CrossingPanel.this.stop(); // call outer class methods
                 CrossingPanel.this.finishRotation = true; // call outer class methods
           }
       }

}
   // this method increments currentRotationAngle and calles repaint
   public void rotateCrossing(){
        currentRotationAngle += ROTATE_ANGLE_OFFSET;
        repaint(); 
   }