java實(shí)現(xiàn)潛艇大戰(zhàn)游戲源碼
代碼如下
package com.util; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Observable; import javax.swing.JLabel; import javax.swing.JPanel; /* * 進(jìn)圖游戲的主界面面板 */ public class MainPanel extends JPanel { private static final long serialVersionUID = 1L; private MyButton startButton; private MyButton exitButton; private JLabel helpLabel; private JLabel helpLabel1; private JLabel helpLabel2; private JLabel helpLabel3; private Image image; private JLabel centerlabel; private boolean isStart; private boolean isExit; private Observable obs; public MainPanel(Observable ob) { obs = ob; startButton = new MyButton("進(jìn)入游戲"); exitButton = new MyButton("退出游戲"); helpLabel = new JLabel(); helpLabel1 = new JLabel(); helpLabel2 = new JLabel(); helpLabel3 = new JLabel(); centerlabel = new JLabel(); this.setLayout(new BorderLayout()); this.helpLabel.setPreferredSize(new Dimension(645,291)); this.helpLabel1.setPreferredSize(new Dimension(180,80)); this.helpLabel2.setPreferredSize(new Dimension(215,80)); this.helpLabel3.setPreferredSize(new Dimension(645,80)); this.centerlabel.setPreferredSize(new Dimension(460,80)); centerlabel.setLayout(new GridLayout(2,1)); centerlabel.add(this.startButton); centerlabel.add(this.exitButton); this.centerlabel.setBackground(new Color(255,255,0)); this.add(helpLabel,BorderLayout.NORTH); this.add(helpLabel1,BorderLayout.EAST); this.add(helpLabel2,BorderLayout.WEST); this.add(helpLabel3,BorderLayout.SOUTH); this.add(centerlabel,BorderLayout.CENTER); image = Toolkit.getDefaultToolkit().getImage("imgs/主界面112.png"); // image = new ImageIcon(image).getImage(); this.startButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { boolean flag = true; MainPanel.this.setIsStart(flag); MainPanel.this.obs.notifyObservers(MainPanel.this); } } ); this.exitButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { MainPanel.this.setExit(true); } } ); } public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D) g; g2.drawImage(image,0,0, this.getWidth(), this.getHeight(),this); super.paintComponents(g); } public boolean getIsStart() { return this.isStart; } public void setIsStart(boolean isStart) { this.isStart = isStart; } public boolean isExit() { return isExit; } public void setExit(boolean isExit) { this.isExit = isExit; } }
效果圖
以上就是java實(shí)現(xiàn)潛艇大戰(zhàn)游戲源碼的詳細(xì)內(nèi)容,更多關(guān)于java潛艇大戰(zhàn)游戲的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SWT(JFace)體驗(yàn)之ApplicationWindow
SWT(JFace)體驗(yàn)之ApplicationWindow2009-06-06一篇文章了解Jackson注解@JsonFormat及失效解決辦法
這篇文章主要給大家介紹了關(guān)于如何通過一篇文章了解Jackson注解@JsonFormat及失效解決辦法的相關(guān)資料,@JsonFormat注解是一個(gè)時(shí)間格式化注解,用于格式化時(shí)間,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11MyBatis-plus使用lambda條件構(gòu)造器報(bào)錯(cuò)問題及解決
這篇文章主要介紹了MyBatis-plus使用lambda條件構(gòu)造器報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Android設(shè)備如何保證數(shù)據(jù)同步寫入磁盤的實(shí)現(xiàn)
這篇文章主要介紹了Android設(shè)備如何保證數(shù)據(jù)同步寫入磁盤的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09利用Java實(shí)現(xiàn)和可被K整除的子數(shù)組完整實(shí)例
這篇文章主要給大家介紹了關(guān)于利用Java實(shí)現(xiàn)和可被K整除的子數(shù)組的相關(guān)資料,這道題來自力扣,通過學(xué)習(xí)這道題的解題思路以及代碼對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01SpringBoot Mybatis Plus公共字段自動填充功能
這篇文章主要介紹了SpringBoot Mybatis Plus公共字段自動填充功能的相關(guān)資料,需要的朋友可以參考下2017-04-04spring 或者spring boot 調(diào)整bean加載順序的方式
這篇文章主要介紹了spring 或者spring boot 調(diào)整bean加載順序的方式,本文通過實(shí)例代碼講解三種調(diào)整類加載順序的方式,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03使用Spring的AbstractRoutingDataSource實(shí)現(xiàn)多數(shù)據(jù)源切換示例
這篇文章主要介紹了使用Spring的AbstractRoutingDataSource實(shí)現(xiàn)多數(shù)據(jù)源切換示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02