您可以覆盖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) 和原始计算中使用的幻数得出的坐标示例:
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;