我有一个名为JPanel的扩展GridPanel类。它使您可以将图像从JList拖放到其中。 GridPanel允许您用鼠标拖动图像并根据需要重新排列图像。我感兴趣的是制作GridPanel组件的缩略图。

如果我理解正确,将JScrollPane的视图设置为GridPanel会使GridPanelJViewPort的子代,而该子集又成为JScrollPane的子代。目前GridPanel已经设置为JScrollPane的视图,我很确定GridPanel不能有两个父母。因此,我不能让两个组件共享同一视图,但实际上我只需要缩略图视图即可绘制GridPanel的缩放可视副本。

这引出了我的问题。是否可以复制GridPanel绘制的内容,但将其绘制在完全独立的组件上?

这是我尝试过的示例,以防万一我不被理解。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TestMain {
public static void main(String[] a) {
    Color[] colors = new Color[]{Color.green, Color.red, Color.blue, Color.yellow};


    final JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationByPlatform(true);
    JPanel content = new JPanel();
    frame.setContentPane(content);
    content.setLayout(new FlowLayout());
    //using variable names to try and help relate to my question
    final JPanel gridPanel = new JPanel();
    gridPanel.setPreferredSize(new Dimension(200,200));
    gridPanel.setLayout(new GridLayout(2, 2));
    JLabel label;
    for (Color c: colors){
        label = new JLabel();
        label.setOpaque(true);
        label.setBackground(c);
        gridPanel.add(label);
    }

    JScrollPane gridScroll = new JScrollPane(gridPanel);

    final JScrollPane thumbnailScroll = new JScrollPane();
    thumbnailScroll.setPreferredSize(new Dimension(200,200));

    JButton tryThumbnailView = new JButton("Activate Thumbnail");
    tryThumbnailView.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            thumbnailScroll.setViewportView(gridPanel);
            frame.repaint();

        }
    });
    content.add(tryThumbnailView);

    content.add(gridScroll);
    content.add(thumbnailScroll);

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


我希望发生的是两个组件都显示相同的彩色JLabels集,而不必复制这些JLabels

最佳答案

您要做的就是“获取一个组件的渲染结果”并重新使用它。
这需要:


截取渲染过程-覆盖paint(Graphics)方法
创建自己的图像以渲染到
将图像渲染为原始图形
将图像渲染到其他组件-覆盖paint(Graphics)方法


前三个步骤可以这样完成:

@Override
public void paint(Graphics originalGraphics) {
  GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
  GraphicsConfiguration c = e.getDefaultScreenDevice().getDefaultConfiguration();
  //create own image to paint to
  BufferedImage image = c.createCompatibleImage(getWidth(), getHeight());
  Graphics2D reusableGraphics = image.createGraphics();
  //let it paint into our graphics
  super.paint(reusableGraphics);
  // draw image on this component
  originalGraphics.drawImage(image, 0, 0, null);
  // draw image on other component
  otherComponent.setMirrorImage(image);
}


在otherComponent中,您必须保存图像并在需要时绘制它:

@Override
public void paint(Graphics g) {
  if (mirroredImage == null) {
    super.paintAll(g);
  } else {
    g.drawImage(mirroredImage, 0, 0, getWidth() * 3 / 4, getHeight() * 3 / 4, null);
  }
}
public void setMirrorImage(BufferedImage mirroredImage) {
  this.mirroredImage = mirroredImage;
  repaint();
}


您可以看一下here作为完整示例

10-05 19:33