package example3_3; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import java.util.Random; // 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 GameFrame() { super("Game Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Squares squares = new Squares(); getContentPane().add(squares); for (int i = 0; i < 15; i++) { squares.addSquare(i * 10, i * 10, 100, 100); } setLocationRelativeTo(null); pack(); // Shouldn't do any more object-changing stuff after this command setVisible(true); // or maybe this one. } public static void main(String[] args) { new GameFrame(); } } // from // http://stackoverflow.com/questions/9713432/drawing-simple-rectangles-on-a-jframe-in-java class Squares extends JPanel { private static final int PREFERRED_W = 500; private static final int PREFERRED_H = PREFERRED_W; private static int MAX_JIGGLE_DIST = 3; // measured in pixels private List squares = new ArrayList(); private Random generator = new Random(); private JButton button = new JButton("Please push me!"); private JTextArea text = new JTextArea("Count: 0"); public Squares() { initButton(); initTimer1(); initTimer2(); } private void initTimer2() { 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); } private void initTimer1() { int delayMS1 = 17; // roughly 60 fps. ActionListener timer1Listener = new ActionListener() { int count = 0; @Override public void actionPerformed(ActionEvent e) { count++; for(Rectangle square : squares) { Point loc = square.getLocation(); // Although getX and getY return doubles, they are stored as ints internally // so it's probably best just to cast them back to ints promptly. int newX = (int)loc.getX() + generator.nextInt(2*MAX_JIGGLE_DIST+1)-MAX_JIGGLE_DIST; int newY = (int)loc.getY() + generator.nextInt(2*MAX_JIGGLE_DIST+1)-MAX_JIGGLE_DIST; square.setLocation(new Point(newX, newY)); } repaint(); } }; Timer timer1 = new Timer(delayMS1,timer1Listener); timer1.start(); } private void initButton() { ActionListener buttonListener = new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { button.setText("You pushed me!"); } }; button.addActionListener(buttonListener); add(button); } public void addSquare(int x, int y, int width, int height) { Rectangle rect = new Rectangle(x, y, width, height); squares.add(rect); } @Override public Dimension getPreferredSize() { return new Dimension(PREFERRED_W, PREFERRED_H); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; for (Rectangle rect : squares) { g2.draw(rect); } } }