English 中文(简体)
在 jframe 重新展开时重新缩放三次三次
原标题:resize cubiccurve on jframe resisize

我有这个曲线形状,我希望宽度和高度与我的JFrame的大小相对相应调整,例如,我的JFrame尺寸是固定的Size(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);
    }
}

EDI:坐标示例

以下是从集装箱最初的某一尺寸(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;




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签