是否可以将转换应用于 Swing 中的自定义或预制控件?一方面允许转换,另一方面在实现中可能存在一些差距。
注意问题是关于如何从控件的父级应用转换,而不是关于如何使用转换。即转换必须由父母发出,而孩子应该服从它。因此,请提示如何转换标准 Swing 控件或如何编写遵循 PARENT 转换的自定义控件。
在绘制孩子之前应用变换Graphics
并且不起作用的简单示例:
public class Tester_TransformDuringPaint_01 {
private static Logger log = LoggerFactory.getLogger(Tester_TransformDuringPaint_01.class);
private static class JPanelEx extends JPanel {
private AffineTransform transform = new AffineTransform();
public AffineTransform getTransform() {
return transform;
}
public void setTransform(AffineTransform transform) {
this.transform = transform;
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
AffineTransform savedTransform = g2.getTransform();
g2.transform(transform);
super.paintComponent(g);
g2.drawOval(0, 0, 100, 100);
g2.setTransform(savedTransform);
}
@Override
protected void paintChildren(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
AffineTransform savedTransform = g2.getTransform();
g2.transform(transform);
super.paintChildren(g);
g2.setTransform(savedTransform);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JButton button = new JButton("Button");
button.setBounds(0,20,100,60);
JPanelEx panel = new JPanelEx();
panel.setLayout(null);
panel.setBounds(10, 10, 640, 480);
panel.setBackground(Color.PINK);
panel.setTransform(AffineTransform.getScaleInstance(2, 1));
panel.add(button);
JFrameEx frame = new JFrameEx();
frame.setLayout(null);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(0.5);
frame.center();
frame.setVisible(true);
}
});
}
}
绘制以下内容:
而按钮的左半部分看起来还活着,而更大的部分看起来已经死了。
这是因为 API 的不同部分使用不同的方法绘制按钮。
修改后的 O'Reilly hack 51
下面是基于@lbalazscs 示例的代码,它表明即使转换在“范围内”也不起作用
public class BackwardsJButton extends JButton {
public BackwardsJButton(String text) {
super(text);
}
public void paint(Graphics g) {
if (g instanceof Graphics2D) {
Graphics2D g2 = (Graphics2D) g;
AffineTransform savedTransform = g2.getTransform();
AffineTransform flipTrans = new AffineTransform();
double widthD = (double) getWidth();
//flipTrans.setToTranslation(widthD, 0);
//flipTrans.scale(-2.0, 1);
flipTrans.scale(0.5, 1);
g2.transform(flipTrans);
super.paint(g);
g2.setTransform(savedTransform);
} else {
super.paint(g);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
buildFrame();
}
});
}
private static void buildFrame() {
JFrame f = new JFrame("Test");
f.setLayout(new FlowLayout());
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.add(new BackwardsJButton("BackwardsJLabel"));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
输出如下(您可能需要调整窗口大小并移动鼠标才能看到它,因为 Swing 错误位于鼠标悬停代码中: