Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bouncer;
- /* NOTE: requires MyImage.java */
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class BouncingImages extends JFrame implements ActionListener {
- public static void main(String[] args) {
- new BouncingImages();
- }
- /* These are the width and height of the DrawingPanel NOT the whole JFrame.
- Bouncing off walls will not work properly if it's the JFrame size */
- static final int WINW = 1000;
- static final int WINH = 800;
- JPanel buttonPanel = new JPanel();
- AnimationPanel animationPanel;
- ArrayList<MyImage> imageList = new ArrayList<MyImage>();
- AnimationThread thread;
- private volatile boolean stopRequested = false; //maybe should use AtomicBoolean
- BouncingImages() {
- //set up button panel
- JButton btnStart = new JButton("Start");
- JButton btnStop = new JButton("Stop");
- JButton btnExit = new JButton("Exit");
- btnStart.addActionListener(this);
- btnStop.addActionListener(this);
- btnExit.addActionListener(this);
- buttonPanel.add(btnStart);
- buttonPanel.add(btnStop);
- buttonPanel.add(btnExit);
- animationPanel = new AnimationPanel();
- //setup JFrame
- //this.setExtendedState(JFrame.MAXIMIZED_BOTH); //If we use this, then we need to determine the dimensions of the JPanel before we start drawing
- // this.setUndecorated(true);
- this.add(buttonPanel, BorderLayout.SOUTH);
- this.add(animationPanel);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.pack(); //since the JPanel is controlling the size, we need pack() here.
- this.setLocationRelativeTo(null); //after pack();
- this.setVisible(true);
- }
- void startAnimation() {
- //this doesn't restart the thread if it terminates
- if (thread == null) {
- thread = new AnimationThread ();
- stopRequested = false;
- thread.start();
- }
- //System.out.println("thread is " + thread.toString());
- //this will "restart" the thread!
- if (thread.getThreadGroup() == null) {
- thread = new AnimationThread ();
- stopRequested = false;
- thread.start();
- }
- }
- //two different versions here as I was testing how to stop threads properly
- void stopAnimation1(){
- stopRequested = true;
- }
- void stopAnimation2(){
- thread.interrupt();
- //System.out.println(thread.isInterrupted());
- }
- void addImage() {
- imageList.add(new MyImage("fish.jpeg"));
- }
- void moveAllImages() {
- for (MyImage image : imageList) {
- image.moveImage();
- }
- }
- //ActionListener for Buttons
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getActionCommand().equals("Start")) {
- addImage();
- startAnimation();
- }
- if (e.getActionCommand().equals("Stop")) {
- stopAnimation2();
- }
- if (e.getActionCommand().equals("Exit")) {
- System.exit(0);
- }
- }
- //This class does drawing graphics and nothing else
- class AnimationPanel extends JPanel {
- AnimationPanel() {
- this.setBackground(Color.BLACK);
- this.setPreferredSize(new Dimension(WINW, WINH));
- }
- @Override
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- for (MyImage im : imageList) {
- g.drawImage(im.image, im.x, im.y, im.width, im.height,null);
- }
- }
- }
- /* NOTE: I'm not that experienced with threads, preferring to use timers. A Swing Timer would work perfectly for this program
- * Quote: "Starting from Java 5 it is not a good idea to work with Threads directly.
- * You should rather use Executor framework and choose an execution policy depending on your requirements."
- */
- private class AnimationThread extends Thread {
- public void run() {
- run2B(); //here we can test different methods
- }
- //this version stops the thread using a flag and break. (use stopAnimation1())
- public void run1() {
- while (true) {
- //everything in the main game loop goes here
- moveAllImages();
- //collide_images()
- animationPanel.repaint();
- try {
- this.sleep(5);
- } catch (InterruptedException e) {}
- if(stopRequested) break;
- }
- }
- //this version stops the thread using an interrupt and break (use stopAnimation2())
- public void run2A() {
- while (true) {
- moveAllImages();
- //collide_images()
- animationPanel.repaint();
- try {
- this.sleep(5);
- } catch (InterruptedException e) {
- System.out.println("interrupted");
- break;
- }
- }
- }
- //this version stops the thread using interrupt only (use stopAnimation2())
- public void run2B() {
- while ( ! thread.isInterrupted() ) { // or //while (! Thread.currentThread().isInterrupted() ) {
- moveAllImages();
- //collide_images()
- animationPanel.repaint();
- try {
- this.sleep(5);
- } catch (InterruptedException e) {
- System.out.println("interrupted");
- thread.interrupt(); //catching the exception seems to clear it.
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment