2014-08-29 7 views
2

다음 코드를 사용하여 사인파를 생성합니다. SineWave
이 애니메이션의 속도는 매우 느립니다. 코드 : 이제Java - 플롯 사인파

public static final double SINE_TO_180 = 114.58865012930961, TIMES = 180, SINE_OF_90 = Math.sin(Math.toRadians(90)); 

public static void main(String[] args) throws Exception{ 
    float velc = 200; 
    float angle = 45; 
    float resistance = 0f; 
    double multiple = (velc * 2.5/SINE_TO_180); 
    int offset = 0; 
    double y = 0; 
    double x = 0; 
    double h = 0; 
    double cos = Math.cos(Math.toRadians(angle)); 
    double sin = Math.sin(Math.toRadians(angle)); 
    for(int i = offset; i < TIMES + 1 + offset; i ++){ 
     y += ((Math.sin(Math.toRadians(i * 2)))) * multiple * sin; 
     if(y >= h) 
      h = y; 
     x += Math.sin(Math.toRadians(i)) * multiple * ((1 - resistance) * 1.5) * Math.abs(cos); 
//   x += multiple * cos; 
//   if(i + offset < TIMES/2){ 
//    x += Math.sin(Math.toRadians(i)) * multiple * ((1 - resistance) * 1.5); 
//   }else{ 
//    x += Math.sin(Math.toRadians(TIMES/2)) * multiple * (1 - resistance); 
//   } 
    } 
    y = Math.round(y); 
    //do round x? 
    x = Math.round(x); 
    System.out.println("X: " + x); 
    JFrame frm = new JFrame("Projectile!"); 
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    BufferedImage b = new BufferedImage((int)x + 1, (int)h + 1, BufferedImage.TYPE_INT_RGB); 
    Graphics g = b.getGraphics(); 
    y = 0; 
    x = 0; 

    JLabel l = new JLabel(new ImageIcon(b)); 
    frm.add(l); 
    frm.pack(); 
    frm.setVisible(true); 

    for(int i = offset; i < TIMES + 1 + offset; i ++){ 
     y += ((Math.sin(Math.toRadians(i * 2)))) * multiple * sin; 

     x += Math.sin(Math.toRadians(i)) * multiple * ((1 - resistance) * 1.5) * Math.abs(cos); 
//   x += multiple * cos; 
//   if(i + offset < TIMES/2){ 
//    x += Math.sin(Math.toRadians(i)) * multiple * ((1 - resistance) * 1.5); 
//   }else{ 
//    x += Math.sin(Math.toRadians(TIMES/2)) * multiple * (1 - resistance); 
//   } 
     g.setColor(Color.red); 
     g.drawLine((int)x, (int)(h - y), (int)x, (int)(h - y)); 
     l.setIcon(new ImageIcon(b)); 
     frm.repaint(); 
     Thread.sleep((int)(1000.0/24.0)); 
    } 
    ImageIO.write(b, "png", new File("C:\\proj.png")); 
} 

내가 사인 애니메이션을 변경하려면이 빠른 - 느린 - 빠른을하는 그것의 첨단에 느린 어디 다음과 같은 결과를 시도하고이 있어요 그래서 :
SineWave2
나는 그것을 기대를 애니메이션 속도가 다를뿐입니다. 코드 :

public static void main(String[] args) throws Exception{ 
    float velc = 200; 
    float angle = 45; 
    float resistance = 0f; 
    double multiple = (velc * 2.5/SINE_TO_180); 
    int offset = 0; 
    double y = 0; 
    double x = 0; 
    double h = 0; 
    double cos = Math.cos(Math.toRadians(angle)); 
    double sin = Math.sin(Math.toRadians(angle)); 
    for(int i = offset; i < TIMES + 1 + offset; i ++){ 
     y += (1 - Math.sin(Math.toRadians(i * 2))) * multiple * sin; 
     if(y >= h) 
      h = y; 
//   x += Math.sin(Math.toRadians(i)) * multiple * ((1 - resistance) * 1.5) * Math.abs(cos); 
     x += 2; 
     //   x += multiple * cos; 
//   if(i + offset < TIMES/2){ 
//    x += Math.sin(Math.toRadians(i)) * multiple * ((1 - resistance) * 1.5); 
//   }else{ 
//    x += Math.sin(Math.toRadians(TIMES/2)) * multiple * (1 - resistance); 
//   } 
    } 
    y = Math.round(y); 
    //do round x? 
    x = Math.round(x); 
    System.out.println("X: " + x); 
    JFrame frm = new JFrame("Projectile!"); 
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    BufferedImage b = new BufferedImage((int)x + 1, (int)h + 1, BufferedImage.TYPE_INT_RGB); 
    Graphics g = b.getGraphics(); 
    y = 0; 
    x = 0; 

    JLabel l = new JLabel(new ImageIcon(b)); 
    frm.add(l); 
    frm.pack(); 
    frm.setVisible(true); 

    for(int i = offset; i < TIMES + 1 + offset; i ++){ 
     y += (1 - Math.sin(Math.toRadians(i * 2))) * multiple * sin; 
     x += 2; 
//   x += Math.sin(Math.toRadians(i)) * multiple * ((1 - resistance) * 1.5) * Math.abs(cos); 
//   x += multiple * cos; 
//   if(i + offset < TIMES/2){ 
//    x += Math.sin(Math.toRadians(i)) * multiple * ((1 - resistance) * 1.5); 
//   }else{ 
//    x += Math.sin(Math.toRadians(TIMES/2)) * multiple * (1 - resistance); 
//   } 
     g.setColor(Color.red); 
     g.drawLine((int)x, (int)(h - y), (int)x, (int)(h - y)); 
     l.setIcon(new ImageIcon(b)); 
     frm.repaint(); 
     Thread.sleep((int)(1000.0/24.0)); 
    } 
    ImageIO.write(b, "png", new File("C:\\proj2.png")); 
} 

