2017-10-02 4 views
0

로터/헤드가 회전하는 풍차를 프로그래밍하려고했습니다. 줄기 자체는 고정되어 캔바스 (javafx)에 배경으로 그려집니다. 로터/헤드 자체는 또 다른 이미지입니다. 이미지 자체를 회전하고 그래픽 컨텍스트에 그릴 수 있다고 생각했습니다. (작동하지 않았다) 그런 다음 여러 가지 시도 :JavaFX 캔버스 고정 된 각도로 이미지 회전 (튀지 않고)

a. 다른 캔버스에 이미지를 그려서 캔버스를 회전하고 해당 캔버스의 스냅 샷을 만듭니다. (작동하지 않음)

b. 이미지 뷰를 만들고 이미지 뷰를 회전하고 스냅 샷을 찍은 다음 그립니다 (작동하지 않음).

c. RotateTransition (작동하지 않음)을 만들려고했습니다. 이제 저는 b로 돌아갑니다 : 회전하는 이미지의 ImageView.

b 종류의 작품, 일종의! 그것은 어떻게 든 "튀어 오릅니다."그리고 의사는 ImageView.setRotate (..)가 이미지를 중심 주위로 회전시키기 때문에 왜 그런지 알지 못합니다. 이미지 자체의 높이와 길이가 같으므로 사각형처럼 튀어 나와야합니다 ..이 바운스가 멈추길 바랄뿐입니다 .. see here (SO didnt allow me to post a gif)

이 모든 시도는이 포럼에서 읽은 것입니다.

Source Code here 또는 텍스트로

:

package sample; 

import javafx.animation.AnimationTimer; 
import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Scene; 
import javafx.scene.SnapshotParameters; 
import javafx.scene.canvas.Canvas; 
import javafx.scene.canvas.GraphicsContext; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

import java.io.IOException; 


public class Main extends Application { 
    private Pane root; 
    private Canvas canvas; 
    private GraphicsContext gc; 
    SnapshotParameters params = new SnapshotParameters(); 

    private final Image stem = new Image(getClass().getResource("windrad.png").toExternalForm()); 
    private final ImageView wheel = new ImageView(new Image(getClass().getResource("rad.png").toExternalForm())); 

    private final int height = 720; 
    private final int width = 720; 
    private final double distanceWidth = 400.0/720.0; 
    private final double distanceHeight = 240.0/720.0; 
    private int degrees = 0; 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     params.setFill(Color.TRANSPARENT); 

     FXMLLoader fxmlLoader = new FXMLLoader(); 
     fxmlLoader.setLocation(getClass().getResource("sample.fxml")); 

     try { 
      root = fxmlLoader.load(); 
      canvas = (Canvas) fxmlLoader.getNamespace().get("canvas"); 
      canvas.setHeight(height); 
      canvas.setWidth(width); 
      primaryStage.setHeight(height); 
      primaryStage.setWidth(width); 
      gc = canvas.getGraphicsContext2D(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     primaryStage.setTitle("Hello World"); 
     primaryStage.setScene(new Scene(root)); 
     primaryStage.show(); 

     animation().start(); 
    } 

    private AnimationTimer animation() { 
     return new AnimationTimer() { 
      @Override 
      public void handle(long now) { 
       gc.clearRect(0,0, width, height); 
       gc.drawImage(stem, 0, 0); 
       degrees = degrees >= 360 ? 0 : ++degrees; 
       wheel.setRotate(degrees); 
       gc.drawImage(wheel.snapshot(params, null), width * distanceWidth - (int) wheel.getImage().getWidth()/2, height * distanceHeight - (int) wheel.getImage().getHeight()/2); 

      } 
     }; 
    } 
} 

좋아, 그래서 그것을 작동있어! @ James_D는 나를 많이 도왔다. 캔버스에 그림을 그리는 대신 모든 객체를 장면 그래프에 배치하고 거기로 옮겼습니다. 내 로터/머리가 이미지 뷰이므로 imageview.setRotate()를 사용할 수있었습니다. 캔버스의 사용으로 인해 발생한이 문제점 (이 스레드를 게시 한 문제점). 나는 아직도 버그 킹 버그가 어떻게 발생했는지는 모르지만 프로젝트 목표는 달성했다. 새로운 소스 코드가 답변에 포함됩니다.

+0

캔버스를 사용해야합니까? 장면 그래프에 노드를 배치하는 것이 더 쉬울 것입니다. –

+0

https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question을 참조하십시오. 질문을 편집하고 코드를 직접 포함하십시오. –

+0

@James_D 코드를 직접 포함시키는 방법은 무엇입니까? 텍스트 편집기에서 붙여 넣기 나 명령을 복사하기 만하면됩니까? 난 씬 그래프를 읽을거야! – Basti

답변

0

나는 캔버스에 그리는 대신 씬 그래프에 노드를 배치하는 것을 의미했습니다. 물론, 당신은 (물론) 씬 그래프에 캔버스를 배치하고 있습니다. 그러나 한 번 그려진 캔버스를 수정하는 것은 본질적으로 어렵습니다. - @James_D

이 의견은 도움을주고 제 문제를 해결했습니다. 새로운 소스 코드가 질문에 나열됩니다. 캔버스 위에 그리는 대신 모든 객체를 장면 그래프에 넣고 캔버스 나 이미지 대신 이미지 뷰를 이동했습니다. 새 소스 코드 Heres :

package sample; 

import javafx.animation.AnimationTimer; 
import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.stage.Stage; 

public class Main extends Application { 
    private Group root; 

    private final ImageView stem = new ImageView(new Image(getClass().getResource("windrad.png").toExternalForm())); 
    private final ImageView wheel = new ImageView(new Image(getClass().getResource("rad.png").toExternalForm())); 

    private final int height = 720; 
    private final int width = 720; 
    private final double distanceWidth = 360.0/720.0; 
    private final double distanceHeight = 270.0/720.0; 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     root = new Group(); 
     root.getChildren().add(stem); 
     root.getChildren().add(wheel); 
     wheel.setX(width * distanceWidth - wheel.getImage().getWidth()/2); 
     wheel.setY(height * distanceHeight - wheel.getImage().getHeight()/2); 
     primaryStage.setTitle("Pinwheel"); 
     primaryStage.setScene(new Scene(root)); 
     primaryStage.show(); 
     primaryStage.getIcons().add(new Image(getClass().getResource("windrad.png").toExternalForm())); 

     animation().start(); 
    } 

    private AnimationTimer animation() { 
     return new AnimationTimer() { 
      @Override 
      public void handle(long now) { 
       wheel.setRotate(wheel.getRotate() + 1); 
      } 
     }; 
    } 
} 
관련 문제