Java billiard games (interest production)

Two pictures at the same level in src directory

 

A version appeared window
Package Penalty for cn.xjion.game;
/ **
* appears in the window
* @author xjion
*
* /
Import the java.awt *;.
Import in javax.swing with *;.
// window class inheritance
public class BallGame extends JFrame {
// loading process window
void launchFrame () {
// window size, width and height
setSize (300,300);
location // window, from the upper left corner of the window position of
the setLocation (400,200);
// window visible
setVisible (true);
}
// main process execution entry
public static void main (String [] args) {
// new objects
Ballgame Ballgame new new BG = ();
// call the method
bg.launchFrame ();
}
}
version two, load picture
here load picture there is a cache problem and needs to minimize the window and then open the
package cn.xjion.game;
/ **
* Load picture
* @author xjion
*
* /
Import the java.awt *;.
Import in javax.swing with *;.
// window class inheritance
public class BallGame extends JFrame {
target // create two images, representing two pictures
Image Ball = Toolkit.getDefaultToolkit () getImage ( "Image / ball.png");.
Image Desk = Toolkit.getDefaultToolkit () getImage ( "Image / desk.jpg");.
// draw window method
public void paint (Graphics G) {
// Videos Desktop
G.drawImage (Desk, 0, 0, null);
// draw ball object coordinates 100,100
G.drawImage (ball, 100, 100, null);
}

method of loading // window
launchFrame void () {
// window size, width and height
setSize (856,500);
location // window, the window position from the upper left corner of
the setLocation (50,50);
// window visible
setVisible (to true);
}
// main process execution entry
public static void main (String [] args) {
// new objects
Ballgame Ballgame new new BG = ();
// call the method
bg.launchFrame ();
}
}
Version three the small ball move
Package Penalty for cn.xjion.game;
/ **
* horizontal scrolling
* @author xjion
*
* /
Import the java.awt *;.
Import in javax.swing with *;.
// window class inheritance
public class BallGame extends JFrame {
// create two images of objects, representing two images
Image Ball = Toolkit.getDefaultToolkit () getImage ( "Image / ball.png");.
. Image Desk = Toolkit.getDefaultToolkit () getImage ( "Image / Desk. JPG ");

int X = 100;
int Y = 100;
Boolean = right to true;

// draw window method
public void Paint (Graphics G) {
// Videos Desktop
G.drawImage (Desk, 0, 0, null);
// draw ball object coordinates 100,100
G.drawImage (ball, X, Y, null );
// if the right to go to plus 10, or minus 10
IF (right) {
X = X + 10;
} the else {
X = X - 10;
}
// ball went when the head returns
if (x> 856 -40-30) {
right = to false;
}
IF (X <40) {
right = to true;
}

}
// window loading method
void launchFrame () {
// window size, width and height of
the setSize (856,500);
// position of the window, the window position from the upper left corner of
the setLocation (50,50);
// window visible
setVisible (to true);

// window redraw, redraw infinitely
the while (to true) {
// redraw method
the repaint ();
// try catch Throws
the try {
// 40ms drawn once for 1 sec 25
the Thread.sleep (40);
} the catch (Exception E) {
e.printStackTrace ();
}
}
}

// main process execution entry
public void main static (String [] args) {
// new objects
Ballgame Ballgame new new BG = ();
// call the method
bg.launchFrame ();
}
}
version four, so that the ball rolling along an arbitrary angle
package cn.xjion.game ;
/ **
* any scrolling
* @author xjion
*
* /
Import the java.awt *;.
Import in javax.swing with *;.
// window class inheritance
public class BallGame2 the extends JFrame {
// create two images of objects, two representatives pictures
Image ball = Toolkit.getDefaultToolkit () getImage ( "image / ball.png").;
Desk Toolkit.getDefaultToolkit = Image () the getImage ( "Image / desk.jpg");.
// coordinates on the window
Double X = 100;
Double Y = 100;

Double Degree = 3.14 /. 3; // arc, 60 °

// draw window method
public void Paint (Graphics G) {
// Videos Desktop
G.drawImage (Desk, 0, 0, null);
// draw ball object coordinates 100,100
G.drawImage (ball, (int) X, (int) y, null);
// change X and y coordinates of
X = X + 10 * Math.cos (Degree);
y = y + 10 * Math.sin, (Degree);

/ * determine when encountered boundary rebound * /
// touch down boundary rebound
IF (Y> 500-40-30 || Y <40 + 40) {
Degree = -degree;
}
// left and right boundaries encountered rebound
if (x> 856- X || 40-30 <30) {
Degree Degree-= 3.14;
}

}
method window // load
launchFrame void () {
// window size, width and height
setSize (856,500);
location // window, from the upper left corner of the window
(50,50) the setLocation;
// window is visible
setVisible (to true);

// redraw window, unlimited redraw
the while (to true) {
// redraw method
the repaint ();
// Throws the try the catch
the try {
// 40ms drawn once for 1 sec 25
the Thread.sleep (40);
} the catch (exception E ) {
e.printStackTrace ();
}
}
}

// main process execution entry
public static void main (String [] args) {
// new objects
BallGame2 new new BallGame2 BG = ();
// call the method
bg.launchFrame (http : //www.my516.com);
}
}
--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/10989716.html