2015-11-09 36 views
0

我玩JavaFX和在開課(第一階段theStage)我有以下代碼:的JavaFX的drawImage旋轉

/*... Scene, stage, canvas ...*/ 
GraphicsContext gc = canvas.getGraphicsContext2D(); 
Image imgage = new Image("gfx/image.png"); 
gc.drawImage(image, 256, 256); 

我怎麼畫它旋轉而不旋轉軸?我發現並試圖此:

gc.save(); // saves the current state on stack, including the current transform 
gc.rotate(45); 
gc.drawImage(image); 
gc.restore(); 

但是它也轉換軸,當我使用改變位置x & Y運動圖像..它出了問題,因爲它使軸旋轉了。我一直在考慮使用sin和cos函數重新計算運動,但我確信有這樣一個更簡單的解決方案。

它可能適用於BufferedImage,但它們通過使用不同的Graphics類來繪製它,我不知道如何使它一起工作。

感謝很多:))

編輯:好,怎麼不只是在roatiting整個GraphicsContext旋轉圖像?

解決: 感謝所有幫助:))我已經使用這個解決它>>

ImageView iv = new ImageView(new Image("gfx/earth.png")); 
iv.setRotate(40); 
SnapshotParameters params = new SnapshotParameters(); 
params.setFill(Color.TRANSPARENT); 
Image rotatedImage = iv.snapshot(params, null); 
gc.drawImage(rotatedImage, 0, 0); 
+0

你爲什麼要在Canvas上繪製圖像?你可以使用「ImageView」嗎? – hotzst

+0

我真的不明白什麼「旋轉軸」意味着你的問題。什麼軸?它是否顯示了某個軸的可見性,而您在其他未提供的代碼中顯示該軸? – jewelsea

+0

可能,這個問題是[如何在JavaFX Canvas上繪製圖像旋轉圖像?](http://stackoverflow.com/questions/18260421/how-to-draw-image-rotated-on-javafx-canvas)的副本,但我很難確定這一點。 – jewelsea

回答

1

讓我們來看看,如果我能理解你的問題......

如何旋轉圖像而不旋轉整個GraphicsContext?

你不能只使用GraphicsContext來做到這一點。您需要旋轉GraphicsContext以使用drawImage API將旋轉後的圖像呈現給它。爲了防止旋轉操作影響其他Canvas繪圖操作,您可以在執行旋轉之前和之後保存和恢復GraphicsContext(因爲它似乎已經從您的問題中的代碼執行)。

但是,在不旋轉GraphicsContext的情況下完成所需操作的一種方法是將SceneGraph與Canvas結合使用。將圖像置於ImageView中,對圖像應用旋轉變換,對其進行快照以獲取旋轉的圖像,然後將旋轉的圖像繪製到Canvas中。

ImageView iv = new ImageView(image); 
iv.setRotate(40); 
SnapshotParameters params = new SnapshotParameters(); 
params.setFill(Color.TRANSPARENT); 
Image rotatedImage = iv.snapshot(params, null); 
gc.drawImage(rotatedImage, 0, 0); 

示例代碼:

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.SnapshotParameters; 
import javafx.scene.canvas.*; 
import javafx.scene.image.*; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

/** Rotates and places it in a canvas */ 
public class RotatedImageInCanvas extends Application { 
    @Override public void start(Stage stage) { 
     Image image = new Image(
      "http://worldpress.org/images/maps/world_600w.jpg", 350, 0, true, true 
     ); 

     // creates a canvas on which rotated images are rendered. 
     Canvas canvas = new Canvas(600, 400); 
     GraphicsContext gc = canvas.getGraphicsContext2D(); 

     ImageView iv = new ImageView(image); 
     iv.setRotate(40); 
     SnapshotParameters params = new SnapshotParameters(); 
     params.setFill(Color.TRANSPARENT); 
     Image rotatedImage = iv.snapshot(params, null); 
     gc.drawImage(rotatedImage, 0, 0); 

     // supplies a tiled background image on which the canvas is drawn. 
     StackPane stack = new StackPane(); 
     stack.setMaxSize(canvas.getWidth(), canvas.getHeight()); 
     stack.setStyle("-fx-background-image: url('http://1.bp.blogspot.com/_wV5JMD1OISg/TDYTYxuxR4I/AAAAAAAAvSo/a0zT8nwPV8U/s400/louis-vuitton-nice-beautiful.jpg');"); 
     stack.getChildren().add(
       canvas 
     ); 

     // places a resizable padded frame around the canvas. 
     StackPane frame = new StackPane(); 
     frame.setPadding(new Insets(20)); 
     frame.getChildren().add(stack); 

     stage.setScene(new Scene(frame, Color.BURLYWOOD)); 
     stage.show(); 
    } 

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

通常情況下,我不建議混合場景圖和帆布API,作爲上述樣品中完成的,而只是代碼只有場景圖形API或僅畫布API。

對於畫布的樣品唯一的解決辦法,請參閱答案:

在一般情況下,對於大多數操作,我發現與場景圖形API工作比編碼更簡單Canvas API並推薦使用場景圖形API而不是Canvas API來完成大多數任務。

+0

是的!這部分與我正在尋找快照參數..謝謝:)) –

1

根據另一條評論,你應該添加setViewport和setTransform,就像我在我的rotateImage函數中做的那樣,如果你想圍繞一個點旋轉圖像而不想改變圖像的中心。

public Image rotateImage(Image image, int rotation) { 
    ImageView iv = new ImageView(image); 
    SnapshotParameters params = new SnapshotParameters(); 
    params.setFill(Color.TRANSPARENT); 
    params.setTransform(new Rotate(rotation, image.getHeight()/2, image.getWidth()/2)); 
    params.setViewport(new Rectangle2D(0, 0, image.getHeight(), image.getWidth())); 
    return iv.snapshot(params, null); 
}