日期:2014-05-20 浏览次数:20818 次
import java.awt.*; import java.awt.image.*; import java.awt.event.*; import javax.swing.*; import java.net.URL; import javax.swing.event.*; public class ImageRotate extends JFrame { private FImage2D originalImage, resultImage; private JLabel originalImageLabel, resultImageLabel; public ImageRotate(String name){ // Initialize the GUI originalImage = new FImage2D( loadImage( name ) ); resultImage = originalImage.invert(); Container pane = getContentPane(); originalImageLabel = new JLabel( new ImageIcon(originalImage.bufferedImage() )); resultImageLabel = new JLabel( new ImageIcon(resultImage.bufferedImage() )); pane.add( originalImageLabel, BorderLayout.WEST ); pane.add( resultImageLabel, BorderLayout.EAST ); pane.add( new JLabel("angle:") ); JSlider angleSlider = new JSlider(JSlider.HORIZONTAL, -180, 180, 0); angleSlider.addChangeListener(new angleListener() ); angleSlider.setMajorTickSpacing(90); angleSlider.setMinorTickSpacing(10); angleSlider.setPaintTicks(true); angleSlider.setPaintLabels(true); angleSlider.setBorder(BorderFactory.createEmptyBorder(0,0,20,20)); pane.add( angleSlider ); } class angleListener implements ChangeListener { public void stateChanged(ChangeEvent e) { JSlider source = (JSlider)e.getSource(); if (!source.getValueIsAdjusting()) { double angle = (double)source.getValue(); resultImage = originalImage.rotate( (float)(angle * Math.PI / 180.0) ); resultImageLabel.setIcon( new ImageIcon(resultImage.bufferedImage()) ); } } } public BufferedImage loadImage( String name ) { URL url = ImageRotate.class.getResource(name); Image image = this.getToolkit().getImage(url);; try { MediaTracker tracker = new MediaTracker( this ); tracker.addImage( image, 0 ); tracker.waitForID(0); } catch ( Exception e ) { System.err.println(e); System.exit(1); } int width = image.getWidth(this); int height = image.getHeight(this); BufferedImage input = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB ); input.createGraphics().drawImage( image, 0, 0, null ); return input; } public static void main( String[] argv ){ if( argv.length == 0 ) { try { JFrame frame = new ImageRotate( "cameraman.jpg" ); frame.pack(); frame.setVisible(true); } catch (Exception e) { System.err.println(e); System.exit(1); } } else { System.err.println("usage: java ImageRotate" ); System.exit(1); } } }
------解决方案--------------------
mark
------解决方案--------------------
用JAI就可以了