[英]Javafx and repositioning an image upon window resize?
好的,所以我試圖熟悉向對象添加偵聽器的過程,並認為我將嘗試修改我的一個應用程序,以便在調整窗格大小之后像在示例中顯示的那樣對圖像進行更新。 ..僅在此項目中,當我嘗試執行相同的操作時,出現此異常:
Exception in Application constructor
Exception in thread "main" java.lang.NoSuchMethodException: MouseEvent.MouseEventDemo.main([Ljava.lang.String;)
at java.lang.Class.getMethod(Class.java:1773)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:119)
我使用的代碼在這里:
// Add a listener to relocate object upon pane resize
pane.widthProperty().addListener(ov -> {
circularText.relocate(pane.getWidth() / 2, pane.getHeight() / 2);
imageView2.relocate(pane.getWidth() / 2, pane.getHeight() / 2);
});
pane.heightProperty().addListener(ov -> {
circularText.relocate(pane.getWidth() / 2, pane.getHeight() / 2);
imageView2.relocate(pane.getWidth() / 2, pane.getHeight() / 2);
});
如果需要整個主要代碼的一部分,可以在下面找到它:
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.effect.BlendMode;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
* Created by John on 7/17/2014.
*/
class MouseEventDemo extends Application {
private double rotateImage;
private final Pane pane = new Pane();
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Set pane's properties
pane.setStyle("-fx-background-color: black");
// Create and add objects
setMouseTrail();
setCenterObject();
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 600, 600);
primaryStage.setTitle("MouseTrailDemo"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/** Set mouse trail */
void setMouseTrail() {
// Create image for cursor
ImageView imageView = new ImageView(new Image("/image/anon5.jpg"));
imageView.setBlendMode(BlendMode.SCREEN);
imageView.setCache(true);
pane.getChildren().add(imageView);
// Create a new RotatingText object
Font font = Font.font("Courier New", FontWeight.BOLD, 12);
RotatingText circlePane = new RotatingText("We Are Anonymous",
220, font);
circlePane.setCircleSize(36);
circlePane.setTextColor(Color.LIME);
circlePane.relocate(imageView.getX() + 34,
imageView.getY() + 29.55);
pane.getChildren().add(circlePane);
// Rotation transform for animation counter clockwise
final Rotate rotationTransform = new Rotate(360, 0, 0);
circlePane.getTransforms().add(rotationTransform);
// Animate the RotatingText object
Timeline rotatingText = new Timeline(new KeyFrame(
Duration.seconds(5),
new KeyValue(rotationTransform.angleProperty(), 0)));
rotatingText.setCycleCount(Timeline.INDEFINITE);
rotatingText.play();
// Set text to circle image and image to follow mouse
pane.setOnMouseMoved(e -> {
imageView.setX(e.getX() - 10);
imageView.setY(e.getY() + 30);
circlePane.relocate(imageView.getX() + 34,
imageView.getY() + 29.55);
});
}
/** Set center object and animate it */
void setCenterObject() {
// Create circular text
Font font2 = Font.font("Courier New", FontWeight.BOLD, 12);
CircularText circularText = new CircularText("We Are Anonymous",
325, 325);
circularText.setTextFill(Color.LIME);
circularText.setTextSize(12, 1.3, 1.1);
circularText.setFont(font2);
circularText.setTextRotate(90, 360);
circularText.setGapSpacing(0.95);
circularText.setTextStartDegree(360);
circularText.relocate(300, 300);
pane.getChildren().add(circularText);
// Rotation transform for circular text animation and image
final Rotate rotationTransform = new Rotate(360, 0, 0);
circularText.getTransforms().add(rotationTransform);
// Animate the RotatingText object
Timeline rotatingText = new Timeline(new KeyFrame(
Duration.seconds(10),
new KeyValue(rotationTransform.angleProperty(), 0)));
rotatingText.setCycleCount(Timeline.INDEFINITE);
rotatingText.play();
// Load image for center
ImageView imageView2 = new ImageView(new Image("/image/anon3.jpg"));
imageView2.setBlendMode(BlendMode.SCREEN);
imageView2.setCache(true);
imageView2.setX(circularText.getLayoutX() - 145);
imageView2.setY(circularText.getLayoutY() - 125);
pane.getChildren().add(imageView2);
// Create EventHandler for image
rotateImage = 0;
EventHandler<ActionEvent> eventHandler = e -> {
if (rotateImage == 360)
rotateImage = 0;
rotateImage += 1;
imageView2.setRotate(rotateImage);
};
// Animate the image rotation
Timeline timeline = new Timeline(new KeyFrame(
Duration.millis(35), eventHandler));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
// Add a listener to relocate object upon pane resize
pane.widthProperty().addListener(ov -> {
circularText.relocate(pane.getWidth() / 2, pane.getHeight() / 2);
imageView2.relocate(pane.getWidth() / 2, pane.getHeight() / 2);
});
pane.heightProperty().addListener(ov -> {
circularText.relocate(pane.getWidth() / 2, pane.getHeight() / 2);
imageView2.relocate(pane.getWidth() / 2, pane.getHeight() / 2);
});
}
}
好的,我是個白痴,顯然不是因為我添加了偵聽器,而是因為我創建了一個新項目來放置文件,並且選擇了錯誤的XD類型
同樣,我最后遇到了一個問題,因為打開了最后一個項目,因為它安裝了avast! 並運行我的第一個快速掃描,病毒掃描程序將我的計算機鎖定了,導致我強制關機並導致.idea中的workspace.xml文件損壞了……刪除它並重新打開該項目並重新運行該程序似乎修理它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.