JAVA课程设计——俄罗斯方块

0.负责模块为可视化界面,技术栈为

(1)异常处理
(2)多线程
(3)文件存储
(4)Java swing

1.登陆界面

我的代码

import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import tetris.control.*;

    /**
    *
     * @author  lsm
    *
    */
    public class Main {

        private static void login() {
            JFrame frame = new JFrame();
            frame.setResizable(false);
            ImageIcon background = new ImageIcon("beautify/login.jpg");
            JLabel backLabel = new JLabel(background);
            backLabel.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
            JPanel panel = new JPanel();
            panel = (JPanel) frame.getContentPane();
            panel.setOpaque(false);
            panel.setLayout(new FlowLayout());
            frame.getLayeredPane().add(backLabel, new Integer(Integer.MIN_VALUE)); 

            /*设置界面属性*/
            frame.setTitle("Tetris");
            GridLayout grid = new GridLayout(2, 2);
            grid.setHgap(25);
            grid.setVgap(25);
            JPanel panel1 = new JPanel(grid);
            panel1.setOpaque(false);
            Font f1 = new Font("Serif", Font.BOLD, 27);
            Font f2 = new Font("Serif", Font.ITALIC, 47);
            JTextField b1 = new JTextField(10);
            JPasswordField b2 = new JPasswordField(10);
            JLabel label1 = new JLabel("         Player Name");
            JLabel label2 = new JLabel("         Password");
            JLabel label3 = new JLabel("Welcome to Tetris!");

            label1.setFont(f1);
            label2.setFont(f1);
            label3.setFont(f2);
            panel1.add(label1);
            panel1.add(b1);
            panel1.add(label2);
            panel1.add(b2);

            JPanel panel2 = new JPanel();
            panel2.setOpaque(false);
            JButton button1 = new JButton("Login");
            JButton button2 = new JButton("Register");
            button1.setForeground(Color.BLACK);
            button2.setForeground(Color.BLACK);
            button1.setBackground(Color.lightGray);
            button2.setBackground(Color.lightGray);

            panel2.add(button1);
            panel2.add(button2);
            panel.add(panel1);
            panel.add(panel2);
            panel.add(label3);

            frame.setBounds(100, 100, background.getIconWidth(), background.getIconHeight());
            frame.setLocation(450, 60);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(500, 500);
            frame.setVisible(true);

            button1.addActionListener(new ActionListener() {
                @SuppressWarnings("deprecation")
                @Override
                public void actionPerformed(ActionEvent arg0) {

                    String playerName = b1.getText();
                    String playerPassword = b2.getText();
                    try {
                        if (PlayerFile.matchPassword(playerName, playerPassword)) {
                            frame.setVisible(false);
                            new RussiaBlocksGame("Tetris");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            });

            button2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    String playerName = b1.getText();
                    @SuppressWarnings("deprecation")
                    String playerPassword = b2.getText();
                    try {
                        if (!PlayerFile.writeFile(playerName, playerPassword)) {

                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            });
        }

        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Music();
                    login();
                }
            });
        }
    }

界面图,加入了背景音乐,引用自 https://blog.csdn.net/qq_40590018/article/details/81452392

1.2游戏界面

我的代码(这里就贴主要界面代码,具体方法不展示)

            public class RussiaBlocksGame extends JFrame {
                    private GameCanvas canvas;
                private ErsBlock block;
                private boolean playing = false;
                private ControlPanel ctrlPanel;
                private JMenuBar menuBar = new JMenuBar();
                private JMenu help = new JMenu("Help");
                private JMenu setting = new JMenu("Setting");
                private JMenuItem about = new JMenuItem("About");
                private JMenuItem setBackground = new JMenuItem("SetBackground");
                private JMenuItem setFrontground = new JMenuItem("SetFrontground");
                        private JMenuItem chart=new JMenuItem("Ranking List");

                /*
                 *  初始化菜单栏
                 */
                private void initScope() {

                    setJMenuBar(menuBar);
                    menuBar.add(help);
                    menuBar.add(setting);
                    help.add(about);
                    help.add(chart);
                    setting.add(setBackground);
                    setting.add(setFrontground);
                    /*
                     *  设置自定义颜色,引用自 https://www.jb51.net/article/142716.htm
                     */
                    setFrontground.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                    Color newFrontColor = JColorChooser.showDialog(RussiaBlocksGame.this, "SetFrontground", canvas.getBlockColor());
                            if (newFrontColor != null) {
                                canvas.setBlockColor(newFrontColor);
                            }
                        }
                    });

                    setBackground.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            Color newBackColor = JColorChooser.showDialog(RussiaBlocksGame.this, "SetBackground",
                                    canvas.getBackgroundColor());
                            if (newBackColor != null) {
                                canvas.setBackgroundColor(newBackColor);
                            }
                        }
                    });
                    about.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            JOptionPane.showMessageDialog(null,
                    "→ move right " + "← move left " + "↓ speed up " + "↑ conservasion");
                        }
                    });
                    chart.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            try {
                                new RanklistDialog(PlayerFile.outputCharts());
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }
                    });

                }

                public RussiaBlocksGame(String title) {
                    super(title);
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    setSize(450, 570);
                    setLocation(450, 60);
                    setTitle("Tetris");

                    initScope();
                    Container container = getContentPane();
                    container.setLayout(new BorderLayout(6, 0));
                    canvas = new GameCanvas(ROWS, COLS);
                    ctrlPanel = new ControlPanel( this );
                    container.add(canvas, BorderLayout.CENTER);
                    container.add(ctrlPanel, BorderLayout.EAST);

                    addWindowListener(new WindowAdapter() {
                        @Override
                        public void windowClosing(WindowEvent we) {
                            stopGame();
                            System.exit(0);
                        }
                    });
                    addComponentListener(new ComponentAdapter() {
                        @Override
                        public void componentResized(ComponentEvent ce) {
                            canvas.adjust();
                        }
                    });
                    setVisible(true);
                    setResizable(false);
                }
            }

