/** * Copied from example5_1_Squares */ package start6_1.squares; import javax.swing.*; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; // Some code copied from // http://stackoverflow.com/questions/9713432/drawing-simple-rectangles-on-a-jframe-in-java // Some code copied from an example with this comment-block /* * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. * * 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 or 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. */ // From // http://stackoverflow.com/questions/9713432/drawing-simple-rectangles-on-a-jframe-in-java public class GameFrame extends JFrame { public static void main(String[] args) { new GameFrame(); } private GameFrame() { super("Game Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initButton(); initTimer2(); setUpSquares(); } private void setUpSquares() { final int HEIGHT = 512; final int WIDTH = 512; // final int SQRT_NUM_SQUARES = 10; // Initial demo works // final int SQRT_NUM_SQUARES = HEIGHT; // Pushing constructor to InvokeLater works // But repainting doesn't happen very well. final int SQRT_NUM_SQUARES = 3*HEIGHT; // TODO: Make not sluggish Squares squares = new Squares(WIDTH, HEIGHT); for (int indCol = 0; indCol < SQRT_NUM_SQUARES; indCol++) { for (int indRow = 0; indRow < SQRT_NUM_SQUARES; indRow++) { squares.addSquare((indCol * WIDTH)/ SQRT_NUM_SQUARES, (indRow * HEIGHT)/ SQRT_NUM_SQUARES, 5, 5); } } getContentPane().add(squares); setLayout(new FlowLayout()); // setLocationRelativeTo(null); // Moves the window to the center of the screen (but doesn't work perfectly for me.) // TODO: Figure out how to make pack work correctly. It seems to glitch and push one component slightly off the frame. setSize(new Dimension(WIDTH+50,HEIGHT+100)); // pack(); // Shouldn't do any more object-changing stuff after this command setVisible(true); // or this one? } private void initButton() { final JButton button = new JButton("Push me!"); ActionListener buttonListener = new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { button.setText("You pushed me!"); } }; button.addActionListener(buttonListener); add(button); } private void initTimer2() { final JLabel text = new JLabel(); int delayMS2 = 1000; ActionListener timerListener2 = new ActionListener() { int count = 0; @Override public void actionPerformed(ActionEvent e) { count++; text.setText("Count: " + count); } }; Timer timer2 = new Timer(delayMS2,timerListener2); timer2.start(); add(text); } }