Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rotating Images

Status
Not open for further replies.

Errant

Programmer
Mar 30, 2005
3
0
0
CA
I'm trying to find a way to rotate an [tt]Image[/tt]. The image is stored as either a .gif or .jpg file, and initially loaded as an [tt]ImageIcon[/tt]. I only need it to be rotated in 90° incriments. I'm looking for a nice, simple way to do this, although as I'm having trouble finding anything at all, anything that works will do.
 
A doable but complicated way is following in 3 steps :
1) Convert your image into a 2 dimensional bytes array
2) Copy the array in another one changing order for one of the array dimensions.
3) Convert again the copied array into an image.

Hope that helps.

Water is not bad as long as it stays out human body ;-)
 
//copy from my post
// positive value or negative value will determine to rotate 90 degrees clockwisely or counter-clockwisely.
// g2.rotate(Math.PI*0.25,250.0,250.0);
// g2.rotate(Math.PI*(-0.25),250.0,250.0);

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.AffineTransform;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.util.*;
class WholeFrame3 extends JFrame implements ActionListener
{
CenterCanvas3 cc;
JButton jb,jb2;
JPanel ccPanel,jbPanel;
double dividedBy = 36.0;
public WholeFrame3()
{
//getContentPane().setBackground(Color.green);
getContentPane().setLayout(null);
ccPanel = new JPanel();
ccPanel.setBounds(0,0,500,500);
cc = new CenterCanvas3();
cc.setBounds(0,0,500,500);
getContentPane().add(ccPanel);
ccPanel.add(cc);

jbPanel = new JPanel();
jbPanel.setBackground(Color.blue);
jbPanel.setBounds(0,600,500,100);
getContentPane().add(jbPanel);

jb = new JButton("click");
jb.addActionListener(this);
jbPanel.add(jb);
}
public static void main(String args[])
{
WholeFrame3 wf = new WholeFrame3();
wf.setSize(600,700);
wf.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb)
{
dividedBy-=1.0;

cc.setRadian(Math.PI*dividedBy/36.0);
}
}
}

class CenterCanvas3 extends Canvas
{

double rad = Math.PI*36.0/36.0;
public void setRadian(double tempRad)
{
rad = tempRad;
repaint();
}
public double getRadian()
{
System.out.println(rad);
return rad;
}
public void update()
{}

public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
setBackground(Color.green);
//Update transform to provide rotation and display,
// the transform values.
g2.rotate(getRadian(),250.0,250.0);
g2.setColor(Color.black);
g2.drawRect(120, 120, 20, 200);
g2.drawString ("Material", 30, 220);
g2.drawString ("Ratings", 30, 240);
g2.drawString ("+5", 102, 130);
g2.drawString ("0", 107, 220);
g2.drawString ("-5", 102, 315);
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top