So, to start, I simply wanted to get a window to popup with a Tic Tac Toe grid in it.
Creating the Frame
Having previous Swing experience, I'm no stranger to getting a JFrame up and running really quickly. However, to get the frame centered used to be a bit more complicated than it is now. Since Java 1.4, there's a new JFrame method "setLocationRelativeTo" which takes another component, or null. If the given component is null, the frame is centered in regards to the screen.
This makes creating a window very easy now.
Previously
// pack the frame, so the gamePanel and frame get their sizes. gameFrame.pack(); Dimension screenSize = toolkit.getScreenSize(); Dimension windowSize = gameFrame.getSize(); int winX = (screenSize.width - windowSize.width) / 2; int winY = (screenSize.height - windowSize.height) / 2; gameFrame.setLocation(winX, winY);
Now
// pack the frame, so the gamePanel and frame get their sizes. gameFrame.pack(); // center the frame to the screen. gameFrame.setLocationRelativeTo(null);
Creating the Grid
With Scenegraph, creating the Tic Tac Toe grid is also very easy. To create the grid with as little components as possible, I'm going to use the background color of the JSGPanel to create the lines, and then create 9 white boxes with space between them.
Creating the JSGPanel
// Build our Game Panel. JSGPanel gamePanel = new JSGPanel(); gamePanel.setPreferredSize(new Dimension(400, 400)); gamePanel.setBackground(Color.black);
Adding it to the Frame
// add the game panel to the frame. gameFrame.setContentPane(gamePanel);
Creating the Scene
The 9 boxes need to be added to a SGGroup node, which will act as our "scene".
// The root group node. SGGroup grid = new SGGroup(); // set the scene in our game panel. gamePanel.setScene(grid);
Creating the 9 boxes
// create the grid cells. for (int row = 0; row < 3; row++) { for (int column = 0; column < 3; column++) { SGShape gridCell = new SGShape(); Point cellLocation = new Point(column * (gridCellSize.width + 2), row * (gridCellSize.height + 2)); Rectangle shape = new Rectangle(cellLocation, gridCellSize); gridCell.setFillPaint(Color.white); gridCell.setShape(shape); grid.add(gridCell); } }
Final
As you can see creating the grid is quite simple. Here is the whole file, including the license and window code.
/* * Copyright (c) 2008 Eric Berry <elberry@gmail.com> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.townsfolkdesigns.games.tictactoe1; import com.sun.scenario.scenegraph.JSGPanel; import com.sun.scenario.scenegraph.SGGroup; import com.sun.scenario.scenegraph.SGShape; import java.awt.Color; import java.awt.Dimension; import java.awt.Point; import java.awt.Rectangle; import javax.swing.JFrame; /** * * @author eberry */ public class TicTacToe { public static void main(String[] args) { // Build our Game Panel. JSGPanel gamePanel = new JSGPanel(); gamePanel.setPreferredSize(new Dimension(400, 400)); gamePanel.setBackground(Color.black); // create the Frame JFrame gameFrame = new JFrame("Tic Tac Toe"); gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // add the game panel to the frame. gameFrame.setContentPane(gamePanel); // pack the frame, so the gamePanel and frame get their sizes. gameFrame.pack(); // center the frame to the screen. gameFrame.setLocationRelativeTo(null); // create the Tic Tac Toe grid. createGrid(gamePanel); gameFrame.setVisible(true); } private static void createGrid(JSGPanel gamePanel) { Dimension gameSize = gamePanel.getSize(); // the grid cell size is equal to 1/3 the game size minus 1 pixel for padding. Dimension gridCellSize = new Dimension((gameSize.width / 3) - 1, (gameSize.height / 3) - 1); // The root group node. SGGroup grid = new SGGroup(); // set the scene in our game panel. gamePanel.setScene(grid); // create the grid cells. for (int row = 0; row < 3; row++) { for (int column = 0; column < 3; column++) { SGShape gridCell = new SGShape(); Point cellLocation = new Point(column * (gridCellSize.width + 2), row * (gridCellSize.height + 2)); Rectangle shape = new Rectangle(cellLocation, gridCellSize); gridCell.setFillPaint(Color.white); gridCell.setShape(shape); grid.add(gridCell); } } } }
As you can see, if you only count the code dealing with Scenegraph, it took less than 20 lines to create the Tic Tac Toe grid.