前言学Java的朋友们,福利来了,今天小编给大家带来了一款原生java8编写的 台球源码,看图:

图片

视频演示

https://githubs.xyz/show/179.mp4

代码采用原生java类库编写, 利用java swing作为界面框架,完整源码获取地址:

gitee.com/hadluo/java_game01.git

环境

JDK1.8

源码实现

我们先从main函数看起,找到 Main.java ,这个是main函数的入口,

public static void main(String[] args) {
               
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                @SuppressWarnings("unused")
                Main start = new Main();
            }
        });
    }

下面会执行Main对象的构造函数, 加载了一些列的界面组件,

public Main() {
        
        // Code for setting up JFrames
        splashScreen.setSize(WIDTH, HEIGHT);
        splashScreen.setExtendedState(JFrame.MAXIMIZED_BOTH);
        mainScreen.setSize(WIDTH, HEIGHT);
        playScreen.setVisible(false);

        mainScreen.setExtendedState(JFrame.MAXIMIZED_BOTH);
        mainScreen.setUndecorated(true);
        mainScreen.setVisible(false);

        try {
            splashScreen.setContentPane(new JLabel(
                    new ImageIcon(ImageIO.read(new File("resource/Images/8 Ball Pool SplashScreen.jpg"))
                            .getScaledInstance((int) WIDTH, (int) HEIGHT, Image.SCALE_SMOOTH))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        splashScreen.getContentPane().setLayout(new BorderLayout(5, 5));
        splashScreen.setUndecorated(true);
        splashScreen.setVisible(true);
        
        // Blinking label animation for splashscreen
        BlinkLabel anyKeyLabel = new BlinkLabel("Press any key to continue...");
        anyKeyLabel.setForeground(Color.WHITE);
        anyKeyLabel.setFont(new Font("Impact", Font.PLAIN, 40));
        splashScreen.getContentPane().add(anyKeyLabel, BorderLayout.PAGE_END);
        anyKeyLabel.startBlinking();

        // Background music in the game
        final Sound bgMusic = new Sound();
        bgMusic.setVolume(-10);

        try {
            bgMusic.loadSound("resource/Music/This City Prod. David Wud.wav");
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (Throwable e1) {
            e1.printStackTrace();
        }

        splashScreen.addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent e) {
                splashScreen.dispose();
                mainScreen.setVisible(true);

                try {
                    bgMusic.playSound();
                } catch (Throwable e1) {
                    e1.printStackTrace();
                }
            }

            public void keyReleased(KeyEvent e) {
            }

            public void keyTyped(KeyEvent e) {
            }
        });
        
        //setting up main menu
        try {
            mainScreen.setContentPane(new JLabel(
                    new ImageIcon(ImageIO.read(new File("resource/Images/main menu background.jpg"))
                            .getScaledInstance((int) WIDTH, (int) HEIGHT, Image.SCALE_SMOOTH))));
        } catch (IOException e) {
            e.printStackTrace();
        }

        mainScreen.getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        
        //panel for all the menu buttons
        JPanel menuPane = new JPanel();
        menuPane.setBackground(new Color(0, 0, 0, 100));
        menuPane.setPreferredSize(new Dimension((int) WIDTH / 3, (int) HEIGHT - (int) HEIGHT / 5));
        menuPane.setLayout(new BoxLayout(menuPane, BoxLayout.Y_AXIS));
        menuPane.setBorder(BorderFactory.createEmptyBorder(25, 25, 25, 25));

        //panel for help screeen and settings
        final JPanel helpPane = new JPanel();
        helpPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        helpPane.setLayout(new BoxLayout(helpPane, BoxLayout.Y_AXIS));
        helpPane.setPreferredSize(new Dimension((int) WIDTH / 3, (int) HEIGHT - (int) HEIGHT / 5));
        helpPane.setBackground(new Color(0, 0, 0, 125));
        helpPane.setOpaque(false);

        JLabel logoLabel;
        try {
            logoLabel = new JLabel(new ImageIcon(ImageIO.read((new File("resource/Images/logo.png")))
                    .getScaledInstance((int) WIDTH / 3 - 30, (int) HEIGHT / 5, Image.SCALE_SMOOTH)));
            logoLabel.setBackground(new Color(0, 0, 0, 0));
            helpPane.add(logoLabel);
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(0, 0, 0, 125);
        mainScreen.getContentPane().add(menuPane, c);
        c.insets = new Insets(0, 125, 0, 0);
        c.gridx = 1;
        mainScreen.getContentPane().add(helpPane, c);

        JButton playButton = button("Play");
        playButton.setAlignmentX(menuPane.CENTER_ALIGNMENT);
        menuPane.add(Box.createRigidArea(new Dimension(0, 50)));
        menuPane.add(playButton);
        
        //you can click enter on the mainscreen to play
        menuPane.getRootPane().setDefaultButton(playButton);
        
        playButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Removes shutter issue (check method header for details)
                startBalls();

            }
        });

        menuPane.add(Box.createRigidArea(new Dimension(0, 75)));

        JButton helpButton = button("Help");
        helpButton.setAlignmentX(menuPane.CENTER_ALIGNMENT);
        menuPane.add(helpButton);

        menuPane.add(Box.createRigidArea(new Dimension(0, 75)));

        JButton settingsButton = button("Settings");
        settingsButton.setAlignmentX(menuPane.CENTER_ALIGNMENT);
        menuPane.add(settingsButton);

        menuPane.add(Box.createRigidArea(new Dimension(0, 75)));
        
        JButton loadButton=button("Load");//button for thomas
        loadButton.setAlignmentX(menuPane.CENTER_ALIGNMENT);
        menuPane.add(loadButton);
        menuPane.add(Box.createRigidArea(new Dimension(0, 75)));

        loadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startBalls();
                content.loadGame("resource/saveFile.txt");
            }
        });

        JButton exitButton = button("Exit");
        exitButton.setAlignmentX(menuPane.CENTER_ALIGNMENT);
        menuPane.add(exitButton);

        helpButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                helpPane.removeAll();
                helpPane.setOpaque(true);
                JTextArea helpText = new JTextArea();
                JLabel helpTitle = new JLabel("How to Play 8-Ball Pool");
                helpTitle.setFont(new Font("High Tower Text", Font.BOLD, 24));
                helpTitle.setForeground(Color.white);
                helpTitle.setAlignmentX(helpPane.CENTER_ALIGNMENT);
                helpText.setText(helpString);
                helpText.setEditable(false);
                helpText.setFont(new Font("High Tower Text", Font.PLAIN, 20));
                helpText.setForeground(Color.WHITE);
                helpText.setHighlighter(null);
                helpText.setBackground(Color.BLACK);
                helpText.setLineWrap(true);
                helpText.setWrapStyleWord(true);
                JScrollPane helpScroll = new JScrollPane(helpText);
                helpScroll.setOpaque(false);
                helpPane.add(helpTitle);
                helpPane.add(helpScroll);

                mainScreen.revalidate();
                mainScreen.repaint();
            }
        });


        final JCheckBox checkMusic = new JCheckBox("Music");
        checkMusic.setSelected(true);

        settingsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                helpPane.removeAll();
                helpPane.setOpaque(true);
                
                checkMusic.setBackground(Color.black);
                checkMusic.setForeground(Color.white);
                checkMusic.setFont(new Font("Impact", Font.PLAIN, 28));
                checkMusic.setAlignmentX(helpPane.CENTER_ALIGNMENT);
                checkMusic.setFocusPainted(false);
                checkMusic.setMargin(new Insets(0, 20, 0, 20));

                helpPane.add(Box.createRigidArea(new Dimension(0, 75)));
                helpPane.add(checkMusic);
                mainScreen.revalidate();
                mainScreen.repaint();
            }
        });
        
        
        //check button to turn on/off music
        checkMusic.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (checkMusic.isSelected() == true) {
                    try {
                        bgMusic.playSound();
                    } catch (Throwable e1) {
                        e1.printStackTrace();
                    }
                } else if (checkMusic.isSelected() == false) {
                    bgMusic.stopSound();
                }
            }
        });
        
        
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Exit",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        });
    }

源码过长,我就不贴全了。

结尾语

源码仅供学习。


精品源码屋
6 声望12 粉丝

提供海量精品源码,业务范围:游戏,网站,工具等。 也支持招商代理