• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

After rotating couldnt view

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends..

An image is displayed as i wanted, when i click on a button to rotate the image, although the rotation takes place but it doesnt show the rotated image on the screen(it shows the same image as it was before i clicked the rotation button)... but after closing the program when i execute the program again it shows the rotated image as i expected.... What i actually mean is to refresh or reload the page when i click the rotation button after rotation..

My rotation button consists of the following code:
****************************************************************
try
{
File input = new File(filePath);// name of the file along with path
BufferedImage source = ImageIO.read(input);

AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(90), source.getHeight()/2, source.getHeight()/2);

BufferedImage rotated = new BufferedImage(source.getHeight(), source.getWidth(), BufferedImage.TYPE_INT_RGB);
Graphics2D gR = rotated.createGraphics();
gR.drawRenderedImage(source, transform);
gR.dispose();

File output = new File(filePath);
ImageIO.write(rotated, "PNG", output);
repaint(1000);
}
catch (IOException ex)
{
System.out.println(ex);
}
****************************************************************

Please help me..
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you are loading the image in from the file, rotating it, and saving it to the file again. You are never drawing this rotated image to any panel. If you post the full code it would be easier to tell what is going on.
 
Manoj Paul
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Full Code IS:


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.net.*;
import java.text.SimpleDateFormat;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.BorderLayout;
import java.awt.Rectangle;

import javax.swing.JEditorPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import java.awt.ScrollPane;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PhotoBucket extends JFrame implements ListSelectionListener
{
public static final ImageIcon ICON_COMPUTER =new ImageIcon("computer.gif");
public static final ImageIcon ICON_DISK =new ImageIcon("disk.gif");
public static final ImageIcon ICON_FOLDER =new ImageIcon("folder.gif");
public static final ImageIcon ICON_EXPANDEDFOLDER =new ImageIcon("expandedfolder.gif");


protected DefaultTreeModel m_model;

// NEW
protected JPopupMenu m_popup;
protected Action m_action;
protected TreePath m_clickedPath;
private JScrollPane jScrollPane1 = new JScrollPane();
private JTree jTree1;
private JScrollPane jScrollPane2 = new JScrollPane();
private JList jList1;
public JLabel picture=new JLabel();
ImageIcon firstImage;

String path="";
String filePath="";
private Vector imageNames;
private JScrollPane jScrollPane3 = new JScrollPane();
private JLabel jLabel2;
private JButton jButton1 = new JButton();

public PhotoBucket()
{
DefaultMutableTreeNode top = new DefaultMutableTreeNode(
new IconData(ICON_COMPUTER, null, "My Computer")); // for the root of the tree

DefaultMutableTreeNode node;
File[] roots = File.listRoots(); /// for the nodes of the tree
for (int k=0; k<roots.length; k++)
{
node = new DefaultMutableTreeNode(new IconData(ICON_DISK,
null, new FileNode(roots[k])));
top.add(node);
node.add(new DefaultMutableTreeNode( new Boolean(true) ));
}

m_model = new DefaultTreeModel(top);
jTree1 = new JTree(m_model);

jTree1.putClientProperty("JTree.lineStyle", "Angled");

TreeCellRenderer renderer = new
IconCellRenderer();
jTree1.setCellRenderer(renderer);

jTree1.addTreeExpansionListener(new
DirExpansionListener());

jTree1.addTreeSelectionListener(new
DirSelectionListener());

jTree1.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
jTree1.setShowsRootHandles(true);
jTree1.setEditable(false);

m_popup = new JPopupMenu(); // to pop-up the nodes when clicked
m_action = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
if (m_clickedPath==null)
return;
if (jTree1.isExpanded(m_clickedPath))
jTree1.collapsePath(m_clickedPath);
else
jTree1.expandPath(m_clickedPath);
}
};
m_popup.add(m_action);
m_popup.addSeparator();

Action a1 = new AbstractAction("Delete")
{
public void actionPerformed(ActionEvent e)
{
jTree1.repaint();
JOptionPane.showMessageDialog(PhotoBucket.this,
"Delete option is not implemented",
"Info", JOptionPane.INFORMATION_MESSAGE);
}
};
m_popup.add(a1);

