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 realize simple billiards game with Java

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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

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