Je suis entièrement d'accord avec le post précédent, AffineTransform est le chemin à parcourir. Voici un exemple simple ....
import java.awt.*;
import java.awt.geom.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class RotateImage {
private static final int PANEL_WIDTH = 600;
private static final Dimension MAIN_SIZE = new Dimension(PANEL_WIDTH, PANEL_WIDTH);
private static final int SLIDER_MIN = -180;
private static final int SIDER_MAX = 180;
private static final int MAJOR_TICK = 30;
private static final int MINOR_TICK = 15;
private static final String URL_PATH = "http://duke.kenai.com/guitar/DukeAsKeith-daylightSmall.png";
private JPanel mainPanel = new JPanel();
private JPanel drawingPanel = createDrawingPanel();
private AffineTransform transform = new AffineTransform();
private JSlider rotationSlider;
private Image image;
public RotateImage() {
mainPanel.setLayout(new BorderLayout());
mainPanel.add(drawingPanel, BorderLayout.CENTER);
mainPanel.add(createTransformPanel(), BorderLayout.SOUTH);
URL url;
try {
url = new URL(URL_PATH);
image = ImageIO.read(url);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private JPanel createTransformPanel() {
rotationSlider = new JSlider(SLIDER_MIN, SIDER_MAX, 0);
rotationSlider.setMajorTickSpacing(MAJOR_TICK);
rotationSlider.setMinorTickSpacing(MINOR_TICK);
rotationSlider.setPaintLabels(true);
rotationSlider.setPaintTicks(true);
rotationSlider.setPaintTrack(true);
rotationSlider.setSnapToTicks(true);
rotationSlider.addChangeListener(new SliderListener());
JPanel transformingPanel = new JPanel(new BorderLayout());
transformingPanel.setBorder(BorderFactory.createEtchedBorder());
transformingPanel.add(rotationSlider);
return transformingPanel;
}
@SuppressWarnings("serial")
private JPanel createDrawingPanel() {
if (drawingPanel != null) {
return drawingPanel;
}
drawingPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
myPaint(g);
}
};
drawingPanel.setPreferredSize(MAIN_SIZE);
return drawingPanel;
}
private void myPaint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(3));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform oldTransform = g2.getTransform();
if (transform != null) {
g2.setTransform(transform);
}
if (image != null) {
g2.drawImage(image, 70, 70, null);
}
g2.setTransform(oldTransform);
}
public JComponent getPanel() {
return mainPanel;
}
private class SliderListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
double theta = Math.PI * rotationSlider.getValue()/180;
double center = (double) PANEL_WIDTH/2;
transform = AffineTransform.getRotateInstance(theta, center, center);
drawingPanel.repaint();
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("RotateShape Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new RotateImage().getPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Ouais c'est utile en java ....... comment cela se fait en html ....... ?? – sasidhar
Que voulez-vous dire html? Est-ce une question Java ou JavaScript? –
@sasidhar, ehm ... vous avez écrit Java, peut-être que si vous clarifiez exactement la technologie avec laquelle vous travaillez, alors vous pouvez obtenir une meilleure réponse. En tout cas comment cela se fait en HTML/Javascript je ne sais pas. –