1. 程式人生 > >java碰撞小球

java碰撞小球

package myproj;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.Timer;

public class MyMagicBall {

	static class Ball {
		private int x = (int) (Math.random() * 100 + 100);
		private int y = (int) (Math.random() * 100 + 100);
		private int vx = (int) (Math.random() * VELOCITY_STEP + VELOCITY_STEP);
		private int vy = (int) (Math.random() * VELOCITY_STEP + VELOCITY_STEP);
		private final static double VELOCITY_STEP = 2.0;
		private int radius = 6;
		private Window win;
		private Color color = new Color(255, 0, 0);

		Ball(Window win) {
			this.win = win;
		}

		public Rectangle boundingbox() {
			return new Rectangle(x - radius - 1, y - radius - 1, radius * 2 + 2, radius * 2 + 2);
		}

		public void randomBump() {
			vx += (int) ((Math.random() * VELOCITY_STEP) - (VELOCITY_STEP / 2));
			vx = -vx;
			vy += (int) ((Math.random() * VELOCITY_STEP) - (VELOCITY_STEP / 2));
			vy = -vy;
		}

		public void move() {
			x = x + vx;
			if (x <= radius) {
				x = radius;
				vx = -vx;
			}
			if (x >= win.getWidth() - radius) {
				x = win.getWidth() - radius;
				vx = -vx;
			}

			y = y + vy;
			if (y <= radius) {
				y = radius;
				vy = -vy;
			}
			if (y >= win.getHeight() - radius) {
				y = win.getHeight() - radius;
				vy = -vy;
			}
		}

		public void paint(Graphics g) {
			Rectangle content = g.getClipBounds();
			if (content.intersects(this.boundingbox())) {
				g.setColor(color);
				g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationWindow frame = new ApplicationWindow();

		// the following code realizes the top level application window
		frame.pack();
		frame.setVisible(true);
	}

	private static class Window extends JComponent {
		static Ball ball;
		WindowListener windowListener;
		Timer timer;
		private boolean mode;
		private int FRAME_PER_SECOND = 100;

		public Window() {
			// TODO Auto-generated constructor stub
			super();
			ball = new Ball(this);
			windowListener = new WindowListener();
			timer = new Timer(1000 / FRAME_PER_SECOND, windowListener);
			mode = false;
		}

		@Override
		public void paintComponent(Graphics g) {
			// first repaint the proper background color (controlled by
			// the windowing system)
			// super.paintComponent(g);
			ball.paint(g);
		}

		public void update() {
			Rectangle oldpos = ball.boundingbox();
			ball.move();
			oldpos.union(ball.boundingbox());
			repaint(oldpos);
		}

		public void setMode(boolean m) {
			if (mode == m) {
				return;
			}
			if (mode == true) {
				removeKeyListener(windowListener);
				removeMouseMotionListener(windowListener);
				removeMouseListener(windowListener);
			}

			mode = m;
			if (mode == true) {
				addKeyListener(windowListener);
				addMouseListener(windowListener);
				addMouseMotionListener(windowListener);
				timer.start();
			} else {
				timer.stop();
			}
		}

		class WindowListener extends MouseAdapter implements KeyListener, MouseMotionListener, ActionListener {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				update();
			}

			@Override
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub

			}
			 @Override public void mouseClicked(MouseEvent e) {
	                ball.randomBump();
	            }
			@Override
			public void mouseMoved(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				ball.randomBump();
			}

			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub

			}

		}
	}

	private static class ApplicationWindow extends JFrame {
		protected static Window win;

		public ApplicationWindow() {
			// TODO Auto-generated constructor stub
			super("Swing Demonstration Program");

			// respond to the window system asking us to quit
			addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					System.exit(0);
				}
			});
			JToolBar jToolBar = new JToolBar();

			addButton(jToolBar);
			win = new Window();

			JScrollPane scrollPane = new JScrollPane(win);

			// Lay out the content pane.
			JPanel contentPane = new JPanel();
			contentPane.setLayout(new BorderLayout());
			contentPane.setPreferredSize(new Dimension(510, 530));
			contentPane.add(jToolBar, BorderLayout.NORTH);
			contentPane.add(scrollPane, BorderLayout.CENTER);
			setContentPane(contentPane);
		}

		void addButton(JToolBar jtbar) {
			JButton jb = null;
			jb = new JButton("start");
			jb.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					win.setMode(true);
				}
			});
			jtbar.add(jb);

			jb = new JButton("stop");
			jb.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					win.setMode(false);
				}
			});
			jtbar.add(jb);
			// win.add(jb);

			jb = new JButton("quit");
			jb.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					System.exit(0);
				}
			});
			// win.add(jb);
			jtbar.add(jb);

		}
	}

}