Action a2 = new AbstractAction("Rename")
{
public void actionPerformed(ActionEvent e)
{
jTree1.repaint();
JOptionPane.showMessageDialog(PhotoBucket.this,
"Rename option is not implemented",
"Info", JOptionPane.INFORMATION_MESSAGE);
}
};
m_popup.add(a2);
jTree1.add(m_popup);
jTree1.addMouseListener(new PopupTrigger());

WindowListener wndCloser = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
addWindowListener(wndCloser);

setVisible(true);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}

private void jbInit() throws Exception
{

filePath="E:/Manoj/Pictures/Rupa/rupsnrish.jpg";
ImageIcon firstImage = createImageIcon(filePath);
if (firstImage != null)
{
jLabel2 = new JLabel(firstImage);
jLabel2.setBorder(BorderFactory.createLineBorder(Color.black, 10));
jLabel2.grabFocus();
}

this.getContentPane().setLayout(null);
this.setSize(new Dimension(1028, 462));
jScrollPane1.setBounds(new Rectangle(0, 15, 150, 415));
jScrollPane1.getViewport().add(jTree1, null);
jScrollPane3.getViewport().add(jLabel2, null);

this.getContentPane().add(jButton1, null);
this.getContentPane().add(jScrollPane3, null);
this.getContentPane().add(jScrollPane1, null);
picture=new JLabel();
jButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
jButton1.setForeground(Color.blue);
jButton1.setBounds(new Rectangle(370, 410, 125, 25));
jButton1.setText("Rotate");
jScrollPane3.setBounds(new Rectangle(315, 10, 700, 395));

picture.setBorder(BorderFactory.createLineBorder(SystemColor.desktop, 4));
this.getContentPane().add(picture, null);
}
DefaultMutableTreeNode getTreeNode(TreePath path)
{
return (DefaultMutableTreeNode)(path.getLastPathComponent());
}

FileNode getFileNode(DefaultMutableTreeNode node)
{
if (node == null)
return null;
Object obj = node.getUserObject();
if (obj instanceof IconData)
obj = ((IconData)obj).getObject();
if (obj instanceof FileNode)
return (FileNode)obj;
else
return null;
}

class PopupTrigger extends MouseAdapter
{
public void mouseReleased(MouseEvent e)
{
if (e.isPopupTrigger())
{
int x = e.getX();
int y = e.getY();
TreePath path = jTree1.getPathForLocation(x, y);
if (path != null)
{
if (jTree1.isExpanded(path))
m_action.putValue(Action.NAME, "Collapse");
else
m_action.putValue(Action.NAME, "Expand");
m_popup.show(jTree1, x, y);
m_clickedPath = path;
}
}
}
}

// Make sure expansion is threaded and updating the tree model
// only occurs within the event dispatching thread.
class DirExpansionListener implements TreeExpansionListener
{
public void treeExpanded(TreeExpansionEvent event)
{
final DefaultMutableTreeNode node = getTreeNode(
event.getPath());
final FileNode fnode = getFileNode(node);

Thread runner = new Thread()
{
public void run()
{
if (fnode != null && fnode.expand(node))
{
Runnable runnable = new Runnable()
{
public void run()
{
m_model.reload(node);
}
};
SwingUtilities.invokeLater(runnable);
}
}
};
runner.start();
}

public void treeCollapsed(TreeExpansionEvent event) {}
}

class DirSelectionListener
implements TreeSelectionListener
{
public void valueChanged(TreeSelectionEvent event)
{
DefaultMutableTreeNode node = getTreeNode(
event.getPath());
FileNode fnode = getFileNode(node);
if (fnode != null)
{
String holdPath=fnode.getFile().getAbsolutePath();

File myDirectory = new File(fnode.getFile().getAbsolutePath());

if ( myDirectory.exists() && myDirectory.isDirectory() )
{
// List the files
String[] fileNameList = myDirectory.list();
String holdFiles="";

for(int u=0;u<fileNameList.length;u++)
{
holdFiles=holdFiles+" "+fileNameList[u];
}
show(holdFiles,holdPath);
}
}
}
}

