java - 我希望图片在滚动时留在 jscrollpane 的左上角

标签 java swing jscrollpane imageicon jviewport

我在 JScrollpane 中有一个 JPanel。 我在 BufferedImage 上绘制,我将其显示在 JPanel 上。 在 JScrollpane 的左上角,我想要一张图片,当我向下滚动查看我的 JPanel 的其余部分时,它总是留在那个角落。 这里是 Jpanel 的 paintComponent 方法:

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    if (bufferedImage != null){
        g.drawImage(bufferedImage, 0, 0, this);
        Point p = parent.getViewPosition();
        System.out.println("paintComponent(): "+ p.x + "," + p.y);
        g.setColor(Color.RED);
        g.fillRect(p.x + 20, p.y + 20, 200, 200);
    }
}

其中 parent.getViewPosition() 给我 scrollPane.getViewport().getViewPosition()。 当我启动时,我可以看到左上角带有红色矩形的缓冲图像。 当我向下滚动时,我可以看到缓冲图像的其余部分,但是当我向上滚动时,红色矩形向上移动然后消失并且不再出现。 在控制台中,当我滚动时,我可以看到点 p 发生了变化:

paintComponent(): 0,0
paintComponent(): 0,10
paintComponent(): 0,20
paintComponent(): 0,30
paintComponent(): 0,40
paintComponent(): 0,50

谁能帮我解决这个问题?

最佳答案

您可以为此使用一 block 玻璃板,并告诉它在取决于视口(viewport)位置的位置绘制图像。例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.*;

@SuppressWarnings("serial")
public class ScrollImgGlass extends JPanel {
   private static final int BI_W = 40;
   private static final int BI_H = BI_W;
   private static final String[] DATA = { "One", "Two", "Three", "Four",
         "Five", "Six", "Seven", "Eight", "Nine", "Zero", "One", "Two",
         "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Zero",
         "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
         "Nine", "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
         "Eight", "Nine", "Zero" };
   private BufferedImage img = null;
   private JViewport viewport;

   public ScrollImgGlass(JViewport viewport) {
      setOpaque(false);
      this.viewport = viewport;
      img = new BufferedImage(BI_W, BI_H, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = img.createGraphics();
      g2.setColor(Color.red);
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      g2.fillOval(0, 0, BI_W, BI_H);
      g2.dispose();
   }

   @Override
   protected void paintComponent(Graphics g) {
      Point vpLocation = viewport.getLocationOnScreen();
      Point gpLocation = getLocationOnScreen();

      int x = vpLocation.x - gpLocation.x;
      int y = vpLocation.y - gpLocation.y;

      super.paintComponent(g);
      if (img != null) {
         g.drawImage(img, x, y, this);
      }
   }

   private static void createAndShowGui() {
      JList<String> jList = new JList<String>(DATA);
      jList.setOpaque(false);

      JViewport viewport = new JViewport();
      JScrollPane scrollpane = new JScrollPane();
      scrollpane.setViewport(viewport);
      viewport.setView(jList);

      ScrollImgGlass glass = new ScrollImgGlass(viewport);

      JFrame frame = new JFrame("ScrollImg");
      frame.setGlassPane(glass);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(scrollpane, BorderLayout.CENTER);

      // just to show that this works if the viewport is shifted over
      frame.getContentPane().add(Box.createRigidArea(new Dimension(20, 20)), BorderLayout.NORTH);
      frame.getContentPane().add(Box.createRigidArea(new Dimension(20, 20)), BorderLayout.WEST);

      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);

      glass.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

显示如下:

enter image description here

关于java - 我希望图片在滚动时留在 jscrollpane 的左上角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15467210/

相关文章:

java - 如何在spring hibernate中反序列化日期格式?

java - Spark : timestamp changes when reading from written file

java - getPassword() 方法每次返回不同的加密字符数组

java - RXTX 2.2pre2 使用内部 AWT/Swing 崩溃

java - 在 Java 中将 JLabels 数组添加到单个面板?

java - 当框架太小无法显示所有 JList 时,如何让滚动条出现在 JList 上?

java.lang.RuntimeException : Unable to instantiate activity (unable to open DEX file)

java - JVM JIT 诊断工具和优化技巧

java - JScrollPane 内的 JPanel JFrame 内的 JPanel 内

java - 如何防止 JTextField 中的箭头键滚动 JScrollPane?