2012-05-21 2 views
0

커브 모양이 있으며, JFrame 크기에 비례하여 너비와 높이 크기를 조정해야합니다. 예를 들어 JFrame 크기는 setSize (440, 300)입니다. - 그런 다음 JFrame을 최대화하면 커브 모양의 크기가 조정되어 모양이 실제 모양을 유지할 수 있기를 바랍니다. 어떤 도움이라도 미리 감사드립니다.jframe resisize에서 큐브 커브 크기 변경

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); 

답변

1

당신은 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); 
    } 
} 

EDIT : 여기서 문제의 인자에 기초하여 예는 여기에서 예를

좌표 용기의 소정의 초기 측정 유래의 좌표의 예 (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; 
+0

빠른 응답을 가져 주셔서 감사합니다. 감사합니다. 네, paintComponent를 오버라이드하고 있습니다. 내가 찾고 있던 것은 JFrame 크기를 조정할 때 커브가 실제 모양을 유지할 수있는 방법이 있습니까? 예를 들어 프레임 크기는 (440, 300)으로 설정되고 JFrame을 최대화하려고하면 큐브 커브는 자연스러운 곡선 형태를 잃어버린 직선처럼 보이기 시작합니다. 프레임을 크기를 조정할 때 곡선 점을 따라갈 수있는 방법을 찾으려고 노력 중입니까? – eLL

+0

@eLL 예제에서 곡선 좌표 뒤에있는 논리가 무엇인지 모르지만 440과 300에 묶여있는 것처럼 보입니다. 컨테이너의 크기를 조정할 때 커브가 계속 유지되도록보다 일반적인 수식이 있어야합니다. – tenorsax

+0

그것은 정확히 당신이 생각하는 것입니다. 이 커브 포인트에 대한 수식을 찾고 있는데, Jframe의 크기가 커지거나 작아지면 cubiccurve가 형태를 유지하는지 여부를 알 수 있습니까?이를 달성하는 방법을 더 정확하게 알 수 있습니까? 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; 도움이 필요하시면 미리 감사드립니다. – eLL