java - 如何将图像堆叠到 javafx 中的现有背景上

标签 java css javafx background-image

以下是我尝试过的方法。我尝试使用 imageView,将图像设置到它并将其添加到堆栈 Pane 。图像显得相当大,看起来不像我想要的主菜单的样子(当我最大化屏幕时背景颜色就在那里,但 logoIcon 最初占据了整个屏幕)。然后我决定摆脱它,使用样式类并将其添加到我的堆栈 Pane 中,但它似乎没有注册我添加到堆栈 Pane 中的样式类。由于这是一个简单的示例,您可以试用该程序并查看结果。

这里是相关代码

package whowantstobeamillionairetriviagame;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class WhoWantsToBeAMillionaireTriviaGame extends Application 
{   
@Override
public void start(Stage startingStage) throws Exception
{    
    Image backgroundColor = new Image("http://1.bp.blogspot.com/-p0s06MBIx_U/T8zKIBZ24pI/AAAAAAAAA7Y/n8hMZfpRic0/s1600/dark+blue+wallpaper+10.jpg");

    BackgroundSize backgroundSize = new BackgroundSize(100, 100, true, true, true, true);
    BackgroundImage backgroundImage = new BackgroundImage(backgroundColor, BackgroundRepeat.NO_REPEAT, 
    BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);

    StackPane background = new StackPane();
    background.setBackground(new Background(backgroundImage));
    background.getStyleClass().add("MillionaireLogo");

    Scene backgroundScene = new Scene(background);
    startingStage.setScene(backgroundScene);

    startingStage.show();
}

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

MillionaireLogo.css 文件

 .MillionaireLogo
{
  -fx-background-image: url("http://3.bp.blogspot.com/-ujgaiwveDzc/TZQeK9gZvPI/AAAAAAAAADY/ZNVAxlaqXoY/s1600/Millionaire%2BParody%2BLogo2%2Bcopy.jpg");
  -fx-background-repeat: no-repeat;
  -fx-background-position: top center;
  -fx-background-size: cover;
}

最佳答案

好吧,这是解决我的主要问题(将图像堆叠到背景)的解决方案

package whowantstobeamillionairetriviagame;

import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class WhoWantsToBeAMillionaireTriviaGame extends Application 
{   
@Override
public void start(Stage startingStage) throws Exception
{      
    StackPane backgroundSettings = new StackPane();

    Image backgroundColor = new Image("http://1.bp.blogspot.com/-p0s06MBIx_U/T8zKIBZ24pI/AAAAAAAAA7Y/n8hMZfpRic0/s1600/dark+blue+wallpaper+10.jpg");

    ImageView background = new ImageView();
    background.setImage(backgroundColor);

    Image millionaireLogo = new Image(new File("MillionaireLogo.PNG").toURI().toString());

    ImageView logoPicture = new ImageView();
    logoPicture.setImage(millionaireLogo);
    logoPicture.setPreserveRatio(true);
    logoPicture.setSmooth(true);
    logoPicture.setCache(true);
    StackPane.setAlignment(logoPicture, Pos.TOP_CENTER);

    backgroundSettings.getChildren().addAll(background, logoPicture);

    Scene backgroundScene = new Scene(backgroundSettings);
    startingStage.setScene(backgroundScene);

    startingStage.setTitle("Who Wants to be a Millionaire");
    startingStage.show();
}

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

我现在的新问题是如何缩小图像的大小(给定我的代码)并将其放置在屏幕的顶部中央?

关于java - 如何将图像堆叠到 javafx 中的现有背景上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29459369/

相关文章:

java - 修改传递给类的 Java 参数

java - 抛出哪个异常?

javascript - 通过单击 div 隐藏和显示文本

java - 保留按钮 :focus when changing Tabs in TabPane

java - 自定义 ControlsFX 通知

java - 使用 Java 运行时 SQLCMD 不工作

java - 使用 Servlet 注释进行映射不起作用

javascript - 检查悬停链接的 href 属性

html - 在 <audio> 中隐藏上下文菜单

Java e(fx)clipse build.build - 以编程方式获取版本号