游戏界面为

3.排行榜(只取前五名)

我的代码:

            import java.awt.Color;
    import java.awt.Font;
    import java.util.HashMap;
    import java.util.Map;
    import javax.swing.ImageIcon;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class RanklistDialog extends JDialog{
        private JLabel score1,score2,score3,score4,score5;
        private JLabel name1,name2,name3,name4,name5;
        private String[][] ranking;

        public RanklistDialog(List<Entry<String, String>> list) {
            ranking = new String[5][2];
            int i=0;
            for(List.Entry<String, String> e: list.entrySet()) {
                String name=e.getKey();
                String  score=e.getValue();
                ranking[i][0]=name;
                ranking[i][1]=score;
                i++;
            }
            Font f = new Font("Serif", Font.ITALIC, 25);
            name1 = new JLabel();
            name1.setText(ranking[0][0]);
            name1.setFont(f);
            name1.setForeground(Color.yellow);
            name1.setBounds(150,270, 100, 30);
            this.add(name1);
            score1 = new JLabel();
            score1.setText(ranking[0][1]);
            score1.setFont(f);
            score1.setForeground(Color.yellow);
            score1.setBounds(280,270,100, 30);
            this.add(score1);

            name2 = new JLabel();
            name2.setText(ranking[1][0]);
            name2.setFont(f);
            name2.setForeground(Color.yellow);
            name2.setBounds(150,330,100, 30);
            this.add(name2);
            score2 = new JLabel(ranking[1][1]);
            score2.setFont(f);
            score2.setForeground(Color.yellow);
            score2.setBounds(280,330,100, 30);
            this.add(score2);

            name3 = new JLabel();
            name3.setText(ranking[2][0]);
            name3.setFont(f);
            name3.setForeground(Color.yellow);
            name3.setBounds(150, 390, 100, 30);
            this.add(name3);
            score3 = new JLabel(ranking[2][1]);
            score3.setFont(f);
            score3.setForeground(Color.yellow);
            score3.setBounds(280, 390, 100, 30);
            this.add(score3);

            name4 = new JLabel();
            name4.setText(ranking[3][0]);
            name4.setFont(f);
            name4.setForeground(Color.yellow);
            name4.setBounds(150, 450, 100, 30);
            this.add(name4);
            score4 = new JLabel(ranking[3][1]);
            score4.setFont(f);
            score4.setForeground(Color.yellow);
            score4.setBounds(280, 450, 100, 30);
            this.add(score4);
            name5 = new JLabel();
            name5.setText(ranking[4][0]);
            name5.setFont(f);
            name5.setForeground(Color.yellow);
            name5.setBounds(150, 510, 100, 30);
            this.add(name5);
            score5 = new JLabel(ranking[4][1]);
            score5.setFont(f);
            score5.setForeground(Color.yellow);
            score5.setBounds(280, 510, 100, 30);
            this.add(score5);

            ImageIcon icon = new ImageIcon("beautify/charts.jpg");
            JLabel background = new JLabel(icon);
            background.setBounds(0, 0, icon.getIconWidth(),icon.getIconHeight());
            JPanel imagePanel = (JPanel)this.getContentPane();
            imagePanel.setOpaque(false);
            imagePanel.setLayout(null);
            this.setModal(true);
            this.setTitle("Ranking List");
            this.setLayout(null);
            this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
            this.setResizable(false);
            this.setSize(400, 600);
            this.setLocationRelativeTo(null);
            this.setVisible(true);
            this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        }
    }

排行榜界面(好像要暴露什么)

4.我的总结

对于一个喜欢美术的人来说,界面设计真的是爽歪歪啊~,不过好像效果一般啊。。。一直对代码这块比较抵触,开发也不是以后的发展方向,
,这次课程设计这个模块就主要是整合另外两个成员写的方法,难度小,乐在其中。

原文地址:https://www.cnblogs.com/afairyfairy/p/11939080.html

时间: 2024-08-29 22:59:28

JAVA课程设计——俄罗斯方块的相关文章

JAVA课程设计 俄罗斯方块