public void show(String files,String holdPath)
{
String imageNamesString=files;
path=holdPath.replace('\\','/');

imageNames = parseList(imageNamesString);

jList1 = new JList(imageNames);
jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jList1.setSelectedIndex(0);
jList1.addListSelectionListener(this);

jScrollPane2.setBounds(new Rectangle(160, 15, 150, 415));
jScrollPane2.getViewport().add(jList1, null);

this.getContentPane().add(jScrollPane2, null);
filePath=path+"/"+(String)imageNames.firstElement();
firstImage = createImageIcon(filePath);
if (firstImage != null)
{
jLabel2.setIcon(firstImage);
jLabel2.setBorder(BorderFactory.createLineBorder(Color.black, 10));
jLabel2.grabFocus();
}
}
public JList getImageList() {
return jList1;
}
public void valueChanged(ListSelectionEvent e)
{

if (e.getValueIsAdjusting())
{
return;
}

JList theList = (JList)e.getSource();
if (theList.isSelectionEmpty())
{
picture.setIcon(null);
picture.setText(null);
}
else
{
int index = theList.getSelectedIndex();
filePath=path+"/" +(String)imageNames.elementAt(index);
firstImage = createImageIcon(filePath);
jLabel2.setIcon(firstImage);

if (firstImage != null)
{
picture.setText(null);
}
else
{
picture.setText("Image not found: "
+ (String)imageNames.elementAt(index));
}
}
}
protected static ImageIcon createImageIcon(String path)
{
File f=new File(path);
URL imgURL=null;

try
{
imgURL = f.toURL();
}
catch (MalformedURLException e)
{
}

if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}

protected static Vector parseList(String theStringList)
{
Vector v=new Vector(10);
// Vector<String> v = new Vector<String>(10);
StringTokenizer tokenizer = new StringTokenizer(theStringList, " ");
while (tokenizer.hasMoreTokens())
{
String image = tokenizer.nextToken();
v.addElement(image);
}
return v;
}
public static void main(String args[])
{
PhotoBucket obj=new PhotoBucket();
obj.setVisible(true);
}

public void jButton1_actionPerformed(ActionEvent e)
{
try
{
File input = new File(filePath);
BufferedImage source = ImageIO.read(input);

AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(90), source.getHeight()/2, source.getHeight()/2);

BufferedImage rotated = new BufferedImage(source.getHeight(), source.getWidth(), BufferedImage.TYPE_INT_RGB);
Graphics2D gR = rotated.createGraphics();
gR.drawRenderedImage(source, transform);
gR.dispose();

File output = new File(filePath);
ImageIO.write(rotated, "PNG", output);
// repaint(1000);
}
catch (IOException ex)
{
System.out.println(ex);
}
}
}

class IconCellRenderer
extends JLabel
implements TreeCellRenderer
{
protected Color m_textSelectionColor;
protected Color m_textNonSelectionColor;
protected Color m_bkSelectionColor;
protected Color m_bkNonSelectionColor;
protected Color m_borderSelectionColor;

protected boolean m_selected;

public IconCellRenderer()
{
super();
m_textSelectionColor = UIManager.getColor(
"Tree.selectionForeground");
m_textNonSelectionColor = UIManager.getColor(
"Tree.textForeground");
m_bkSelectionColor = UIManager.getColor(
"Tree.selectionBackground");
m_bkNonSelectionColor = UIManager.getColor(
"Tree.textBackground");
m_borderSelectionColor = UIManager.getColor(
"Tree.selectionBorderColor");
setOpaque(false);
}

public Component getTreeCellRendererComponent(JTree tree,
Object value, boolean sel, boolean expanded, boolean leaf,
int row, boolean hasFocus)

{
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)value;
Object obj = node.getUserObject();
setText(obj.toString());

if (obj instanceof Boolean)
setText("Retrieving data...");

if (obj instanceof IconData)
{
IconData idata = (IconData)obj;
if (expanded)
setIcon(idata.getExpandedIcon());
else
setIcon(idata.getIcon());
}
else
setIcon(null);

setFont(tree.getFont());
setForeground(sel ? m_textSelectionColor :
m_textNonSelectionColor);
setBackground(sel ? m_bkSelectionColor :
m_bkNonSelectionColor);
m_selected = sel;
return this;
}

