gpt4 book ai didi

java - AffineTransform 似乎忽略了组件边界

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:29:17 25 4
gpt4 key购买 nike

我有以下内容:

public class ParametricEQView extends JPanel implements PluginView {
private static final int BAND_WIDTH = 3;
private static final int THROW_HEIGHT = 64;

private static final int WIDTH = 128*BAND_WIDTH + 2*MARGIN;
private static final int HEIGHT = 2*THROW_HEIGHT + 2*MARGIN;
private static final int MID_HEIGHT = THROW_HEIGHT + MARGIN;

private final ParametricEQ _peq;

public ParametricEQView(ParametricEQ peq) {
super();
_peq = peq;

SwingUtils.freezeSize(this, WIDTH, HEIGHT);
setToolTipText("Parametric Equalizer");
}

@Override
public void paint(Graphics g) {
final Graphics2D g2d = (Graphics2D) g;
final int max = findMax();

g.setColor(BACKGROUND);
g.fillRect(0, 0, WIDTH, HEIGHT);

g.setColor(DATA);
final double scalingFactor = -((double) THROW_HEIGHT) / max;
final double[] fineLevels = _peq.getFineLevels();
int x = MARGIN;
int h;
final int[] xPoints = new int[128];
final int[] yPoints = new int[128];
for (int i = 0; i < 128; ++i) {
h = (int) (fineLevels[i] * scalingFactor);
xPoints[i] = x;
yPoints[i] = MID_HEIGHT + h;
x += BAND_WIDTH;
}
g.drawPolyline(xPoints, yPoints, 128);

g.setColor(AXES);
g.drawLine(MARGIN, MARGIN, MARGIN, HEIGHT-MARGIN);
g.drawLine(MARGIN, MID_HEIGHT, WIDTH-MARGIN, MID_HEIGHT);

g.setFont(AXIS_FONT);
final FontMetrics metrics = g.getFontMetrics();
int width = (int) metrics.getStringBounds(AXIS_LABEL_INPUT_MIDINUM, g).getWidth();
g.drawString(AXIS_LABEL_INPUT_MIDINUM, WIDTH-MARGIN-width, HEIGHT-3);

final AffineTransform atx = new AffineTransform();
atx.setToRotation(-Math.PI/2, 0, HEIGHT);
g2d.setTransform(atx);
final String topLabel = "+" + max;
width = (int) metrics.getStringBounds(topLabel, g).getWidth();
g2d.drawString(topLabel, HEIGHT-MARGIN-width, HEIGHT+10);

width = (int) metrics.getStringBounds(AXIS_LABEL_OUTPUT_VELOCITY, g).getWidth();
g2d.drawString(AXIS_LABEL_OUTPUT_VELOCITY, MID_HEIGHT-(width/2), HEIGHT+10);

g2d.drawString("-" + max, MARGIN, HEIGHT+10);
}

private int findMax() {
int max = 3;
for (int i = 0; i < 128; ++i)
max = Math.max(max, (int) Math.ceil(Math.abs(_peq.getFineLevels()[i])));
return max;
}
}

这是它的样子: With bounds at 0,0
ParametricEQView 是白色背景填充大部分窗口的组件。在此图像中,它的坐标在包含框架中为 (0,0),一切都很好。但是,如果我调整窗口大小使 ParametricEQView 移动一点(它具有固定大小并设置为在其可用空间中居中),则旋转的文本保持相对于 (0, 0) 框架而不是组件:

Starting to move Fully off the component


其他一切都相对于面板绘制,只是旋转的文本没有。我做错了什么?

最佳答案

当您调用 g2d.setTransform(atx); 时,您会覆盖当前在 Graphics 对象中设置的转换,即面板与其父框架之间的转换。这就是为什么文本是在框架引用中绘制的,而不是在面板引用中绘制的。

正确的代码是获取当前变换并修改它或直接调用 Graphics2D.rotate(double)

关于java - AffineTransform 似乎忽略了组件边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20763027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com