我会将绘图代码从paintComponent()
您的实现方法重构JPanel
为该面板中的公共/包保护方法,该方法可以绘制到任意Graphics
宽度/高度的任意对象(大概绘图代码足够通用)。
这个例子有一个框架,包含一个面板,它有一些绘图逻辑(一个大 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 外部的对象与它的绘图算法挂钩。我看到的一个缺点是,您将在任何想要打印面板的对象与面板的实际实现之间创建依赖关系。但这仍然比动态调整面板的大小要好(我已经尝试过并且似乎有一些问题 - 我认为setSize()
传播更改需要一些时间。
编辑:
作为对评论的回应,我提供了上面代码片段的修改版本。这可能不是最好的方法,也不是非常用户友好(所以我不会在最终用户应用程序中使用它),但它会根据布局管理器的规则调整框架中的所有内容。
/* 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()
,但使用消息更透明)。然后它打印框架并将其调整回原来的形状。有点hacky,但它有效。
-- Flaviu Cipcigan