...それがお役に立てば幸い高さ(おそらく描画コードは一般的です)。
この例は、いくつかの描画ロジック(大きなX、パネルのサイズ)を持つパネルを含むフレームを持っています。この例の主な方法は、画像のサイズがパネルのサイズと異なる場合でも、画像を取得してファイルに書き込む方法を示しています。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MockFrame extends JFrame {
// throws Exception, as just an example (not really advised to do this)
public static void main(String[] args) throws Exception {
MockFrame frame = new MockFrame();
frame.setVisible(true);
// different sizes from the frame
int WIDTH = 500;
int HEIGHT = 500;
BufferedImage b = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) b.getGraphics();
// should set some background, as the panel's background
// is dealt with by super.paintComponent()
g2d.setBackground(Color.white);
frame.getPanel().drawingLogic(b.getGraphics(), WIDTH, HEIGHT);
ImageIO.write(b, "png", new File("test.png"));
}
private MockPanel panel;
public MockFrame() {
this.setSize(200, 200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new MockPanel();
getContentPane().add(panel);
}
public MockPanel getPanel() {
return panel;
}
private class MockPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawingLogic(g, getWidth(), getHeight());
}
public void drawingLogic(Graphics g, int width, int height) {
g.setColor(Color.black);
g.drawLine(0, 0, width, height);
g.drawLine(0, height, width, 0);
}
}
}
これにより、GUIの外部にあるオブジェクトが描画アルゴリズムにフックできるようになります。私が見る1つの欠点は、パネルを実際に実装するためにパネルを印刷したいオブジェクト間の依存関係を作成することです。しかし、それは(私はそれを試してみましたが、いくつかの問題を持っていると思われている、まだその場でパネルのサイズを変更するよりはましだ - 私はそれが伝播するsetSize()
変更のためのいくつかの時間がかかると思います
EDIT:
受けて私は上記のコードスニペットの修正版を提供しました。おそらくそれを行う最良の方法ではなく、あまりユーザーフレンドリーではありません(はエンドユーザーアプリケーションでを使用しません)。レイアウトマネージャのルールに従ってフレーム内のすべてのサイズを変更します。
/* This code snippet describes a way to resize a frame for printing at
* a custom size and then resize it back.
*
* Copyright (C)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MockFrame extends JFrame {
// throws Exception, as just an example (not really advised to do this)
public static void main(String[] args) throws Exception {
final MockFrame frame = new MockFrame();
frame.setVisible(true);
// different sizes from the frame
final int WIDTH = 500;
final int HEIGHT = 700;
final BufferedImage b = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2d = (Graphics2D) b.getGraphics();
final int previousWidth = frame.getWidth();
final int previousHeight = frame.getHeight();
frame.setSize(WIDTH, HEIGHT);
frame.repaint();
JOptionPane.showMessageDialog(null,
"Press OK when the window has finished resizing");
frame.print(g2d);
frame.setSize(previousWidth, previousHeight);
ImageIO.write(b, "png", new File("test.png"));
}
public MockFrame() {
this.setSize(200, 200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
boolean shouldFill = true;
boolean shouldWeightX = true;
Container pane = getContentPane();
// code from
// http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html
// just to add some components in the frame... :)
// left out in here for brevity
}
}
コードは、基本的に、フレームのサイズを変更し再描画が完了するまでスレッドがブロックできるようにユーザに確認メッセージを示している(Thread.sleep()
によって行うことができ、それはメッセージを使用して、より透明'S)。次に、フレームを印刷し、元の形状に戻します。少しハッキーですが、それは動作します。
-- Flaviu Cipcigan
コンポーネントはレイアウトマネージャの制御下にあり、したがって、独自のサイズを制御することはできません。それを呼び出すことは何もしません。 –