leendert/src/main/java/nl/minkema/leendert/Menu.java

120 lines
3.8 KiB
Java

package nl.minkema.leendert;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.text.TextBoundsType;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Random;
/**
* @author J0QUE
* @version 1.0
*/
public class Menu extends Group {
public static ArrayList<Paint> playerColors = new ArrayList<>();
public Menu() throws URISyntaxException, MalformedURLException {
while(playerColors.size()<21) {
Color c = getRandomColor();
if (!playerColors.contains(c)) {
playerColors.add(c);
}
}
ArrayList<Node> nodes = new ArrayList<>();
Image bg = new Image("https://s-media-cache-ak0.pinimg.com/originals/56/b2/3e/56b23e286dc03db415db7af75d15d58a.jpg");
ImageView view = new ImageView(bg);
System.out.println(bg.getHeight());
view.setX(0);
view.setY(0);
view.setFitHeight(900);
view.setPreserveRatio(true);
String loc = this.getClass().getResource("/music/menu.mp4").toString();
//loc = new File("menu.mp4").toURI().toURL().toString();
System.out.println(loc);
Media music = new Media(loc);
MediaPlayer menuplayer = new MediaPlayer(music);
menuplayer.setCycleCount(Integer.MAX_VALUE);
menuplayer.setVolume(0.6f);
menuplayer.setAutoPlay(true);
menuplayer.play();
menuplayer.play();
nodes.add(view);
nodes.add(createButton("2 players", 100, 100, event -> {
System.out.println("2 players");
}));
nodes.add(createButton("3 players", 100, 150, event -> System.out.println("3 players")));
getChildren().addAll(nodes);
playerColors.forEach(System.out::println);
}
public static StackPane createButton(String string, double x, double y, EventHandler<MouseEvent> onClick) {
Text text = createText(string);
Rectangle rectangle = rectangulate(text);
StackPane pane = new StackPane(rectangle, text);
pane.setOnMouseClicked(onClick);
pane.setLayoutX(x);
pane.setLayoutY(y);
return pane;
}
private static Text createText(String string) {
Text text = new Text(string);
text.setBoundsType(TextBoundsType.VISUAL);
text.setStyle(
"-fx-font-family: \"Arial\";" +
//"-fx-font-style: normal;" +
"-fx-font-size: 24px;"
);
return text;
}
private static Rectangle rectangulate(Text text) {
Rectangle rectangle = new Rectangle();
rectangle.setFill(getRandomColor());
final double PADDING = 25;
rectangle.setWidth(getWidth(text) + PADDING);
rectangle.setHeight(getHeight(text) + PADDING);
rectangle.setArcHeight(20);
rectangle.setArcWidth(20);
return rectangle;
}
private static double getWidth(Text text) {
new Scene(new Group(text));
text.applyCss();
return text.getLayoutBounds().getWidth();
}
private static double getHeight(Text text) {
new Scene(new Group(text));
text.applyCss();
return text.getLayoutBounds().getHeight();
}
public static Color getRandomColor() {
Random random = new Random(System.currentTimeMillis());
return Color.hsb(random.nextInt(360), 0.8, 0.8);
}
}