/* * Copyright (c) 2008, 2012 Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. * * This file is available and licensed under the following license: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * - Neither the name of Oracle Corporation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package brickbreaker; import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.embed.swing.JFXPanel; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Toolkit; import javax.swing.*; public class Main extends Application { private static MainFrame mainFrame; public Main() {} Main(boolean embedded) { Config.initialize(); JFrame frame = new JFrame("Brick Breaker in JFXPanel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); // Create javafx panel final JFXPanel fxPanel = new JFXPanel(); fxPanel.setPreferredSize(new Dimension(Config.SCREEN_WIDTH + 2*Config.WINDOW_BORDER, Config.SCREEN_HEIGHT+ 2*Config.WINDOW_BORDER + Config.TITLE_BAR_HEIGHT)); frame.getContentPane().add(fxPanel, BorderLayout.CENTER); // create JavaFX scene Platform.runLater(new Runnable() { public void run() { Group root = new Group(); mainFrame = new MainFrame(root); Scene scene = new Scene(root); scene.setFill(Color.BLACK); fxPanel.setScene(scene); mainFrame.changeState(MainFrame.SPLASH); } }); // show frame frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static MainFrame getMainFrame() { return mainFrame; } @Override public void start(Stage stage) { Config.initialize(); Group root = new Group(); mainFrame = new MainFrame(root); stage.setTitle("Brick Breaker"); stage.setResizable(false); stage.setWidth(Config.SCREEN_WIDTH + 2*Config.WINDOW_BORDER); stage.setHeight(Config.SCREEN_HEIGHT+ 2*Config.WINDOW_BORDER + Config.TITLE_BAR_HEIGHT); Scene scene = new Scene(root); scene.setFill(Color.BLACK); stage.setScene(scene); mainFrame.changeState(MainFrame.SPLASH); stage.show(); } public static void main(String[] args) { if (System.getProperty("embedded") != null) { SwingUtilities.invokeLater(new Runnable() { public void run() { new Main(true); } }); } else { Application.launch(args); } } public class MainFrame { // Instance of scene root node private Group root; // Instance of splash (if exists) private Splash splash; // Instance of level (if exists) private Level level; // Number of lifes private int lifeCount; // Current score private int score; private MainFrame(Group root) { this.root = root; } public int getState() { return state; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getLifeCount() { return lifeCount; } public void increaseLives() { lifeCount = Math.min(lifeCount + 1, Config.MAX_LIVES); } public void decreaseLives() { lifeCount--; } // Initializes game (lifes, scores etc) public void startGame() { lifeCount = 3; score = 0; changeState(1); } // Current state of the game. The next values are available // 0 - Splash public static final int SPLASH = 0; // 1..Level.LEVEL_COUNT - Level private int state = SPLASH; public void changeState(int newState) { this.state = newState; if (splash != null) { splash.stop(); } if (level != null) { level.stop(); } if (state < 1 || state > LevelData.getLevelsCount()) { root.getChildren().remove(level); level = null; splash = new Splash(); root.getChildren().add(splash); splash.start(); } else { root.getChildren().remove(splash); splash = null; level = new Level(state); root.getChildren().add(level); level.start(); } } } }