1

我在谷歌搜索了几个小时,但我仍然找不到正确的答案,所以我有最后一次机会来这里询问。

我正在制作学年 JAVA FX 项目。我正在使用 NetBeans。

我有一点可以在我拥有的应用程序上看到。问题是:我想要一张大地图(背景),我需要能够随着我的视线移动。例如向右移动 50 (x)。

我有使用 Stage、Scene、StackPane 的应用程序。

我听说过一些关于 Java 中的Dimensions,但我不能在javafx 应用程序中使用它。有没有类似的东西,我可以在我的应用程序中使用什么?

非常感谢。

4

1 回答 1

5

我认为您要求的是在背景中Scene带有地图(表示为Image)和分层在地图顶部的控件以允许在某些位置与地图进行交互。你的问题有点不清楚,所以我不确定这是否是你要问的。

如果是这样,这里有一些示例代码来实现它。

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

/** Constructs a scene with a pannable Map background. */
public class PannableView extends Application {
  private Image backgroundImage;
  
  @Override public void init() {
    backgroundImage = new Image("https://www.narniaweb.com/wp-content/uploads/2009/08/NarniaMap.jpg");
  }
  
  @Override public void start(Stage stage) {
    stage.setTitle("Drag the mouse to pan the map");
    
    // construct the scene contents over a stacked background.
    StackPane layout = new StackPane();
    layout.getChildren().setAll(
      new ImageView(backgroundImage),
      createKillButton()
    );

    // wrap the scene contents in a pannable scroll pane.
    ScrollPane scroll = createScrollPane(layout);
    
    // show the scene.
    Scene scene = new Scene(scroll);
    stage.setScene(scene);
    stage.show();

    // bind the preferred size of the scroll area to the size of the scene.
    scroll.prefWidthProperty().bind(scene.widthProperty());
    scroll.prefHeightProperty().bind(scene.widthProperty());
    
    // center the scroll contents.
    scroll.setHvalue(scroll.getHmin() + (scroll.getHmax() - scroll.getHmin()) / 2);
    scroll.setVvalue(scroll.getVmin() + (scroll.getVmax() - scroll.getVmin()) / 2);
  }
  
  /** @return a control to place on the scene. */
  private Button createKillButton() {
    final Button killButton = new Button("Kill the evil witch");
    killButton.setStyle("-fx-base: firebrick;");
    killButton.setTranslateX(65);
    killButton.setTranslateY(-130);
    killButton.setOnAction(new EventHandler<ActionEvent>() {
      @Override public void handle(ActionEvent t) {
        killButton.setStyle("-fx-base: forestgreen;");
        killButton.setText("Ding-Dong! The Witch is Dead");
      }
    });
    return killButton;
  }

  /** @return a ScrollPane which scrolls the layout. */
  private ScrollPane createScrollPane(Pane layout) {
    ScrollPane scroll = new ScrollPane();
    scroll.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
    scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
    scroll.setPannable(true);
    scroll.setPrefSize(800, 600);
    scroll.setContent(layout);
    return scroll;
  }
  
  public static void main(String[] args) { launch(args); }  
}

可平移

例如,使用鼠标(或者可能是触摸命令或触控板滚动手势——尽管我没有触摸屏或触控板来测试它)来拖动地图。点击按钮“杀死邪恶的女巫”。

该解决方案通过以下方式起作用:

  1. 创建一个ImageView来保存背景图。
  2. StackPane在堆叠的背景中构建场景内容ImageView
  3. 将场景包裹在场景ScrollPane大小的范围内。
  4. 在 上设置属性ScrollPane以使其可平移。
于 2013-02-25T19:24:32.970 回答