views:

35

answers:

1

Hi Guys,

I have a 3 X 3 grid of JLabels of Images. I would like to rotate the the label in [2][2]. I know how to do rotation.

my question is what should i do to be able to rotate that cell. Do i have to convert that cell into an image object or is it possible to rotate the label just like that?

Thanks for your answer.

EDIT

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

public class CrossingRobot 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 int cyclesToGo;

     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 = Toolkit.getDefaultToolkit().getImage("images/horizontalCrossing.JPG");

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

     } 

    public CrossingRobot(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 GeneralPath getPath() {
        return path;
    }
    public void setPath(GeneralPath path) {
        this.path = path;
    }

    private void constructInterfaceComponents(){
    }




 public void paintComponent(Graphics grp){ 

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

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



        //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 int getDegreesToTurn() {
        return degreesToTurn;
    }

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




    private class MoveListener implements ActionListener {



        public MoveListener(){

        }   

            public void actionPerformed(ActionEvent e) {

                if (cyclesToGo-- > 0){ 
                    rotateCrossing();

                }else {
                    CrossingRobot.this.stop(); // call outer class methods
                     CrossingRobot.this.finishRotation = true; // call outer class methods
                     cyclesToGo = 0;
               }
           }
    }

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


     void start(int degreesToTurn) {
            if (timer != null) {
                timer.start();
            }
            new MoveListener();
            this.setDegreesToTurn(degreesToTurn);
            cyclesToGo = this.degreesToTurn / ROTATE_ANGLE_OFFSET;

        }

     void stop() {
            timer.stop();

        }






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



}
+2  A: 

I once created a class for that: RotatedLabel

Joey
Nice little label :)
willcodejavaforfood
Thank you very much for your reply. What should i do to test it. I am running it but don't how to make it rotate.
Edy Moore
@Edy: It's a drop-in replacement for `JLabel` but it has a `direction` property. Just use `setDirection()`.
Joey
Thanks. I have a little question. Can you modify it a little bit so that i can give it a value say 90 or 180 and it will rotate that degree. I am trying but it is not working. I need something like that for my project.Thanks
Edy Moore
I have edited my code and add a class that i use to do rotation base on the supplied argument. I would like to do the same with your code. Help me how to make the necessary changes so that it can work for me
Edy Moore
Apparently you want an arbitrary rotation angle and animation. It wasn't obvious from your original question that you wanted that. In this case my answer is useless. If you would be so kind to remove the accepted state from the answer that I can delete it.
Joey