Results 1 to 2 of 2
- 12-09-2011, 01:35 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
Drawing on and rotating buffered Image
Hi,
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 ImageIO.read(imageFile) 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:
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 ).Java Code:JLabel panelLabel = new JLabel(new ImageIcon(bi));
So, if I call
Then my graphics2D object will be updated, and in turn my buffered image and the Jlabel.Java Code:graphic2D.drawString("Hello, this is my test.",10,10);
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!
Thanks,
Java Code: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.*; 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; 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); JButton saveBtn = new JButton("Save"); saveBtn.addActionListener(this); toolbox.add(saveBtn); JButton closeBtn = new JButton("Close"); closeBtn.addActionListener(this); toolbox.add(closeBtn); toolbox.add(new JButton("Freeform")); JButton colourChooserBtn = new JButton("Colour Chooser"); colourChooserBtn.addActionListener(this); toolbox.add(colourChooserBtn); 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); JLabel panelLabel = new JLabel(new ImageIcon(bi)); panelLabel.setMaximumSize(new Dimension(bi.getWidth(),bi.getHeight())); panelLabel.addMouseListener(this); editorScrollPane = new JScrollPane(panelLabel); container.add(editorScrollPane); editorFrame.setVisible(true); editorFrame.validate(); container.validate(); editorPanel.validate(); drawLabel(); //editorScrollPane.repaint(); } void drawLabel(){ graphic2D = bi.createGraphics(); graphic2D.drawString("Hello, this is my test.",10,10); } void drawCircle(){ } void saveImage(){ try{ 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()); } System.out.println(returner); 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", Color.black); return; } 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); editorScrollPane.repaint(); } 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(Color.black); graphic2D.drawRect(pressedx, pressedy, releasedx-pressedx,releasedy-pressedy); System.out.println(pressedx);} public void mouseClicked(MouseEvent e){ } }
- 12-09-2011, 04:55 AM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Help with Image Rotating?
By Leospaceman in forum Java 2DReplies: 2Last Post: 05-31-2011, 02:48 PM -
Drawing onto a buffered image
By trishtren in forum Java 2DReplies: 3Last Post: 04-09-2011, 09:23 PM -
Rotating Buffered Image distorts image
By VortexSpin in forum Java 2DReplies: 1Last Post: 02-13-2011, 05:54 AM -
Rotating an image
By lackofcolor in forum Java 2DReplies: 3Last Post: 02-27-2009, 11:54 PM -
Rotating Image?
By sciguy77 in forum Java AppletsReplies: 9Last Post: 02-17-2009, 01:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks