3

という拡張JPanelクラスがありGridPanelます。から画像をドラッグ アンド ドロップできますJList。では、GridPanelマウスで画像をドラッグして、必要に応じて並べ替えることができます。私が興味を持っているのは、GridPanelコンポーネントのサムネイル ビューを作成することです。

私の理解が正しければ、JScrollPaneのビューを に設定GridPanelすると、 のGridPanelJViewPortになり、 の子になりJScrollPaneます。現在GridPanel、すでに のビューになるように設定されており、2 つの親を持つことはできないとJScrollPane確信しています。GridPanelしたがって、2 つのコンポーネントで同じビューを共有することはできませんが、縮小された のビジュアル コピーをペイントするために必要なのはサムネイル ビューだけです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 つのコンポーネントの「レンダリング結果を取得」して再利用するだけです。それには次のものが必要です。

  1. レンダリング プロセスをインターセプトする - paint(Graphics) メソッドをオーバーライドする
  2. レンダリングする独自のイメージを作成する
  3. 画像を元のグラフィックスにレンダリングする
  4. 画像を他のコンポーネントにレンダリングする - paint(Graphics) メソッドをオーバーライドする

最初の 3 つの手順は、次のように実行できます。

@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);
}

これは、 asをgridPanelオーバーライドするか、このクラスが と同じパッケージに含まれている必要があることを前提としています。paintComponentpublicGridPanel

于 2013-08-03T08:43:17.667 に答える