Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to realize billiards Mini Game

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report