누구나 내가 처음에 다른 애니메이션 속도와 동일한 결과를 기대하기 때문에 내가 뭘 잘못하고 있는지 알 수 있습니까?

+0

'애니메이션 속도'의 의미를 설명 할 수 있습니까? 그것은 그려지는 두 점 사이의 시간입니까? 그렇다면, 나는 너를 바로 잡아야한다 : 점을 그리는 시간은 항상 같은 시간이다! 하지만 두 지점 사이의 거리가 바뀌고 있습니다! 예 2에서와 같이 예를 들어, 예 1에서는 앞쪽으로 픽셀의 양을 다른 방향으로 앞으로 이동합니다 (처음에는 거의 표시되지 않았고, 가운데는 더 많이, 끝에는 거의 표시되지 않았습니다). –

답변

3

부드러운 애니메이션을 만들고 싶다면 데이터와 애니메이션을 분리해야합니다.

먼저 데이터를 생성 - 단순히 데이터

그래서
private int endOfX = 100; //adjust as you wish 
private int[] data; 
public void calculateData(){ 
    data = new int[amendOfX ountPixels]; 
    for(int x = 0; x < endOfX ; x++){ 
     y = x*x; //this is an example, use your mathematical function here 
    } 
} 

위한 배열을 사용할 수 있도록, -는 [> Y F (X)를 의미한다] - A (수학) 함수 이제 쉽게 사용 이 지금 무엇을, 당신이 응용 프로그램의 시작이 될 것입니다 -이 데이터는 부드러운 애니메이션을 너무

public class AnimationOfFunction(){ 
    public static void main(String[] args){ 
     new AnimationOfFunktion().createAndShowGui(); //as from java tutorials 
    } 

    private BufferedImage img; 
    private Graphics gr; 
    private void createAndShowGui(){ 
     calculateData(); first of all we create the data! 
     JFrame frame = new JFrame();//then create your frame here 
     JPanel panel = createContent(); //create your drawing panel here 
     frame.add(panel);//adding drawing panel 
     frame.pack(); //setting the proper size of frame 
     frame.setVisible(true); //show frame   
     startAnimation(panel); //this is important - after showing the frame you start your animation here! 
    } 
} 

를 제공하기 위해?

private void startAnimation(final JPanel panel){ 
    //create a anonym class 
    Runnable r = new Runnable(){ 
     private int py = 0; //previous values 
     private int py = 0; //previous values 
     @overrdie 
     public void run(){ 
      for(int x = 0; x < endOfX ; x++){ 
       int y = data[x]; 
       //now we have x and y, so you can plot your function; 
       gr.drawLine(px, py, x,y); //you can scale here 
       int sleeptime = calculateSleepTime(px,py, x,y); 
       Thread.sleep(sleeptime); 

       //set the previouse values; 
       px = x; 
       py = y; 

       //important - repaint your panel to create an 'animation' 
       panel.repaint(); 

      } 
     } 
    } 

    //having that runnable we must start that runnable within an thread 
    Thread thread = new Thread(r); 
    thread.setDaemon(true); 
    thread.start(); 
} 

그렇게 왼쪽 뭐죠 : 당신이 애니메이션을 시작하는 한 -

private JPanel createContent(){ 
    //create a anonym class 
    @surpress serial 
    JPanel panel = new JPanel(){ 

     @override 
     public void paintComponent(Graphics gr){ 
      super.paintComponent(gr); 
      gr.drawImage(img, 0,0, null); 
     } 

    } 
    panel.setPreferredSize(new Dimension(img.getWidth(), img.getHeight())); 
    return panel; 
} 

가장 중요한 : 먼저 적절한 도면 패널을 만들? 우리는 수면 시간을 계산해야합니다 : 두 점 사이의 거리가 우리가 거리가 짧은 경우, 우리는 덜 자고, 더 이상 잠 '큰'경우 ...

public int calculateSleeptime(int px, int py, int x, int y){ 
    int distance = (y-py)*(y-py)+(x-px)*(x-px); 
    distance = (int)(Math.sqrt(distance); 
    int sleepTime = distance*100; //play with this value! 
    return sleeptime; 
} 

내가 내 머리에서 그 코드를 작성했습니다 , 어떤 철자 오류 또는 컴파일 오류가 포함되어 있는지 확인하려면 IDE가 없었어요, 제발 자신을 위해 그것을 할뿐만 아니라, 나는 BufferedImage img ang Graphics gr을 시작하지 않았다. 하지만 분명히 이미 그렇게 할 수 있습니다!

+0

코드를 따랐습니다 그러나 그것은 같은 것을 끝내었다! 그것은 두 번째 예와 같이 이상한 선을 가지고있었습니다. – TameHog

+0

어쩌면 당신은 당신이 원하는 그래프를 설명 할 수 있습니까? 내 코드 샘플을 쉽게 조정하여 기능을 표시 할 수 있습니다 .... 어떻게 보이는지 알려주세요. (사진을 추가할까요?) –