3

我有一个JPanel名为GridPanel. 它允许您将图像从JList. 允许您使用GridPanel鼠标拖动图像并根据需要重新排列它们。我感兴趣的是制作GridPanel组件的缩略图视图。

如果我理解正确,将 aJScrollPane的视图设置GridPanelGridPanela 的孩子JViewPort,它成为JScrollPane. 目前GridPanel已经设置为a的视图,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

4

2 回答 2

2

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

  1. 截取渲染过程——覆盖paint(Graphics)方法
  2. 创建自己的图像来渲染
  3. 将图像渲染为原始图形
  4. 将图像渲染到其他组件 - 覆盖 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();
}

您可以在这里查看完整示例

于 2013-08-03T09:19:34.857 回答
0

您可以像这样覆盖paintComponent

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.scale(scaleFactor, scaleFactor);
    gridPanel.paintComponenet(g);
}

这假定gridPanel覆盖paintComponentaspublic或此类必须与GridPanel.

于 2013-08-03T08:43:17.667 回答