Results 1 to 2 of 2
  1. #1
    NutterzUK is offline Member
    Join Date
    Dec 2011
    Rep Power

    Default Drawing on and rotating buffered Image


    I'm new here, I've been trying to do this for some time now, and as a last hope decided to *swing* by the forums to see if someone could help me, before this drives me insane.

    I have a class, which takes an image ( type File ). It them creates a JFrame, some panels for buttons along the top, and a JScrollPanel below, containing a JPanel, which shows the image as a JLabel.

    Within the constructor, I call to create my buffered image ( global ), and then create a new graphics2D object from the bufferedImage.createGraphics().
    The graphic created is then used to create the JLabel, and put it on a panel:

    Java Code:
    JLabel panelLabel = new JLabel(new ImageIcon(bi));
    My understanding, which may be wrong is that now if I have methods to draw on my graphics2D object, that they will be reflected on the JLabel ( Providing I have repainted ).
    So, if I call
    Java Code:
    graphic2D.drawString("Hello, this is my test.",10,10);
    Then my graphics2D object will be updated, and in turn my buffered image and the Jlabel.
    This works for this one method. Excellent.

    So, I have a button to rotate the image. I want it to rotate my full image, the one which was taken in originally, as well as any strings that have been drawn on it, and display it back in the JLabel.
    I call graphic2D.rotate(1);
    Repaint the JLabel, and it's not moved, nothing has rotated.
    I notice if I put the rotate before painting strings, then the strings I paint are rotated.

    Is the graphics2D a kind of layer on top of my image? I want to be able to rotate my BufferedImage.
    I have made a method to also save my BufferedImage back to the file. It saves any strings I draw, but doesn't rotate.

    Perhaps I am misunderstanding something, perhaps I'm using Graphics2D incorrectly, or perhaps my code is bad, but I've been trying this for hours and hours :(.

    I've put my code below for reference, although I don't expect anyone to fix it, just pointing me in the right direction here would be a huge help.
    I've read lots of examples and tutorials, so please don't just link me to one without explaining where I've gone wrong, as I'm really stuck with this one!


    Java Code:
    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.*;
    import java.awt.geom.AffineTransform;	
    import java.awt.image.AffineTransformOp;
    class EditorView implements ActionListener, ChangeListener, MouseListener{
    	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);
    	Color color = new Color(0);
    	int pressedx = 0;
    	int releasedx = 0;
    	int pressedy = 0;
    	int releasedy = 0;
    	public EditorView(File file){
    		this.imageFile = file;
    		bi =;
    		}catch(Exception e){}
    		graphic2D = bi.createGraphics();
    	void createAndShowGUI(){
    		editorFrame = new JFrame("Editor");
    		container.setLayout(new BoxLayout(container,BoxLayout.Y_AXIS));
    		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");
    		JButton saveBtn = new JButton("Save");
    		JButton closeBtn = new JButton("Close");
    		toolbox.add(new JButton("Freeform"));
    		JButton colourChooserBtn = new JButton("Colour Chooser");
    		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"));
    		JLabel panelLabel = new JLabel(new ImageIcon(bi));
    		panelLabel.setMaximumSize(new Dimension(bi.getWidth(),bi.getHeight()));
    		editorScrollPane = new JScrollPane(panelLabel);
    	void drawLabel(){
    		graphic2D = bi.createGraphics();
            graphic2D.drawString("Hello, this is my test.",10,10);
    	void drawCircle(){
    	void saveImage(){
    		ImageIO.write(bi,getFileExtension(), imageFile);
    		}catch(Exception e){System.out.println("Couldn't write file");}
    	String getFileExtension(){
    		int positionOfDot = imageFile.getName().lastIndexOf(".");
    		String returner = null;
    		if( positionOfDot !=-1){
    			returner = imageFile.getName().substring((positionOfDot+1), imageFile.getName().length());
    		return returner;
    	public void actionPerformed(ActionEvent e){	
     		if(e.getActionCommand().equalsIgnoreCase("Close")) editorFrame.setVisible(false);
     		if(e.getActionCommand().equalsIgnoreCase("Save")) saveImage();
     		if(e.getActionCommand().equalsIgnoreCase("Colour Chooser")){
     			this.color = JColorChooser.showDialog(editorFrame, "Choose a Colour",;
     		if(e.getActionCommand().equalsIgnoreCase("Rotate Left")) {graphic2D.rotate(1); editorPanel.repaint(); return;}
    	public void stateChanged(ChangeEvent c){
    		graphic2D = bi.createGraphics();
    		graphic2D.scale(slider.getValue()/5, slider.getValue()/5);
    	public void mouseExited(MouseEvent e){}
    	public void mouseEntered(MouseEvent e){}
    	public void mouseReleased(MouseEvent e){ releasedx = e.getX(); releasedy=e.getY(); }
    	public void mousePressed(MouseEvent e){ pressedx = e.getX(); pressedy=e.getY(); graphic2D.setPaint(; graphic2D.drawRect(pressedx, pressedy, releasedx-pressedx,releasedy-pressedy); System.out.println(pressedx);}
    	public void mouseClicked(MouseEvent e){	}

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Drawing on and rotating buffered Image

    There's too much irrelevant code there to easily find anything important to the stated problem. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example. And note that the first 'S' stands for Short. Also, follow some standard formatting style, e.g. Code Conventions for the Java(TM) Programming Language: Contents Your actionPerformed(...) method is a mess and the mouse event processing lines are no better.

    It does appear that you read the image file only in the constructor, and reassign the field graphic2D every time it's used. Don't know whether that's significant or not.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Help with Image Rotating?
    By Leospaceman in forum Java 2D
    Replies: 2
    Last Post: 05-31-2011, 02:48 PM
  2. Drawing onto a buffered image
    By trishtren in forum Java 2D
    Replies: 3
    Last Post: 04-09-2011, 09:23 PM
  3. Rotating Buffered Image distorts image
    By VortexSpin in forum Java 2D
    Replies: 1
    Last Post: 02-13-2011, 05:54 AM
  4. Rotating an image
    By lackofcolor in forum Java 2D
    Replies: 3
    Last Post: 02-27-2009, 11:54 PM
  5. Rotating Image?
    By sciguy77 in forum Java Applets
    Replies: 9
    Last Post: 02-17-2009, 01:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts