gpt4 book ai didi

java - 在jframe resizeize上调整cubiccurve的大小

转载 作者:行者123 更新时间:2023-12-01 15:22:31 25 4
gpt4 key购买 nike

我有这个曲线形状,我希望宽度和高度相对于我的 JFrame 的大小相应地调整大小,例如我的 JFrame 大小是 setSize(440, 300); -- 然后,如果我最大化 JFrame,我希望曲线形状也能调整大小,以便形状保持其实际形状。任何帮助,请提前感谢。谢谢,

这是我的代码:

float offset = (float) Math.sin(Math.PI);

x1 = offset;
y1 = (height/4.0f) - 4.0f;

x1ctl = ((width/4) - 140) + 90.0f;
y1ctl = ((height/4) - 100) + 20.0f;

x2ctl = ((width/4) - 10.0f) + 60.0f;
y2ctl = ((height/4) - 8.0f) + 1.0f;

x2 = (width/2.0f) - 20.0f;
y2 = offset - 4.0f;

curve = new CubicCurve2D.Float(
x1,y1,
x1ctl,y1ctl,
x2ctl,y2ctl,
x2,y2);

g2d.draw(curve);

最佳答案

您可以重写paintComponent并使用组件的尺寸。以下是基于问题中的论点的示例:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.CubicCurve2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class CubicCurveComponentTest extends JComponent {

public void paintComponent(Graphics g) {

float offset = (float) Math.sin(Math.PI);

float x1 = offset;
float y1 = (getHeight()/4.0f) - 4.0f;

float x1ctl = ((getWidth()/4) - 140) + 90.0f;
float y1ctl = ((getHeight()/4) - 100) + 20.0f;

float x2ctl = ((getWidth()/4) - 10.0f) + 60.0f;
float y2ctl = ((getHeight()/4) - 8.0f) + 1.0f;

float x2 = (getWidth()/2.0f) - 20.0f;
float y2 = offset - 4.0f;

CubicCurve2D curve = new CubicCurve2D.Float(
x1,y1,
x1ctl,y1ctl,
x2ctl,y2ctl,
x2,y2);

Graphics2D g2 = (Graphics2D) g;
g2.draw(curve);
}

private static void createAndShowGUI() {
JFrame f = new JFrame("Cubic Curve Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(440, 300);
f.add(new CubicCurveComponentTest());
f.setVisible(true);
}

public static void main(String args[]) {
Runnable doCreateAndShowGUI = new Runnable() {
public void run() {
createAndShowGUI();
}
};
SwingUtilities.invokeLater(doCreateAndShowGUI);
}
}

编辑:坐标示例

下面是从容器的给定初始尺寸 (440, 300) 导出的坐标示例以及原始计算中使用的魔数(Magic Number):

    float x1 = 0;
float y1 = getHeight() * 0.24f;

float x1ctl = getWidth() * 0.125f;
float y1ctl = 0;

float x2ctl = getWidth() * 0.24f;
float y2ctl = getHeight() * 0.22f;

float x2 = getWidth() * 0.45f;
float y2 = -getHeight() * 0.013f;

关于java - 在jframe resizeize上调整cubiccurve的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10694103/

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