如果用java做一个类似photoshop的程序,首先载入一副图片,然后,其中一个功能是图形的放大缩小,这应该怎么实现呢,那么是否应该把图像的jpanel放到ScrollPane中,以便出现滚动条?有什么好方法吗?

解决方案 »

  1.   

    import java.io.File;
    import java.awt.image.BufferedImage;
    import java.awt.Image;
    import java.awt.image.AffineTransformOp;
    import javax.imageio.ImageIO;
    import java.awt.geom.AffineTransform;public class UploadImg{/**
    * @param fromdir 图片的原始目录
    * @param todir 处理后的图片存放目录
    * @param imgfile 原始图片
    * @param sysimgfile 处理后的图片文件名前缀
    *
    */ ............................ 
    public boolean CreateThumbnail() throws Exception
    {
    //ext是图片的格式 gif JPG 或png
    String ext="";
    double Ratio=0.0;
    File F = new File(fromdir,imgfile);
    if (!F.isFile())
    throw new Exception(F+" is not image file error in CreateThumbnail!");//首先判断上传的图片是gif还是JPG ImageIO只能将gif转换为png
    if (isJpg(imgfile)){
    ext="jpg";
    }else{
    ext="png"; 
    }
    File ThF = new File(todir,sysimgfile+"."+ext); 
    BufferedImage Bi = ImageIO.read(F);
    //假设图片宽 高 最大为120 120
    Image Itemp = Bi.getScaledInstance (120,120,Bi.SCALE_SMOOTH);
    if ((Bi.getHeight()>120) || (Bi.getWidth()>120)){
    if (Bi.getHeight()>Bi.getWidth())
    Ratio = 120.0/Bi.getHeight();
    else
    Ratio = 120.0/Bi.getWidth();
    }
    AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
    Itemp = op.filter(Bi, null);try {
    ImageIO.write((BufferedImage)Itemp, ext, ThF);
    }catch (Exception ex) {
    throw new Exception(" ImageIo.write error in CreatThum.: "+ex.getMessage()); 
    }
    return (true);
    }
    }
      

  2.   

    对不起哦,楼主;上面是我在测试我的java版灌水机程序
      

  3.   


    public class test2 extends JFrame {
        public test2() {
            try {
                jbInit();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            this.setSize(600, 600);
        }    public static void main(String[] args) {
            test2 frame = new test2();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.show();
        }    private void jbInit() throws Exception {
            this.getContentPane().setLayout(borderLayout1);
            jPanel1.addMouseMotionListener(new test2_jPanel1_mouseMotionAdapter(this));
             jPanel1.addMouseListener(new test2_jPanel1_mouseAdapter(this));
            this.setJMenuBar(jMenuBar1);
                    jMenuItem1.addActionListener(new test2_jMenuItem1_actionAdapter(this));
                   jPanel2.add(jPanel1);
            
            
            this.getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
        }    int a = 0;
        myJPanel jPanel1 = new myJPanel();
        Canvas canvas1 = new Canvas();
        xxJPanel jPanel2 = new xxJPanel();
        public void jPanel1_mouseMoved(MouseEvent e) {
            if (jPanel1.up1.contains(e.getX(), e.getY()) == true ||
                jPanel1.down3.contains(e.getX(), e.getY()) == true) {
                setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
            } else if (jPanel1.up2.contains(e.getX(), e.getY()) == true ||
                       jPanel1.down2.contains(e.getX(), e.getY()) == true) {
                setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));        } else if (jPanel1.up3.contains(e.getX(), e.getY()) == true ||
                       jPanel1.down1.contains(e.getX(), e.getY()) == true) {
                setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));        } else if (jPanel1.middle1.contains(e.getX(), e.getY()) == true ||
                       jPanel1.middle2.contains(e.getX(), e.getY()) == true) {            setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));        } else {
                setCursor(Cursor.getDefaultCursor());
            }
        }
        public void jPanel1_mousePressed(MouseEvent e) {
            if (jPanel1.up1.contains(e.getX(), e.getY()) == true)
                a = 1;
            else if (jPanel1.down3.contains(e.getX(), e.getY()) == true)
                a = 9;
            else if (jPanel1.up2.contains(e.getX(), e.getY()) == true)
                a = 2;
            else if (jPanel1.down2.contains(e.getX(), e.getY()) == true)
                a = 8;
            else if (jPanel1.up3.contains(e.getX(), e.getY()) == true)
                a = 3;
            else if (jPanel1.down1.contains(e.getX(), e.getY()) == true)
                a = 7;
            else if (jPanel1.middle1.contains(e.getX(), e.getY()) == true)
                a = 4;
            else if (jPanel1.middle2.contains(e.getX(), e.getY()) == true)
                a = 6;
            else if (jPanel1.itself.contains(e.getX(), e.getY()) == true) {
                a = 10;
                xx = e.getX() - jPanel1.x;
                yy = e.getY() - jPanel1.y;
            } else
                a = 0;    }    int xx = 0;
        int yy = 0;
        BorderLayout borderLayout1 = new BorderLayout();
        JMenuBar jMenuBar1 = new JMenuBar();
        JMenu jMenu1 = new JMenu();
        JMenuItem jMenuItem1 = new JMenuItem();
        JMenuItem jMenuItem2 = new JMenuItem();
        public void jPanel1_mouseDragged(MouseEvent e) {
            if (a == 1) {
                jPanel1.width = jPanel1.width - e.getX() + jPanel1.x;
                jPanel1.length = jPanel1.length - e.getY() + jPanel1.y;
                jPanel1.x = e.getX();
                jPanel1.y = e.getY();
                jPanel1.repaint();        } else if (a == 2) {
                jPanel1.length = jPanel1.length - e.getY() + jPanel1.y;
                jPanel1.y = e.getY();
                jPanel1.repaint();
            } else if (a == 3) {
                jPanel1.length = jPanel1.length - e.getY() + jPanel1.y;
                jPanel1.width = e.getX() - jPanel1.x;
                jPanel1.y = e.getY();
                jPanel1.repaint();
            } else if (a == 4) {
                jPanel1.width = jPanel1.width - e.getX() + jPanel1.x;
                jPanel1.x = e.getX();
                jPanel1.repaint();
            } else if (a == 6) {
                jPanel1.width = e.getX() - jPanel1.x;
                //jPanel1.x =e.getX() ;
                jPanel1.repaint();
            } else if (a == 7) {
                jPanel1.width = jPanel1.width - (e.getX() - jPanel1.x);
                jPanel1.length = e.getY() - jPanel1.y;
                jPanel1.x = e.getX();
                jPanel1.y = jPanel1.y;            jPanel1.repaint();
            } else if (a == 8) {
                jPanel1.length = e.getY() - jPanel1.y;
                jPanel1.repaint();
            } else if (a == 9) {            jPanel1.width = e.getX() - jPanel1.x;
                jPanel1.length = e.getY() - jPanel1.y;
                jPanel1.repaint();
            } else if (a == 10) {
                jPanel1.x = e.getX() - xx;
                jPanel1.y = e.getY() - yy;
                jPanel1.repaint();
            }
        }
      

  4.   


        public void jPanel1_mouseReleased(MouseEvent e) {
               }    public void jMenuItem1_actionPerformed(ActionEvent e) {
               }
    }
    class test2_jMenuItem1_actionAdapter implements ActionListener {
        private test2 adaptee;
        test2_jMenuItem1_actionAdapter(test2 adaptee) {
            this.adaptee = adaptee;
        }    public void actionPerformed(ActionEvent e) {
            adaptee.jMenuItem1_actionPerformed(e);
        }
    }
    class test2_jPanel1_mouseMotionAdapter extends MouseMotionAdapter {
        private test2 adaptee;
        test2_jPanel1_mouseMotionAdapter(test2 adaptee) {
            this.adaptee = adaptee;
        }    public void mouseDragged(MouseEvent e) {
            adaptee.jPanel1_mouseDragged(e);
        }    public void mouseMoved(MouseEvent e) {
            adaptee.jPanel1_mouseMoved(e);
        }
    }
    class test2_jPanel1_mouseAdapter extends MouseAdapter {
        private test2 adaptee;
        test2_jPanel1_mouseAdapter(test2 adaptee) {
            this.adaptee = adaptee;
        }    public void mousePressed(MouseEvent e) {        adaptee.jPanel1_mousePressed(e);
        }
    }class xxJPanel extends JPanel {
        private Image image;
        public int x;
        public int y;
        public int width;
        public int length;    public xxJPanel() {
            try {
          image = ImageIO.read(new File("21.jpg"));
           length = image.getHeight(this);
          width = image.getWidth(this);
          x = this.getX();
          y = this.getY();
          this.setPreferredSize(new Dimension(width, length));
          this.repaint() ;
      } catch (IOException exception) {  }
        public void paintComponent(Graphics g) {        super.paintComponent(g);
            // t = new AffineTransform();
            // if(image==null) return;
            //g.drawImage(image,0,0,300,200,null) ;        if (image == null)return;
            g.drawImage(image, x, y, width, length, null);    }}
    class myJPanel extends JLabel {    public Image image2;
        public int x;
        public int y;
        public int width;
        public int length;
        public Rectangle up1;
        public Rectangle up2;
        public Rectangle up3;
        public Rectangle middle1;
        public Rectangle middle2;
        public Rectangle down1;
        public Rectangle down2;
        public Rectangle down3;
        public Rectangle itself;
        public myJPanel() {
            try {
                           image2 = ImageIO.read(new File("32.png"));
                       length = image2.getHeight(this);
                width = image2.getWidth(this);
                x = this.getX();
                y = this.getY();
                 this.setPreferredSize(new Dimension(width, length));
            } catch (IOException exception) {        }    }    public void paintComponent(Graphics g) {        super.paintComponent(g);
      
            if (image2 == null)return;
            g.drawImage(image2, x, y, width, length, null);
            up1 = new Rectangle(x, y, 8, 8);
            up2 = new Rectangle(x + width / 2, y, 8, 8);
            up3 = new Rectangle(x + width - 8, y, 8, 8);
            middle1 = new Rectangle(x, y + length / 2, 8, 8);
            middle2 = new Rectangle(x + width - 8, y + length / 2, 8, 8);
            down1 = new Rectangle(x, y + length - 8, 8, 8);
            down2 = new Rectangle(x + width / 2, y + length - 8, 8, 8);
            down3 = new Rectangle(x + width - 8, y + length - 8, 8, 8);
            itself = new Rectangle(x, y, width, length);
            g.fillRect(x, y, 8, 8);
            g.fillRect(x + width / 2, y, 8, 8);
            g.fillRect(x + width - 8, y, 8, 8);
            g.fillRect(x, y + length / 2, 8, 8);
            g.fillRect(x + width - 8, y + length / 2, 8, 8);
            g.fillRect(x, y + length - 8, 8, 8);
            g.fillRect(x + width / 2, y + length - 8, 8, 8);
            g.fillRect(x + width - 8, y + length - 8, 8, 8);
     
        }
    }