俄罗斯方块 可实现功能 1.账号管理:登录.注册 2.游戏实现:移动.旋转.消除方块统计得分.暂停游戏.暂停后继续游戏.此轮游戏未结束开启新一轮游戏.游戏未结束退出游戏. 3.排行榜:按分数排名.按局数排名(尚待改进) 一.团队介绍 团队名称:俄罗斯方块 二.项目git地址 https://gitee.com/QianChenYangYang/Tetris.git 三.项目git提交记录截图 四.项目功能架构图与主要功能流程图 (1)功能架构图 (2)主要功能流程图 五.项目运行截图 1.游戏打

JAVA课程设计——俄罗斯方块(团队)

1.团队介绍 1.1 团名:终于可以回家了嗷嗷嗷 1.2 团员介绍 2.参考来源 https://www.jb51.net/article/142716.htm 3.项目git地址 先空着 4.前期调查 5.项目功能架构图.主要功能流程图 6.UML图 7.运行截图 7.1登陆界面 7.2注册成功 7.3登陆后转换为游戏界面 7.4排行榜 8.关键代码 8.1登陆界面账号密码匹配操作,优先匹配账号 8.2文件更新操作,每轮游戏过后,都会将所获得的信息进行更新 8.3对文件中的分数进行排序操作,取

java(课程设计之记事本界面部分代码公布)

代码:涉及记事本的一些界面......!! 1 /* 2 *java课程设计之记事本(coder @Gxjun) 3 * 编写一个记事本程序 4 * 要求: 5 * 用图形用户界面实现. 6 * 能实现编辑.保存.另存为.查找替换等功能. 7 * 提示:使用文件输入输出流. 8 */ 9 package project; 10 11 import javax.swing.*; 12 import java.awt.*; 13 import java.awt.event.*; //引用类设置触发事

Java课程设计—学生成绩分析系统

Java课程设计 - 我的选题是学生成绩分析系统. 下面是课程的需求图: 完成的这个小系统其实逻辑代码是非常简单的,唯一感觉有难点的地方在于涉及到了知识点比较多.当时只是匆匆地大概学了Java的一些基本知识,书上的例题和代码也没怎么敲过, 几乎都是在边做的过程中边学具体需要用到的东西: 其中感觉最麻烦的地方就是JTable.文件读取IO流以及绘图和给各个组件布局的时候. 另外还花了些功夫在UI上面:我这次是用到了substance.jar的swing美化包,想了解的可以百度下,网上有很多教程.

java课程设计团队(搜索引擎)

JAVA课程设计 基于学院网站的搜索引擎 对学院网站进行抓取.建索(需要中文分词).排序(可选).搜索.摘要显示.可以是GUI界面,也可以是Web界面. 一.团队介绍 学号 班级 姓名 简介 201621123049 网络1612 [组长]袁德兴 热衷于网络安全 201621123047 网络1612 陈芳毅 有思想,有深度 ,有能力 201621044079 网络1612 韩烨 学习力强,人称韩可爱 201621123055 网络1612 刘兵 人称五社区发哥,动手能力强 2016211230

java课程设计--坦克大战

java课程设计--坦克大战 一. 团队课程设计博客链接 https://www.cnblogs.com/zwtcyt/p/12173572.html 二.个人负责模块和任务说明 墙体类,子弹类,道具类以及音效类的编写,部分GUI的编写 三.代码的提交记录截图 四.负责模块和任务详细说明 墙体类 主墙体即不可摧毁的墙体 该类为所有障碍物的父类,子类继承时改变里面draw方法 草地类 河流类 可摧毁的墙类 音效类 子弹类 子弹与碰撞检测 利用javafx中shape的intersect方法来得出两

java课程设计

程序设计实训报告 题目:计算数学表达式程序 1.课设目的 (1)复习巩固java语言的基础知识,进一步加深对java语言的理解和掌握 (2)课设为大家提供一个即动手又动脑,独立实践的机会.提高我们适应实际,编程的能力 (3)培养我们在项目开发中创新意识及能力,通过亲身实践,利用所学编写简单的面向对象程序,提高对面向对象及java语言的解释 2.设计题目分析 (1)由用户输入一个简单的四则运算表达式,求出其计算结果后显示 (2)允许在表达式中出现常用的数学函数,如取整.三角函数.倒数.平方根.平方

Java课程设计——扫雷(winmine)

因为是我的课程设计,要是有冲突就不好了,转载注明出处!!! 程序很简单,毕竟我是搞acm的,我就只介绍一下闪光点. 中心空白搜索的时候,我用的DFS: API文档之后补上. package com.TreeDream.MyGame; public class Block { String name; int number; boolean boo = false; public void setName(String name) { this.name = name; } public void

数据库+Java课程设计 人事管理系统 (一)

                                                                  一.JAVA与数据库的合作 此次开发语言为Java,所用的数据库驱动是mysql-connector-java-5.1.8-bin.jar 第一步   用Java连接MySQL数据库(驱动下载:https://dev.mysql.com/downloads/connector/j/   ) 将下载好的mysql-connector-java-5.1.8-bin.ja