How to realize simple billiards game with Java
This article mainly explains "Java how to achieve a simple billiards game", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java how to achieve a simple billiards game" bar!
Java to achieve a simple billiard table problem, for your reference, the details are as follows
Demand:
So that the ball can move on the desktop, moving to the edge of the desktop will be bounced back, showing the movement of the community
Program source code:
Package billiard game; import java.awt.*;import javax.swing.*;public class BallGame extends JFrame {Image ball=Toolkit.getDefaultToolkit (). GetImage ("image/ball.png"); Image desk=Toolkit.getDefaultToolkit (). GetImage ("image/desk.jpg"); double x = true; / / direction * / double degree=3.14/3 / / Radian indicates that 60 °/ / window loads void launchFrame () {setSize (856500); / / parameters and desktop related setLocation (400400); setVisible (true); / / sets window visibility / / redraws the window, drawing while (true) {repaint () 25 times per second; / / repaint () method calls the paint () method try {Thread.sleep (20); / / 40ms,} catch (Exception e) {e.printStackTrace () }} / / drawing window method public void paint (Graphics g) {System.out.println ("window is drawn once!") ; g.drawImage (desk,0,0,null); g.drawImage (ball, (int) x, (int) y, null); / * / / Notes for horizontal movement of the ball; if (right) {xcolors 10;} else {xlin 10;} if (x > 856-40-30) / / diameter of the 30 ball, right=false; if (x500-40-30) of the 40-ball table frame | y