public void paintComponent(Graphics g)
{
Color bColor = getBackground();
Icon icon = getIcon();

g.setColor(bColor);
int offset = 0;
if(icon != null && getText() != null)
offset = (icon.getIconWidth() + getIconTextGap());
g.fillRect(offset, 0, getWidth() - 1 - offset,
getHeight() - 1);

if (m_selected)
{
g.setColor(m_borderSelectionColor);
g.drawRect(offset, 0, getWidth()-1-offset, getHeight()-1);
}

super.paintComponent(g);
}
}

class IconData
{
protected Icon m_icon;
protected Icon m_expandedIcon;
protected Object m_data;

public IconData(Icon icon, Object data)
{
m_icon = icon;
m_expandedIcon = null;
m_data = data;
}

public IconData(Icon icon, Icon expandedIcon, Object data)
{
m_icon = icon;
m_expandedIcon = expandedIcon;
m_data = data;
}

public Icon getIcon()
{
return m_icon;
}

public Icon getExpandedIcon()
{
return m_expandedIcon!=null ? m_expandedIcon : m_icon;
}

public Object getObject()
{
return m_data;
}

public String toString()
{
return m_data.toString();
}
}

class FileNode
{
protected File m_file;

public FileNode(File file)
{
m_file = file;
}

public File getFile()
{
return m_file;
}

public String toString()
{
return m_file.getName().length() > 0 ? m_file.getName() :
m_file.getPath();
}

public boolean expand(DefaultMutableTreeNode parent)
{
DefaultMutableTreeNode flag =
(DefaultMutableTreeNode)parent.getFirstChild();
if (flag==null) // No flag
return false;
Object obj = flag.getUserObject();
if (!(obj instanceof Boolean))
return false; // Already expanded

parent.removeAllChildren(); // Remove Flag

File[] files = listFiles();
if (files == null)
return true;

Vector v = new Vector();

for (int k=0; k<files.length; k++)
{
File f = files[k];
if (!(f.isDirectory()))
continue;

FileNode newNode = new FileNode(f);

boolean isAdded = false;
for (int i=0; i<v.size(); i++)
{
FileNode nd = (FileNode)v.elementAt(i);
if (newNode.compareTo(nd) < 0)
{
v.insertElementAt(newNode, i);
isAdded = true;
break;
}
}
if (!isAdded)
v.addElement(newNode);
}

for (int i=0; i<v.size(); i++)
{
FileNode nd = (FileNode)v.elementAt(i);
IconData idata = new IconData(PhotoBucket.ICON_FOLDER,
PhotoBucket.ICON_EXPANDEDFOLDER, nd);
DefaultMutableTreeNode node = new
DefaultMutableTreeNode(idata);
parent.add(node);

if (nd.hasSubDirs())
node.add(new DefaultMutableTreeNode(
new Boolean(true) ));
}

return true;
}

public boolean hasSubDirs()
{
File[] files = listFiles();
if (files == null)
return false;
for (int k=0; k<files.length; k++)
{
if (files[k].isDirectory())
return true;
}
return false;
}

public int compareTo(FileNode toCompare)
{
return m_file.getName().compareToIgnoreCase(
toCompare.m_file.getName() );
}

protected File[] listFiles()
{
if (!m_file.isDirectory())
{
JOptionPane.showMessageDialog(null,"Error reading directory "+m_file.getAbsolutePath(),"Warning", JOptionPane.WARNING_MESSAGE);
return null;
}
try
{
return m_file.listFiles();
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null,
"Error reading directory "+m_file.getAbsolutePath(),
"Warning", JOptionPane.WARNING_MESSAGE);
return null;
}
}
}
 
a wee bit from the empire
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic