Java instance-tank wars and java instance tank wars

Source: Internet
Author: User

Java instance-tank wars and java instance tank wars
JavaInstance-Tank warsI. sample image



Ii. class graph Structure



All classes in tank wars




Class Relationship Diagram




My tanks


Iii. Description

1. Every new thing that runs independently is a thread, like our tanks, like enemy tanks, like all bullets.

2. Each thread must implement the run method and start the thread.

3. No matter tanks or bullets, they all have life, coordinates (static), speed, and moving directions (motion)

4. The movement of tanks and bullets is achieved by the refresh coordinates after the coordinates are modified.

5. the bullets in the tank are all painted in the JPanel, And the repainting can show all the movements.

6. the continuous movement of bullets is realized through the endless loop in the run method, that is, constant coordinate transformation.

7. The keyboard action is monitored by the keyboard.

8. Multiple Threads are stored by collection classes, such as a collection of bullets and an array of enemy tanks.


9. the animation of your tank is caused by the repaint in the keyboard event. the animation of the enemy tank is caused by the call of the start function of the MyPanel thread on the Tank1 interface. The repaint function is available in the run interface.

10. The essence of running repaint is to call the paint function again, so we need to check what is in the paint.

11. In the paint function, I can draw anything with life. I can draw as many as I can, for example, our tanks, enemy tanks, our bullets, and enemy bullets.

12. drawing enemy bullets requires a dual loop. It is better to draw them together with enemy tanks.

13. A bullet can be drawn only when it is fired.

4. Code

Code on Baidu Disk:

(1) Main Interface: TankWarUI class


(2) tanks in war zones: MyPanel


(3) Bullet: Bullet class


(4) tanks: Tank


(5) My Tank Class: MyTank class


(6) enemy tanks: EnemyTank







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.