Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 关于JPanel图像,缩放和平移_Java_Image_Swing_Zooming_Paintcomponent - Fatal编程技术网

Java 关于JPanel图像,缩放和平移

Java 关于JPanel图像,缩放和平移,java,image,swing,zooming,paintcomponent,Java,Image,Swing,Zooming,Paintcomponent,这是我目前的代码: class loadImage extends JPanel { private int choice; double scale; double currScale = 1.0; BufferedImage img; Timer timer; @Override public void paintComponent(Graphics g) { super.paintComponent(g); System.out.println(choice);

这是我目前的代码:

class loadImage extends JPanel {
private int choice;
double scale;
double currScale = 1.0;
BufferedImage img;
Timer timer;

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    System.out.println(choice);

     g.drawImage(img, 0, 0, getWidth(), getHeight(), null);

    if (choice == 1) 
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        int w = getWidth();
        int h = getHeight();
        int imageWidth = img.getWidth();
        int imageHeight = img.getHeight();
        double x = (w - currScale * imageWidth) / 2;
        double y = (h - currScale * imageHeight) / 2;
        AffineTransform at = AffineTransform.getTranslateInstance(x, y);
        at.scale(currScale, currScale);
        g2.drawRenderedImage(img, at);
    }
    else if (choice == 2)
    {

        Graphics2D g2 = (Graphics2D)g;
        int x = 5;
        int y = getHeight() - img.getHeight();
        AffineTransform at = AffineTransform.getTranslateInstance(x, y);
        at.setToTranslation(x, y);
        g2.drawRenderedImage(img, at);
    }
}                            

public loadImage() {
    choice = 0;
    try {
        img = ImageIO.read(new File("pic.png"));
    } catch (IOException ex) {
    }

}

public void setChoice(int choice) {
    this.choice = choice;
}


public void setCurrScale(double currScale) {
    this.currScale = currScale;
    repaint();
}

public double getCurrScale() {
    return currScale;
}    
}


public class NewJFrame extends javax.swing.JFrame {

/**
 * Creates new form NewJFrame
 */
public NewJFrame() {
    initComponents();
    setLocationRelativeTo(null);

}

public void ZoomIn(double scale) {
    if(scale > ((loadImage)imageArea).getCurrScale())
    {
        double scaleAddOn = 0.1;
        Timer timer;
        timer = new Timer(40, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                double currScale;
                currScale = ((loadImage)imageArea).getCurrScale();
                if(currScale < scale)
                {
                    ((loadImage)imageArea).setCurrScale(currScale + scaleAddOn);
                }
            }
        });

        if(((loadImage)imageArea).getCurrScale() >= scale)
        {
             timer.stop();
        }
         else
         {
             timer.setRepeats(true);
             timer.setCoalesce(true);
             timer.start();  
         }
    }
    else JOptionPane.showMessageDialog(rootPane, "Fail Zoom In\nScale: " + scale + "\nCurrScale: " + ((loadImage)imageArea).getCurrScale());
}
public void ZoomOut(double scale) {
    if(scale < ((loadImage)imageArea).getCurrScale())
    {
    double scaleDecrement = 0.1;
    Timer timer;
    timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            double currScale;
            currScale = ((loadImage)imageArea).getCurrScale();
            if(scale < currScale)
            {
                ((loadImage)imageArea).setCurrScale(currScale - scaleDecrement);
            }
        }
    });

    if(scale > ((loadImage)imageArea).getCurrScale())
    {
         timer.stop();
    }
     else
     {
         timer.setRepeats(true);
         timer.setCoalesce(true);
         timer.start();  
     }
    }         
    else JOptionPane.showMessageDialog(rootPane, "Fail Zoom Out\nScale: " + scale + "\nCurrScale: " + ((loadImage)imageArea).getCurrScale());
}      


private void resetImageActionPerformed(java.awt.event.ActionEvent evt) {                                         
    ((loadImage)imageArea).setChoice(0);
    this.repaint();
}                                        

private void zoomInActionPerformed(java.awt.event.ActionEvent evt) {                                         
    ((loadImage)imageArea).setChoice(1);
    this.repaint();
    ZoomIn(3.0);
}                                        

private void printScaleActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.out.println(((loadImage)imageArea).getCurrScale());
}                                        

private void zoomOutActionPerformed(java.awt.event.ActionEvent evt) {                                         
    ((loadImage)imageArea).setImage(1);
    this.repaint();
    ZoomOut(1.0);
}                                        
目标是显示图像,然后添加放大、缩小和移动

图像的详细信息=其尺寸为1280 x 1024 GUI jpanel的尺寸是563 x 377。 当程序运行时,图像正好适合JPanel 563 x 377

放大完成了它的工作,但当我执行放大并减慢计时器时,我注意到它似乎并没有开始跟随JPanel的维度,而是跟随原始维度。当放大代码运行时,它会恢复到原来的尺寸(现在在jpanel中被裁剪),然后进行缩放

缩小代码的作用与图像大小的变化相同。它实际上并没有缩小。我最终在屏幕上看到的是图像放大了一点,然后又缩小了一点,缩小的幅度与放大的幅度相同。它就在那个恒定的循环中。我按下printScale按钮,它返回完全相同的数字,即使此时显示的是循环的哪个部分

如果choice==2,则转换也是相同的。它不遵循JPanel的图像大小,而是实际的图像大小

为什么即使g.drawImageimg,0,0,getWidth,getHeight,null,选项中的图像也不跟随图像的大小;有人打过电话吗? 为什么我的缩小方法是这样的? 抱歉读了这篇不愉快的文章。感谢您的帮助

我的解决方案:
我最终放弃了线程对应的计时器,放大和缩小不再有问题。我仍在试图找出g.drawImage的东西,以及如何让它们保持一致。当我弄明白后,我会再次更新此信息。

1若要更快获得更好的帮助,请发布或。例如,获取图像的一种方法是热链接到中看到的图像。我使用Netbean的GUI生成器,因为我不擅长。我真的不知道如何编写拖放GUI。我想这件事不会有人回答。无论如何,谢谢你的提醒。