如题

解决方案 »

  1.   

    这个有点复杂。因为Component并没有默认的拉伸事件监听。给你提供个思路吧。建一个继承ImageIcon的类,比如说叫MyImage,里面监听MouseMotionEvent.mouseMoved事件,判断鼠标的坐标,比如说是
    (0, 0)--(5, 5)这个范围的话,鼠标光标变成NW箭头,
    (width-5, 0)--(width, 5)的话变成NE箭头
    (0, height-5)--(5, height)的话变成SW箭头
    (width-5, height-5)--(width, height)的话变成SE箭头这些是鼠标在四角位置的判断。在调用这个MyImage类的时候,为其添加MouseEvent,监听其MouseDragged事件,根据鼠标所在位置决定是哪一点的坐标如何变化。四个角需要单独照顾到。逻辑上不复杂,实现起来代码稍微有点繁琐,主要是判断鼠标所在的角,和鼠标移动时图片的尺寸和坐标应该怎么设置。
      

  2.   

    不好意思,上面判断四角的坐标可能不对,更正一下
    左上:(0,0)--(5,5)
    右上:(width,0)--(width-5,5)
    左下:(0,height)--(5,height-5)
    右下:(width,height)--(width-5,height-5)
      

  3.   


    package testGUI;import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;import javax.imageio.ImageIO;
    import javax.swing.JFrame;public class TestImage extends JFrame { private static final long serialVersionUID = 1L; private static final String filename = "C:/1.jpg"; private volatile BufferedImage image = null; public TestImage() {
    init();
    } private void init() {
    this.getContentPane().setLayout(new BorderLayout());
    this.setSize(new Dimension(800, 600));
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.addComponentListener(new MyAdapter(this));
    this.setVisible(true);
    } public BufferedImage getImage(boolean reload) {
    if (image == null || reload) {
    try {
    image = ImageIO.read(new File(filename));
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return image;
    } public void resize(int height, int width, boolean bb) {
    try {
    double ratio = 0.0;
    File f = new File(filename);
    BufferedImage bi = getImage(false);
    Image itemp = getImage(false).getScaledInstance(width, height,
    BufferedImage.SCALE_SMOOTH);
    if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
    if (bi.getHeight() > bi.getWidth()) {
    ratio = (new Integer(height)).doubleValue()
    / bi.getHeight();
    } else {
    ratio = (new Integer(width)).doubleValue() / bi.getWidth();
    }
    AffineTransformOp op = new AffineTransformOp(AffineTransform
    .getScaleInstance(ratio, ratio), null);
    itemp = op.filter(bi, null);
    }
    if (bb) {
    BufferedImage image = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, width, height);
    if (width == itemp.getWidth(null))
    g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
    itemp.getWidth(null), itemp.getHeight(null),
    Color.white, null);
    else
    g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
    itemp.getWidth(null), itemp.getHeight(null),
    Color.white, null);
    g.dispose();
    itemp = image;
    }
    ImageIO.write((BufferedImage) itemp, "jpg", f);
    TestImage.this.getGraphics().drawImage(getImage(true), this.getX(),
    this.getY(), TestImage.this);
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(getImage(false), this.getX(), this.getY(), this);
    } /**
     * @param args
     */
    public static void main(String[] args) {
    new TestImage();
    }
    }class MyAdapter extends ComponentAdapter {
    TestImage adaptee; MyAdapter(TestImage adaptee) {
    this.adaptee = adaptee;
    } public void componentResized(ComponentEvent e) {
    adaptee.resize(adaptee.getHeight(), adaptee.getWidth(), true);
    }
    }