当我在 jscrollpane 上上下滚动时,图像的某些部分消失了

大家好,我对在 a 中绘制的图像有疑问JPanel。将图像绘制到面板时没有问题。当我向上或向下滚动图像时出现问题。当再次返回该部分时,在视口中未看到的图像部分会被涂成白色。

我 BufferedImage 在JPanel. 我还可以使用调整图像大小AffineTransform。问题是当我JScollPane在图像中添加 a 时,每当我向上或向下滚动时,scrollpane图像的某些部分就会消失..

我也尝试过使用drawRenderedImage它确实解决了滚动的问题,但它搞砸了其他功能。

这是我能做的最简洁的代码;

package convert;


import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.awt.image.BufferedImage;

import java.io.*;

import java.net.*;

import java.util.ArrayList;

import javax.imageio.ImageIO;

import javax.swing.*;

import javax.swing.event.*;


public class ZoomTest {


    public static void main(String[] args) {

        ImagePanel panel = new ImagePanel();


        JFrame f = new JFrame();

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        f.getContentPane().add(new JScrollPane(panel));

        f.setSize(1200, 1200);

        f.setLocationRelativeTo(null);

        f.setVisible(true);

    }

}


class ImagePanel extends JPanel {


    BufferedImage image;

    double scale;


    public ImagePanel() {

        loadImage();

        scale = .38;

        addMouseWheelListener(new MouseWheelListener() {


            @Override

            public void mouseWheelMoved(MouseWheelEvent e) {

                int rotation = e.getWheelRotation();

                if (rotation < 0) {

                    scale -= .05;

                } else {

                    scale += .05;

                }

                if (scale < 0) {

                    scale = 0;

                }


                revalidate();

                repaint();


            }

        });


    }


    @Override

    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;


        double x = (getWidth() - scale * image.getWidth()) / 2;

        double y = (getHeight() - scale * image.getHeight()) / 2;


        AffineTransform at = new AffineTransform();

        at.translate(x, y);


        at.scale(scale, scale);

        g2.setTransform(at);


        g2.drawImage(image, 0, 0, null);


    }



至尊宝的传说
浏览 112回答 2
2回答

holdtom

看看Graphics2D#drawImage(Image, AffineTransform, ImageObserver)。可能有帮助。package convert;import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import java.awt.image.BufferedImage;import java.io.*;import java.net.*;import java.util.ArrayList;import javax.imageio.ImageIO;import javax.swing.*;import javax.swing.event.*;public class ZoomTest2 {  public static void main(String[] args) {    ImagePanel panel = new ImagePanel();    JFrame f = new JFrame();    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    f.getContentPane().add(new JScrollPane(panel));    f.setSize(1200, 1200);    f.setLocationRelativeTo(null);    f.setVisible(true);  }}class ImagePanel extends JPanel {  BufferedImage image;  double scale;  public ImagePanel() {    loadImage();    scale = .38;    addMouseWheelListener(new MouseWheelListener() {      @Override      public void mouseWheelMoved(MouseWheelEvent e) {        int rotation = e.getWheelRotation();        if (rotation < 0) {          scale -= .05;        } else {          scale += .05;        }        if (scale < 0) {          scale = 0;        }        revalidate();        repaint();      }    });  }  @Override  protected void paintComponent(Graphics g) {    super.paintComponent(g);    Graphics2D g2 = (Graphics2D) g.create();    double x = (getWidth() - scale * image.getWidth()) / 2;    double y = (getHeight() - scale * image.getHeight()) / 2;    AffineTransform at = new AffineTransform();    at.translate(x, y);    at.scale(scale, scale);    // g2.setTransform(at);    // g2.drawImage(image, 0, 0, this);    g2.drawImage(image, at, this);    // or:    // AffineTransform atf = g2.getTransform();    // atf.concatenate(at);    // g2.setTransform(atf);    // g2.drawImage(image, 0, 0, this);    g2.dispose();  }  public Dimension getPreferredSize() {    int w = (int)(scale * image.getWidth());    int h = (int)(scale * image.getHeight());    return new Dimension(w, h);  }  private void loadImage() {    String fileName = "aaa.png";    try {      image = ImageIO.read(new File(fileName));    } catch (Exception e) {      e.printStackTrace();    }  }}

MMTTMM

这是您的问题的解决方案。我在自定义绘画方面也很不走运,所以我的解决方案是直接更新鼠标滚轮上的图像并将其设置为标签。import java.awt.Image;import java.awt.event.MouseWheelEvent;import java.awt.event.MouseWheelListener;import java.awt.image.BufferedImage;import java.io.File;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JScrollPane;import javax.swing.SwingConstants;public class ZoomTest {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; ImagePanel panel = new ImagePanel();&nbsp; &nbsp; &nbsp; &nbsp; JFrame f = new JFrame();&nbsp; &nbsp; &nbsp; &nbsp; f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; f.getContentPane().add(new JScrollPane(panel));&nbsp; &nbsp; &nbsp; &nbsp; f.setSize(1000, 1000);&nbsp; &nbsp; &nbsp; &nbsp; f.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; f.setVisible(true);&nbsp; &nbsp; }}class ImagePanel extends JLabel {&nbsp; &nbsp; BufferedImage image;&nbsp; &nbsp; double scale;&nbsp; &nbsp; public ImagePanel() {&nbsp; &nbsp; &nbsp; &nbsp; setHorizontalAlignment(SwingConstants.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; loadImage();&nbsp; &nbsp; &nbsp; &nbsp; scale = .38;&nbsp; &nbsp; &nbsp; &nbsp; updateImage();&nbsp; &nbsp; &nbsp; &nbsp; addMouseWheelListener(new MouseWheelListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void mouseWheelMoved(MouseWheelEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int rotation = e.getWheelRotation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rotation < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scale -= .05;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scale += .05;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (scale < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scale = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateImage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; revalidate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; private void updateImage() {&nbsp; &nbsp; &nbsp; &nbsp; int w = (int) (scale * image.getWidth());&nbsp; &nbsp; &nbsp; &nbsp; int h = (int) (scale * image.getHeight());&nbsp; &nbsp; &nbsp; &nbsp; setIcon(new ImageIcon(image.getScaledInstance(w, h, Image.SCALE_SMOOTH)));&nbsp; &nbsp; }&nbsp; &nbsp; private void loadImage() {&nbsp; &nbsp; &nbsp; &nbsp; String fileName = "c:\\users\\john ebarita\\downloads\\lorem-ipsum-1.jpg";&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image = ImageIO.read(new File(fileName));&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java