当前位置:网站首页>Make a static tank
Make a static tank
2022-06-11 18:36:00 【C_ x_ three hundred and thirty】
List of articles
painting
Basic knowledge
To develop the tank war game , I think you should think about the following :
- Make interface
- Draw a static tank
The above two questions are what this blog will take you to solve , To complete the above two challenges , You have to know Java A tool class provided
JFrame It can be understood as a frameworkJPanel Can be understood as a sketchpad If you want to paint There must be a way to draw things on the board paint()Graphics g This g It can be understood as a brushFirst of all, you need to know , The drawing board should be placed on top of a frame , And the brush should draw the content , You must draw on the drawing board
- So based on the above little knowledge , Then we can write the outline of the framework , Implement the above class through code
public class Tank extends JFrame {
private Mypanel mp=null;
}
class Mypanel extends JPanel{
@Override
public void paint(Graphics g) {
super.paint(g);
}
}
Just said , The most basic thing to make a page is to have a frame , Then there is the drawing board , So how to add the Sketchpad to the framework in the form of code ? To find out , We need to know something about JFrame Some of the ways
this.add(mp); This add The method is equivalent to putting our drawing board Add to frame this.setSize(1000,800); This is to set the size of the frame , Default color It's white this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); This method is when we click the exit interface to make , Give Way JavaJVM The virtual machine also shuts down automatically this.setVisible(true); This method is to make what we draw on the drawing board visible
Interface building
With the above knowledge reserve , We can continue to adjust our interface construction
public class Tank extends JFrame { private Mypanel mp=null; public Tank(){ mp=new Mypanel(); this.add(mp); this.setSize(1000,800); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { Tank tank = new Tank(); } } class Mypanel extends JPanel{ @Override public void paint(Graphics g) { super.paint(g); } }- The construction of such a basic framework and panel is completed
Common graphic drawing methods
Then we can simply draw some figures
- Before painting , We need to know a little bit first , This paint() The method is to open the interface every time , Or panel redrawing , Or when the keyboard or mouse moves, it will be automatically called by the system
- The upper left corner of the Sketchpad is set as the origin , Horizontal axis x Axis , The vertical axis is y Axis , You should know very well from the picture I drew
public void paint(Graphics g) { super.paint(g); g.drawLine(10,10,50,50); Draw a straight line The first two parameters are x y Starting position The last two parameters are of the end point x y The location of g.fillOval(20,20,30,30); To draw you first have to get to a point He drew this in a rectangle This is a painting The first two parameters are the starting coordinates Then, the last two parameters must be the same to ensure that a g.setColor(Color.green); Set the color of the brush g.drawString("Cx_330",100,100); Draw a string g.fill3DRect(110,110,60,60,false); This is to draw 3D Rectangle of effect Later parameters true perhaps false Fine It depends on everyone's preferences Image image=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/b3.jpg")); g.drawImage(image,250,250,100,200,this); To draw a picture, you must first create a picture The format is fixed , But one thing to note is that you have to put your picture in out Directory is the next level directory of your module . If you still don't understand, take a look at the screenshot of the module in the first blog }
Painting tanks
Through this picture , You can know , It's easy to draw a tank , Just draw :
- Two identical rectangles As a tire
- A rectangle acts as a shell
- A circle serves as a lid
- A thin line acts as a cannon barrel
- Knowing this , Then when drawing, you only need to understand the coordinates of each figure , length , Width , Then you can easily draw a tank , Before drawing, I suggest you try to draw a tank on the paper , Mark out the corresponding coordinates and then implement them in code. This will be very simple
- Code demonstration
drawTank(hero.getX(),hero.getY(),g,hero.getDirect(),0); --------------------------------------------------------------------------------- public void drawTank(int x,int y,Graphics g,int direct,int type){ //0 My tank 1 Enemy tanks switch (type){ case 0: g.setColor(Color.cyan); break; case 1: g.setColor(Color.green); break; } // Direction (w 0 On 1d 2 Right s 3 Next a Left ) switch (direct){ case 0:// g.fill3DRect(x,y,10,60,false);// Left wheel g.fill3DRect(x+30,y,10,60,false);// Right wheel g.fill3DRect(x+10,y+10,20,40,false);// Body frame g.fillOval(x+10,y+20,20,20);// The lid g.drawLine(x+20,y,x+20,y+20);// Barrel break; case 1:// g.fill3DRect(x,y,60,10,false);// Left wheel g.fill3DRect(x,y+30,60,10,false);// Right wheel g.fill3DRect(x+10,y+10,40,20,false);// Body frame g.fillOval(x+20,y+10,20,20);// The lid g.drawLine(x+60,y+20,x+20,y+20);// Barrel break; case 2:// g.fill3DRect(x,y,10,60,false);// Left wheel g.fill3DRect(x+30,y,10,60,false);// Right wheel g.fill3DRect(x+10,y+10,20,40,false);// Body frame g.fillOval(x+10,y+20,20,20);// The lid g.drawLine(x+20,y+20,x+20,y+60);// Barrel break; case 3:// g.fill3DRect(x,y,60,10,false);// Left wheel g.fill3DRect(x,y+30,60,10,false);// Right wheel g.fill3DRect(x+10,y+10,40,20,false);// Body frame g.fillOval(x+20,y+10,20,20);// The lid g.drawLine(x,y+20,x+20,y+20);// Barrel break; } }
边栏推荐
- Async leads to unexpected function results and changes the intention of the original code; await is only valid in async functions and the top level bodies of modules
- Niu Ke's question -- Fibonacci series
- 最长严格递增子序列
- Quanzhi T3 development board (4-core arm cortex-a7) - detailed explanation of logo display during system startup
- [c language] compress strings and add markup characters
- SA token single sign on SSO mode 2 URL redirection propagation session example
- EasyCwmp源码分析
- [golang] leetcode - 349 Intersection of two arrays (hash table)
- MMA-Self-defining function
- 力扣32题最长有效括号
猜你喜欢

Niu Ke's question -- finding the least common multiple

北京邮电大学2023级工商管理硕士MBA(非全日制)已开启

V-for loop traversal

Labelme for image data annotation

牛客刷题——二叉搜索树与双向链表

力扣刷题——二叉树的层序遍历Ⅱ
开发中必备的文件的上传与下载

Common operations of Visio

Why is ti's GPMC parallel port more often used to connect FPGA and ADC? I give three reasons

Oracle高级数据库复习
随机推荐
On the sequence traversal of binary tree Ⅱ
论工作流选型
判断是否为平衡二叉树
Apipost精妙使用技巧
SAP BTP 上 workflow 和 Business Service 的 project 管理
Surveillance des fonctions de perte avec visdom
学习使用LSTM和IMDB评论数据进行情感分析训练
力扣32题最长有效括号
MATLAB 保存imshow绘制图片到指定文件夹中的两种方法
[c language] compress strings and add markup characters
全志科技T3开发板(4核ARM Cortex-A7)——视频开发案例
SA token single sign on SSO mode 2 URL redirection propagation session example
初识企业级平台
牛客刷题——求最小公倍数
V-for loop traversal
力扣31 下一个排列
使用Visdom對損失函數進行監控
用户组的操作
Specific methods for JS to realize full screen display
Ubuntu installs PSQL and runs related commands