0

JComponentの描画をtiff形式に保存するには? Java ファイル全体を保存する方法しか知りませんが、特定の Jcomponent を保存する方法はわかりません。助けて :-(

編集済み: おかげさまで、図面を Jpeg に保存できるようになりました。

ただし、コンポーネントの1つを保存したかっただけですか? c.paintAll(bufferedImage.getGraphics());コンポーネント全体を保存しているようです。しかし、 どうすればそれc.add(new PaintSurface(), BorderLayout.CENTER);panel.add(saveBtn);行うことができますか? ありがとう。

Container c = getContentPane();
c.setLayout(new BorderLayout());      
Panel panel = new Panel();
panel.add(saveBtn);
c.add("South", panel);
c.setBackground(Color.WHITE);
c.add(new PaintSurface(), BorderLayout.CENTER);
4

3 に答える 3

1

これは、正しい構文を使用し、適切な JAI ルーチンを実際に呼び出すだけで、 broschb のソリューションと本質的に同じです。

public void saveComponentAsTiff(Component c, String filename, boolean subcomp) throws IOException {
    saveComponentTiff(c, "tiff", filename, subcomp);
}

public void saveComponent(Component c, String format, String filename, boolean subcomp) throws IOException {
    // Create a renderable image with the same width and height as the component
    BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);

    if(subcomp) {
        // Render the component and all its sub components
        c.paintAll(image.getGraphics());
    }
    else {
        // Render the component and ignoring its sub components
        c.paint(image.getGraphics());
    }

    // Save the image out to file
    ImageIO.write(image, format, new File(filename));
}

さまざまな機能のドキュメントは、次の場所にあります。

tiff 以外の形式で保存する場合は、ImageIO.getWriterFormatNames()を使用して、現在 JRE によってロードされているすべての画像出力形式のリストを取得できます。

更新:サブコンポーネントのペイントに関心がない場合は、Component.paintAll(...) の呼び出しを Component.paint(...) に置き換えることができます。これを反映するようにサンプル コードを変更しました。subcomp を true に設定してサブコンポーネントをレンダリングし、それを false に設定すると、それらは無視されます。

于 2009-10-06T22:53:56.880 に答える
0

パネルのサイズのバッファリングされたイメージを作成することにより、コンポーネントのバッファリングされたイメージ、または図面を含むパネルを取得できます。その後、パネルのコンテンツをバッファリングされた画像にペイントできます。その後、JAI(Java Advanced Imaging) ライブラリを使用して、バッファリングされた画像を tiff として保存できます。ここでドキュメントを確認する必要があります。

JComponent component;  //this is your already created component
BufferedImage image = new BufferedImage(component.getWidth(),
                                        component.getHeight(),
                                        Bufferedimage.TYPERGB)

Graphics g = image.getGraphics;
component.paintComponent(g);

構文は少しずれている可能性があります。私は考えていませんが、それが一般的な考えです。その後、JAI を使用して、バッファリングされたイメージを TIFF に変換できます。

于 2009-10-06T22:43:47.350 に答える
0

ScreenImageクラスを使用すると、任意のコンポーネントの画像を保存できます。

于 2009-10-06T23:49:37.697 に答える