0

我想显示图像以放大和缩小并且可以移动。我有下面的脚本,但它不能移动。如何修复它。

编码 :

package dispertasih;

import static dispertasih.PanPanel.startX;
import java.awt.*;  
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.*;  
import java.awt.image.BufferedImage;  
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;  
import javax.swing.event.*;  

public class PadAndZoom implements ChangeListener {  
    BufferedImage image;  
    PanPanel label;  

    public void stateChanged(ChangeEvent e) {  
        int value = ((JSlider)e.getSource()).getValue();  
        double scale = value/200.0;  
        BufferedImage scaled = getScaledImage(scale);  
       label = new PanPanel(scaled);  
        label.revalidate();  // signal scrollpane  
    }  

    private BufferedImage getScaledImage(double scale) {  
        int w = (int)(scale*image.getWidth());  
        int h = (int)(scale*image.getHeight());  
        BufferedImage bi = new BufferedImage(w, h, image.getType());  
        Graphics2D g2 = bi.createGraphics();  
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,  
                            RenderingHints.VALUE_INTERPOLATION_BICUBIC);  
        AffineTransform at = AffineTransform.getScaleInstance(scale, scale);  
        g2.drawRenderedImage(image, at);  
        g2.dispose();  
        return bi;  
    }  

    private PanPanel getContent() {  
        createAnImage();  
        label = new PanPanel(image);  
        //label.setHorizontalAlignment(JLabel.CENTER);  
        return label;  
    }  

    private void createAnImage() {  
        int w = 500;  
        int h = 500;  
        int type = BufferedImage.TYPE_INT_RGB; // many options  
        try {
            image = ImageIO.read(PadAndZoom.class.getResource("/tampilan/background5.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(PadAndZoom.class.getName()).log(Level.SEVERE, null, ex);
        }


    }  

    private JSlider getControl() {  
        JSlider slider = new JSlider(JSlider.HORIZONTAL, 50, 200, 50);  
        slider.setMajorTickSpacing(50);  
        slider.setMinorTickSpacing(10);  
        slider.setPaintTicks(true);  
        slider.setPaintLabels(true);  
        slider.addChangeListener(this);  
        return slider;          
    }  

    public static void main(String[] args) {  
        PadAndZoom app = new PadAndZoom();  
        JFrame f = new JFrame();  
        JButton b = new JButton();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        f.getContentPane().add(new JScrollPane(app.getContent()));  

        //f.getContentPane().add(new JButton("tessss"));
        f.getContentPane().add(app.getControl(), "Last");  
        f.setSize(400, 400);  
        f.setLocation(200,200);  
        f.setVisible(true);  
    }  
}  
class PanPanelX extends JPanel {

private int x, y;
private int width = 800, height = 800;
BufferedImage img;
private final static RenderingHints textRenderHints = new    RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
private final static RenderingHints imageRenderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
private final static RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
static int startX, startY;

public PanPanelX(BufferedImage img) {
    x = 20;
    y = 20;
    this.img = img;

    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent me) {
            super.mousePressed(me);
            startX = me.getX();
            startY = me.getY();
        }
    });

    addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent me) {
            super.mouseDragged(me);

            if (me.getX() < startX) {//moving image to right
                x -= 2;
            } else if (me.getX() > startX) {//moving image to left
                x += 2;
            }

            if (me.getY() < startY) {//moving image up
                y -= 2;
            } else if (me.getY() > startY) {//moving image to down
                y += 2;
            }
            repaint();
        }
    });
}

@Override
protected void paintComponent(Graphics grphcs) {
    super.paintComponent(grphcs);
    Graphics2D g2d = (Graphics2D) grphcs;

    //turn on some nice effects
    applyRenderHints(g2d);

    g2d.drawImage(img, x, y, null);
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(width, height);
}

public static void applyRenderHints(Graphics2D g2d) {
    g2d.setRenderingHints(textRenderHints);
    g2d.setRenderingHints(imageRenderHints);
    g2d.setRenderingHints(renderHints);
}
}

我想:

public void stateChanged(ChangeEvent e) {  
        int value = ((JSlider)e.getSource()).getValue();  
        double scale = value/200.0;  
        BufferedImage scaled = getScaledImage(scale);  
       label = new PanPanel(scaled);  
        label.revalidate();  // signal scrollpane  
    } 

有错误,但我一直在修复它....

谢谢你 ...

4

1 回答 1

2

问题是,屏幕上显示的图像的参考与调整大小时创建的图像不同......

PanPanel通过使用创建...

BufferedImage scaled = getScaledImage(scale);
label = new PanPanel(scaled);

getScaledImage每次调用时都会根据原始图像创建一个新图像。

BufferedImage bi = new BufferedImage(w, h, image.getType());

这意味着,imageinPadAndZoom与 in 不同PanPanel,因此您对任何更改image或任何新的缩放实例都将永远不会被绘制,因为PanPanel对它们一无所知......

现在,在 中stateChanged,不是更新PanPanel已有的实例,而是创建它的新实例……就是这样……它从未添加到任何可显示的组件中,因此它永远不会出现在屏幕上。

相反,在您的 中PanPanel,您需要为该方法提供一些stateChanged方法来将图像的缩放实例传递给它......

public void setImage(BufferedImage img) {
    this.img = img;
    revalidate();
    repaint();
}

然后在你的stateChanged方法中你可以做......

int value = ((JSlider) e.getSource()).getValue();
double scale = value / 200.0;
BufferedImage scaled = getScaledImage(scale);
label.setImage(scaled);
于 2013-09-06T01:32:29.577 回答