How to use java to realize billiards Mini Game
This article mainly shows you "how to use java to achieve billiards Mini Game", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use java to achieve billiards Mini Game" this article.
The details are as follows
Billiard Mini Game
The two pictures are placed in the same directory of src
Version 1. Window appears
Package cn.xjion.game;/** * window appears * @ author xjion * * / import java.awt.*;import javax.swing.*;// inherits window class public class BallGame extends JFrame {/ / window loading method void launchFrame () {/ / window size, width and height setSize (300300); / / position of the window, setLocation (400200) from the upper left corner of the window; / / window visible setVisible (true) } / / main method, execute entry public static void main (String [] args) {/ / New object BallGame bg = new BallGame (); / / call method bg.launchFrame ();}}
Version 2. Loading diagram
There is a cache problem with the loaded images here. You need to minimize the window before opening it.
Package cn.xjion.game;/** * load picture * @ author xjion * * / import java.awt.*;import javax.swing.*;// inherits the window class public class BallGame extends JFrame {/ / to create an object of two pictures, representing two pictures Image ball = Toolkit.getDefaultToolkit (). GetImage ("image/ball.png"); Image desk = Toolkit.getDefaultToolkit (). GetImage ("image/desk.jpg") / / method of drawing window public void paint (Graphics g) {/ / drawing desktop g.drawImage (desk, 0,0, null); / / drawing ball object with coordinates 100100 g.drawImage (ball, 100,100, null);} / / method of window loading void launchFrame () {/ / window size, width and height setSize (856500); / / position of the window, distance from the position of the upper left corner of the window setLocation (505050); / / window visible setVisible (true) } / / main method, execute entry public static void main (String [] args) {/ / New object BallGame bg = new BallGame (); / / call method bg.launchFrame ();}}
Version 3. Make the ball move
Package cn.xjion.game;/** * horizontal scrolling * @ author xjion * * / import java.awt.*;import javax.swing.*;// inherits the window class public class BallGame extends JFrame {/ / to create an object of two pictures, representing two pictures Image ball = Toolkit.getDefaultToolkit (). GetImage ("image/ball.png"); Image desk = Toolkit.getDefaultToolkit (). GetImage ("image/desk.jpg"); int x = 100; int y = 100; boolean right = true / / public void paint (Graphics g) {/ / draw desktop g.drawImage (desk, 0,0, null); / / draw ball object with coordinates 100100 g.drawImage (ball, x, y, null); / / add 10 if you go right, otherwise subtract 10 if (right) {x = x + 10;} else {x = x-10;} / return if (x > 856-40-30) {right = false when the ball ends } if (x500-40-30 | | y856-40-30 | | x