0

I am trying to rotate a graphic, but for some reason it won't work. I've done a lot of research looking in other forums but seem to not be able to fix this issue.

So, this is the bit of my program works.

  1. Takes in a file.
  2. Creates buffered image
  3. Creates graphics2D from the bufferedimage.createGraohics();

A Jlabel is made in a pane, which shows the buffered image. I then have a method to write text on the image, and a method to save the image. If I write text, then save, that works fine. This uses:

graphic2D.drawString("Hello, this is my test.",10,10);

I also see this update the JLabel, so I can see the text on the image.

However, if I create a method with:

graphics2D.rotate(45);

I see absolutely nothing change.

Does anyone have any idea why?

This is my method:

public void actionPerformed(ActionEvent e){     
    graphic2D.rotate(4.5);
    saveImage();
}

Thanks,

    import java.io.File;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class EditorView implements ActionListener, ChangeListener{

    JFrame editorFrame;
    JPanel container = new JPanel();
    JPanel toolbox = new JPanel();
    JPanel editorPanel = new JPanel();
    JScrollPane editorScrollPane;
    File imageFile;
    BufferedImage bi;
    ImageIcon ii;
    Graphics2D graphic2D;
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 10, 5);

    public EditorView(File file){
        this.imageFile = file;

        try{
        bi = ImageIO.read(imageFile);
        }catch(Exception e){}

        graphic2D = bi.createGraphics();

        createAndShowGUI();

    }


    void createAndShowGUI(){

        editorFrame = new JFrame("Editor");
        editorFrame.setSize(1000,650);  
        container.setLayout(new BoxLayout(container,BoxLayout.Y_AXIS));
        editorFrame.add(container);
        toolbox.setMaximumSize(new Dimension(Integer.MAX_VALUE,50));
        toolbox.setPreferredSize(new Dimension(Integer.MAX_VALUE,50));
        toolbox.setLayout(new GridLayout(2,10));
        JButton rotateLeftBtn = new JButton("Rotate Left");
        toolbox.add(rotateLeftBtn);
        rotateLeftBtn.addActionListener(this);
        toolbox.add(new JButton("Save"));
        toolbox.add(new JButton("Close"));
        toolbox.add(new JButton("Freeform"));
        toolbox.add(new JButton("Colour"));
        toolbox.add(new JButton("Text"));
        toolbox.add(new JButton("Square"));
        toolbox.add(new JButton("Circle"));
        toolbox.add(new JButton("Elipse"));
        toolbox.add(new JButton("Rotate Right"));


        slider.addChangeListener(this);
        toolbox.add(slider);


        container.add(toolbox);

        editorScrollPane = new JScrollPane(new JLabel(new ImageIcon(bi)));

        container.add(editorScrollPane);
        editorFrame.setVisible(true);
        editorFrame.validate();
        container.validate();
        editorPanel.validate();

        drawLabel();
        //editorScrollPane.repaint();
    }

    void drawLabel(){
        graphic2D.rotate(1);
        graphic2D.drawString("Hello, this is my test.",10,10);

    }

    void drawCircle(){

    }

    void saveImage(){

        try{
        ImageIO.write(bi,getFileExtension(), imageFile);
        }catch(Exception e){}
    }

    String getFileExtension(){
        int positionOfDot = imageFile.getName().lastIndexOf(".");
        String returner = null;
        if( positionOfDot !=-1){
            returner = imageFile.getName().substring((positionOfDot+1), imageFile.getName().length());
        }
        System.out.println(returner);
        return returner;
    }

    public void actionPerformed(ActionEvent e){ 

        graphic2D.rotate(1);
        saveImage();

    }

    public void stateChanged(ChangeEvent c){
        graphic2D.scale(slider.getValue()/5, slider.getValue()/5);
        editorScrollPane.repaint();

    }

}
ThePerson
  • 3,048
  • 8
  • 43
  • 69

2 Answers2

4

The graphics2D.rotate call transforms subsequent renderings, so you need to repaint and put the rotate call just before you render the text.

See also: Javadocs

Also, the method requires the input to be in radians.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
  • Thanks for the help. I still don't know exactly how I am supposed to do this. This is my view for one of the frames in the application, where this code resides. Please can you let me know where I'm going wrong? Much much appreciated. ( Added code to question ). It's the whole image taken in within the file that I am trying to rotate, including anything ( such as the label ) added to it. – ThePerson Dec 08 '11 at 21:51
  • For now, the rotate is in the actionPerformed, for any button. That's just because it's not totally complete yet. – ThePerson Dec 08 '11 at 22:02
  • Ok, so after much more messing around drawing squares and rotating.. I have found that I can actually rotate, but the actual image I load does not rotate with the Graphic2D. I was thinking that as I called createGraphic, the image would be on the graphic2d? – ThePerson Dec 08 '11 at 23:56
  • 1
    @user1068470: There's a complete example [here](http://stackoverflow.com/a/3420651/230513). – trashgod Dec 09 '11 at 01:47
1

For the

graphics2D.rotate();

method, try

graphics2D.rotate(Math.toRadians(45));
Jont
  • 194
  • 14