import java.awt.*;
import javax.swing.*;public class c4_01_02 extends JFrame{
public static void main(String args[]){
c4_01_02 frame=new c4_01_02();
}
Image image1;
public c4_01_02(){
super("c4_01_02");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.white);
setSize(600,500);
setVisible(true);

}
public void paint(Graphics g){
int x,y;
image1=Toolkit.getDefaultToolkit().getImage("photo3.jpg");
x=10;y=40;
g.drawImage(image1,x,y,x+240,y+180,0,0,240,180,this);
x=10;y=240;
g.drawImage(image1,x,y,x+240,y+180,0,180,240,0,this);
x=300;y=40;
g.drawImage(image1,x,y,x+240,y+180,0,180,240,0,this);
    x=300;y=240;
g.drawImage(image1,x,y,x+240,y+90,60,0,240,90,this);
}
}
请快些加分...冲级中..

解决方案 »

  1.   

    也可以先rotate(double theta,double x,double y)然后g.drawImage()
      

  2.   

    to thomas_20(执子之手,与子偕老);
                   给我详细点代码啊~,弄个小例子出来
      

  3.   

    顶一下
     我也要做这个
     
    可楼上.drawImage(image1,x,y,x+240,y+180,0,180,240,0,this);这个各参数值是什么意思啊
    我看了一下API里面的可是还没研究出是什么意思比如我想转90,180,270,360等等有源码吗
      

  4.   

    /*
    * @(#)Transform.java   1.2 98/07/31
    */
    import java.lang.Integer;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.awt.event.ItemListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.*;/*
     * This applet renders a shape, selected by the user, with a paint,stroke, and rendering method,
     * also selected by the user.
    */public class Transform extends JApplet implements ItemListener,
    ActionListener {
        JLabel primLabel, lineLabel, paintLabel, transLabel, strokeLabel;
        TransPanel display;
        static JComboBox primitive, line, paint, trans, stroke;
        JButton redraw;
        public static boolean no2D = false;    public void init() {
            GridBagLayout layOut = new GridBagLayout();
            getContentPane().setLayout(layOut);
            GridBagConstraints c = new GridBagConstraints();        c.weightx = 1.0;
            c.fill = GridBagConstraints.BOTH;
            primLabel = new JLabel();
            primLabel.setText("Primitive");
            Font newFont = getFont().deriveFont(1);
            primLabel.setFont(newFont);
            primLabel.setHorizontalAlignment(JLabel.CENTER);
            layOut.setConstraints(primLabel, c);
            getContentPane().add(primLabel);        lineLabel = new JLabel();
            lineLabel.setText("Lines");
            lineLabel.setFont(newFont);
            lineLabel.setHorizontalAlignment(JLabel.CENTER);
            layOut.setConstraints(lineLabel, c);
            getContentPane().add(lineLabel);        paintLabel = new JLabel();
            paintLabel.setText("Paints");
            paintLabel.setFont(newFont);
            paintLabel.setHorizontalAlignment(JLabel.CENTER);
            layOut.setConstraints(paintLabel, c);
            getContentPane().add(paintLabel);        c.gridwidth = GridBagConstraints.RELATIVE;
            transLabel = new JLabel();  
            transLabel.setText("Transforms");
            transLabel.setFont(newFont);
            transLabel.setHorizontalAlignment(JLabel.CENTER);
            layOut.setConstraints(transLabel, c);
            getContentPane().add(transLabel);        c.gridwidth = GridBagConstraints.REMAINDER;
            strokeLabel = new JLabel();
            strokeLabel.setText("Rendering");
            strokeLabel.setFont(newFont);
            strokeLabel.setHorizontalAlignment(JLabel.CENTER); 
            layOut.setConstraints(strokeLabel, c);
            getContentPane().add(strokeLabel);        GridBagConstraints ls = new GridBagConstraints();
            ls.weightx = 1.0;
            ls.fill = GridBagConstraints.BOTH;
            primitive = new JComboBox( new Object []{
                                       "rectangle",
                                       "ellipse",
                                       "text"});
            primitive.addItemListener(this);
            newFont = newFont.deriveFont(0, 14.0f);
            primitive.setFont(newFont);
            layOut.setConstraints(primitive, ls);
            getContentPane().add(primitive);        line = new JComboBox( new Object []{  
                                  "thin",
                                  "thick",
                                  "dashed"});
            line.addItemListener(this);
            line.setFont(newFont);
            layOut.setConstraints(line, ls);
            getContentPane().add(line);        paint = new JComboBox( new Object[]{
                                   "solid",
                                   "gradient",
                                   "polka"});
            paint.addItemListener(this);
            paint.setFont(newFont);
            layOut.setConstraints(paint, ls);
            getContentPane().add(paint);        ls.gridwidth = GridBagConstraints.RELATIVE;        trans = new JComboBox( new Object[]{
    "Identity",
    "rotate",
    "scale",
    "shear"});
    trans.addItemListener(this);
    trans.setFont(newFont);
    layOut.setConstraints(trans, ls);
    getContentPane().add(trans);        ls.gridwidth = GridBagConstraints.REMAINDER;
            stroke = new JComboBox( new Object[]{
                                    "Stroke",
                                    "Fill",
                                    "Stroke & Fill"}); 
            stroke.addItemListener(this);
            stroke.setFont(newFont);
            layOut.setConstraints(stroke, ls);
            getContentPane().add(stroke); GridBagConstraints button = new GridBagConstraints();
    button.gridwidth = GridBagConstraints.REMAINDER;
    redraw = new JButton("Redraw");
    redraw.addActionListener(this);
            redraw.setFont(newFont);
            layOut.setConstraints(redraw, button);
            getContentPane().add(redraw);        GridBagConstraints tP = new GridBagConstraints();
            tP.fill = GridBagConstraints.BOTH;
            tP.weightx = 1.0;
            tP.weighty = 1.0;
            tP.gridwidth = GridBagConstraints.REMAINDER;
            display = new TransPanel();
            layOut.setConstraints(display, tP);
            display.setBackground(Color.white);
            getContentPane().add(display);        validate();    }    public void itemStateChanged(ItemEvent e){}   public void actionPerformed(ActionEvent e) {
            display.setTrans(trans.getSelectedIndex());
            display.renderShape();
       }

      

  5.   


        public static void main( String[] argv ) {
            if ( argv.length > 0 && argv[0].equals( "-no2d" ) ) {
                Transform.no2D = true;
            }
            
            JFrame frame = new JFrame( "Transform" );
            frame.addWindowListener( new WindowAdapter(){
                public void windowClosing( WindowEvent e ){
                    System.exit( 0 );
                }
            });                             JApplet applet = new Transform();
            frame.getContentPane().add( BorderLayout.CENTER, applet );
            
            applet.init();
            
            frame.setSize( 550, 400 );
            frame.show();
       }}class TransPanel extends JPanel {
        AffineTransform at = new AffineTransform();
        int w, h;
        Shape shapes[] = new Shape[3];
        BufferedImage bi;
        boolean firstTime = true;    public TransPanel(){
            setBackground(Color.white);
            shapes[0] = new Rectangle(0, 0, 100, 100);
            shapes[1] = new Ellipse2D.Double(0.0, 0.0, 100.0, 100.0);
            TextLayout textTl = new TextLayout("Text", new Font("Helvetica", 1, 96), new FontRenderContext(null, false, false));
    AffineTransform textAt = new AffineTransform();
    textAt.translate(0, (float)textTl.getBounds().getHeight());
            shapes[2] = textTl.getOutline(textAt);
        }    public void setTrans(int transIndex) {
            // Sets the AffineTransform.
            switch ( transIndex ) {
            case 0 : at.setToIdentity();
                at.translate(w/2, h/2); break;
            case 1 : at.rotate(Math.toRadians(45)); break;
            case 2 : at.scale(0.5, 0.5); break;
            case 3 : at.shear(0.5, 0.0); break;
          }
        }

        public void renderShape() {
            repaint();
        }    public void paintComponent(Graphics g) {
    super.paintComponent(g);

    if ( !Transform.no2D ) {
            Graphics2D g2 = (Graphics2D) g;
             Dimension d = getSize();
             w = d.width;
             h = d.height;
            // Prints out the intructions.
            String instruct = "Pick a primitive, line style, paint, transform,";
            TextLayout thisTl = new TextLayout(instruct, new Font("Helvetica", 0, 10), g2.getFontRenderContext());
            float width = (float)thisTl.getBounds().getWidth();
    float height = (float)thisTl.getBounds().getHeight();
            thisTl.draw(g2, w/2-width/2, 15); instruct = "and rendering method and click the Redraw button.";
            thisTl = new TextLayout(instruct, new Font("Helvetica", 0, 10), g2.getFontRenderContext());
            width = (float)thisTl.getBounds().getWidth();
            thisTl.draw(g2, w/2-width/2, height + 17); // Initialize the transform.
    if (firstTime) {
        at.setToIdentity();
                at.translate(w/2, h/2);
                firstTime = false;
    }        // Sets the Stroke.
    Stroke oldStroke = g2.getStroke();        switch ( Transform.line.getSelectedIndex() ) {
            case 0 : g2.setStroke(new BasicStroke(3.0f)); break;
            case 1 : g2.setStroke(new BasicStroke(8.0f)); break;
            case 2 : float dash[] = {10.0f};
                g2.setStroke(new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f));
                break;
            }        // Sets the Paint.
    Paint oldPaint = g2.getPaint();        switch ( Transform.paint.getSelectedIndex() ) {
            case 0 : g2.setPaint(Color.blue);break;
            case 1 : g2.setPaint(new GradientPaint(0, 0, Color.lightGray, w-250, h, Color.blue, false));
                break;
            case 2 : BufferedImage buffi = new BufferedImage(15, 15, BufferedImage.TYPE_INT_RGB);
                Graphics2D buffig = buffi.createGraphics();
                buffig.setColor(Color.blue);
                buffig.fillRect(0, 0, 15, 15);
                buffig.setColor(Color.lightGray);
        buffig.translate((15/2)-(5/2), (15/2)-(5/2));
                buffig.fillOval(0, 0, 7, 7); 
                Rectangle r = new Rectangle(0,0,25,25);
                g2.setPaint(new TexturePaint(buffi, r));
                break;
            }        // Sets the Shape.
            Shape shape = shapes[Transform.primitive.getSelectedIndex()];
            Rectangle r = shape.getBounds();        // Sets the selected Shape to the center of the Canvas.
            AffineTransform saveXform = g2.getTransform();
      AffineTransform toCenterAt = new AffineTransform();
            toCenterAt.concatenate(at);
            toCenterAt.translate(-(r.width/2), -(r.height/2));        g2.transform(toCenterAt);        // Sets the rendering method.
            switch ( Transform.stroke.getSelectedIndex() ) {
            case 0 : g2.draw(shape); break;
            case 1 : g2.fill(shape); break;
            case 2 : Graphics2D tempg2 = g2;
                g2.fill(shape);
                g2.setColor(Color.darkGray);
                g2.draw(shape);
                g2.setPaint(tempg2.getPaint()); break;   
            } g2.setStroke(oldStroke);
    g2.setPaint(oldPaint);
            g2.setTransform(saveXform);

    }
    }
    }
      

  6.   

    来个简单的.
    import java.awt.*;
    import javax.swing.*;
    import java.awt.geom.*;
    public class c4_01_03 extends JFrame
    {
    public static void main(String args[])
    {
    c4_01_03 frame=new c4_01_03();

    }
    Image image1;
    public c4_01_03(){
    super ("c4_01_03");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBackground(Color.white);
    setSize(500,400);
    setVisible(true);
    }
    public void paint(Graphics g){
    int x,y;
    Graphics2D g2;
    g2=(Graphics2D) g;
    image1=Toolkit.getDefaultToolkit().getImage("photo3.jpg");
    g2.rotate(Math.toRadians(10));//旋转10度
    g2.drawImage(image1,200,20,240,180,this);
    g2.rotate(Math.toRadians(10));//旋转10度
        g2.drawImage(image1,200,20,240,180,this);
        g2.rotate(Math.toRadians(10));//旋度10度
        g2.drawImage(image1,200,20,240,180,this);
        }
    }
      

  7.   

    DESL:(
    只怪我没说清
     我说这图片不仅仅是显示的时候是倒转了90度
     而是本身生成图片是真正的倒转了90度
     
    并且你给的drawImage这参数值我也没搞懂
    你再帮帮我 一定结贴
      

  8.   

    楼主可以看看 enrico(小甭)的例子
      

  9.   

    以前在vc上做过bmp图片的旋转。
    这需要修改bmp本身内部的点信息。
    具体算法可以看看图像处理的书,书店里还是比较多的。
      

  10.   

    to  tomcatjava(小鱼儿);
    我看了  可是也不是对图片本身编辑倒转
      

  11.   

    hoho,这东西,如果你借助java2d实现,那就很简单了!
      

  12.   

    to  zhang21cnboy(事了抚衣去,不留身与名) 
                                               如果你多给点提示啊我真是太感谢了 !!!!!!!!!!!!
      

  13.   

    ImageIcon imgIcon = new ImageIcon(imageurl);
         Image imageOriginal = imgIcon.getImage();
         int widthOriginal = imageOriginal.getWidth(null);
         int heightOriginal = imageOriginal.getHeight(null);
         System.out.println("width=" + widthOriginal + ",heightOriginal=" + heightOriginal);
         BufferedImage   buffImg = new BufferedImage(widthOriginal, heightOriginal,
                                       BufferedImage.TYPE_INT_RGB);
         Graphics g = buffImg.createGraphics();
         int num = Integer.parseInt(rotateNum);
          //旋转90度
        if (flag.equals("2")) {      if (num % 4 == 1) {
            g.drawImage(imageOriginal, 0, 0, widthOriginal, heightOriginal, 0,
                        heightOriginal, widthOriginal, 0, null);
          }
          else if (num % 4 == 0) {
            g.drawImage(imageOriginal, 0, 0, widthOriginal, heightOriginal, 0,
                        0, widthOriginal, heightOriginal, null);
          }
        }    //旋转180度
        else if (flag.equals("3")){
          if (num % 2 == 1) {
            g.drawImage(imageOriginal, 0, 0, widthOriginal, heightOriginal, 0,
                        heightOriginal, widthOriginal, 0, null);
          }
          else if (num % 2 == 0) {
            g.drawImage(imageOriginal, 0, 0, widthOriginal, heightOriginal, 0,
                        0, widthOriginal, heightOriginal, null);
          }
     }
       g.dispose();   FileOutputStream fos = new FileOutputStream(imageurl);
       BufferedOutputStream bos = new BufferedOutputStream(fos);
       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
       encoder.encode(buffImg);
       bos.flush();
       bos.close();