ThePerson
ThePerson

Reputation: 3236

Rotate graphics2D

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

    }

}

Upvotes: 0

Views: 3847

Answers (2)

Jont
Jont

Reputation: 194

For the

graphics2D.rotate();

method, try

graphics2D.rotate(Math.toRadians(45));

Upvotes: 1

Platinum Azure
Platinum Azure

Reputation: 46183

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.

Upvotes: 4

Related Questions