JLabels / JPanel未显示或刷新

时间:2014-04-13 20:09:37

标签: java swing user-interface jlabel

我正在创建一个简单的游戏,玩家可以在2D地图上移动,但是我在显示地图时遇到问题,并且当玩家移动时地图会刷新。

到目前为止,GUI以下列形式获取地图:

X###X
#####
...G.
#####
X..GX

从服务器,这是在构造函数顶部的线程中完成的。然后将该线程传递给一个函数,该函数应该将显示正确图像的JLabel添加到JPanel,但是这似乎没有发生,到目前为止还没有显示任何内容。我的代码如下:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextPane;



public class dodGUI {

    public final Vector<String> messageQueue = new Vector<String>();
    public final Vector<String> responseQueue = new Vector<String>();

    final Queue<String> queue = new LinkedList<String>();
    final StringBuilder view = new StringBuilder();

    private JFrame frame = new JFrame();
    final JPanel mainMenu = new JPanel();
    final JPanel humanGameWindow = new JPanel();

    public static void main(String[] args) {
        dodGUI GUI = new dodGUI();
        GUI.frame.setVisible(true);
    }

    public void handleResponse(String response) {
        if(response.equals("WIN")) {
            JOptionPane.showMessageDialog(null,  "YOU WIN!!!");
            mainMenu.setVisible(true);
            humanGameWindow.setVisible(false);
        }

        if (queue.size() < 6) {
            queue.add(response);
        }
        else {
            queue.remove();
            queue.add(response);
        }
        if(queue.contains("LOOKREPLY") && !response.equals("LOOKREPLY")) {
            view.append(response);
        }

        if(view.length() == 25) {
            createImage(view.toString());
            view.delete(0,  25);
        }
    }

    public dodGUI() {

        new Thread(new Runnable() {
            public void run() {
                try {
                    while (true) {
                        String receiving = null;
                        synchronized (responseQueue) {
                            if (responseQueue.size() > 0) {
                                receiving = responseQueue.remove(0);
                            }
                        }
                        if (receiving != null) {
                            handleResponse(receiving);
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (NullPointerException npe2) {
                    npe2.printStackTrace();
                    System.err.println("Null Pointer2: " + npe2);
                }
            }
        }).start();

        frame.getContentPane().setBackground(new Color(255, 255, 255));
        frame.getContentPane().setLayout(null);
        frame.setBounds(100, 100, 500, 630);
        frame.setUndecorated(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //############################################################################################################################################################################

        mainMenu.setLayout(null);
        mainMenu.setBackground(Color.WHITE);
        mainMenu.setBounds(0, 0, 500, 600);
        mainMenu.setVisible(true);

        JLabel mainMenuTitle = new JLabel("DUNGEON OF DOOM!!");
        mainMenuTitle.setForeground(new Color(100, 149, 237));
        mainMenuTitle.setFont(new Font("Moire", Font.BOLD, 28));
        mainMenuTitle.setBounds(50, 13, 380, 50);
        mainMenu.add(mainMenuTitle);

        final JTextPane hostNameTextPane = new JTextPane();
        hostNameTextPane.setToolTipText("Enter the hostname");
        hostNameTextPane.setBackground(new Color(192, 192, 192));
        hostNameTextPane.setBounds(50, 100, 250, 30);
        hostNameTextPane.setFont(new Font("Moire", Font.BOLD, 19));
        mainMenu.add(hostNameTextPane);

        final JTextPane socketNumberTextPane = new JTextPane();
        socketNumberTextPane.setToolTipText("Enter the socket number");
        socketNumberTextPane.setBackground(new Color(192, 192, 192));
        socketNumberTextPane.setBounds(50, 175, 250, 30);
        socketNumberTextPane.setFont(new Font("Moire", Font.BOLD, 19));
        mainMenu.add(socketNumberTextPane);

        JTextPane userNameTextPane = new JTextPane();
        userNameTextPane.setToolTipText("Enter your Username");
        userNameTextPane.setBackground(new Color(192, 192, 192));
        userNameTextPane.setBounds(50, 250, 250, 30);
        userNameTextPane.setFont(new Font("Moire", Font.BOLD, 19));
        mainMenu.add(userNameTextPane);

        JButton mainMenuQuit = new JButton("Quit");
        mainMenuQuit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        mainMenuQuit.setBackground(new Color(100, 149, 237));
        mainMenuQuit.setBounds(220, 345, 70, 55);
        mainMenu.add(mainMenuQuit);

        JButton playGameHuman = new JButton("Play Game Human");
        final dodGUI gui = this;
        playGameHuman.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mainMenu.setVisible(false);
                humanGameWindow.setVisible(true);
                @SuppressWarnings("unused")
                ServerClient newclient = new ServerClient(gui, hostNameTextPane.getText(),
                        Integer.parseInt(socketNumberTextPane.getText()));
            }
        });
        playGameHuman.setBackground(new Color(100, 149, 237));
        playGameHuman.setBounds(50, 345, 150, 55);
        mainMenu.add(playGameHuman);

        frame.add(mainMenu);

        //############################################################################################################################################################################

        humanGameWindow.setLayout(null);
        humanGameWindow.setBackground(Color.WHITE);
        humanGameWindow.setBounds(0, 0, 500, 600);
        humanGameWindow.setVisible(false);

        JLabel gameTitle = new JLabel("DUNGEON OF DOOM!!");
        gameTitle.setForeground(new Color(100, 149, 237));
        gameTitle.setFont(new Font("Moire", Font.BOLD, 28));
        gameTitle.setBounds(93, 13, 380, 50);
        humanGameWindow.add(gameTitle);

        JButton up = new JButton("Up");
        up.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                synchronized (messageQueue) {
                    messageQueue.add("MOVE N");
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException a) {
                        a.printStackTrace();
                    }
                    messageQueue.add("LOOK");
                }
            }
        });
        up.setBackground(new Color(100, 149, 237));
        up.setBounds(274, 483, 100, 55);
        humanGameWindow.add(up);        

        JButton down = new JButton("Down");
        down.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                synchronized (messageQueue) {
                    messageQueue.add("MOVE S");
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException a) {
                        a.printStackTrace();
                    }
                    messageQueue.add("LOOK");
                }
            }
        });
        down.setBackground(new Color(100, 149, 237));
        down.setBounds(274, 540, 100, 55);
        humanGameWindow.add(down);

        JButton left = new JButton("Left");
        left.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                synchronized (messageQueue) {
                    messageQueue.add("MOVE W");
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException a) {
                        a.printStackTrace();
                    }
                    messageQueue.add("LOOK");
                }
            }
        });
        left.setBackground(new Color(100, 149, 237));
        left.setBounds(173, 520, 100, 55);
        humanGameWindow.add(left);

        JButton right = new JButton("Right");
        right.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                synchronized (messageQueue) {
                    messageQueue.add("MOVE E");
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException a) {
                        a.printStackTrace();
                    }
                    messageQueue.add("LOOK");
                }
            }
        });
        right.setBackground(new Color(100, 149, 237));
        right.setBounds(375, 520, 100, 55);
        humanGameWindow.add(right);

        JButton pickup = new JButton("Pickup");
        pickup.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                synchronized (messageQueue) {
                    messageQueue.add("PICKUP");
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException a) {
                        a.printStackTrace();
                    }
                    messageQueue.add("LOOK");
                }
            }
        });
        pickup.setBackground(new Color(100, 149, 237));
        pickup.setBounds(40, 520, 100, 55);
        humanGameWindow.add(pickup);

        JButton Exit = new JButton("Exit");
        Exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        Exit.setBackground(new Color(100, 149, 237));
        Exit.setBounds(427, 17, 70, 40);
        humanGameWindow.add(Exit);

        frame.add(humanGameWindow);
    }

        //##################################################################################################################################################

        public void createImage(String view) {

            final JPanel fiveByFiveLook = new JPanel();
            final JPanel sevenBySevenLook = new JPanel();

            String [] viewSplit = view.split("(?!^)");

            if(viewSplit.length == 25) {

                System.out.println("GOT HERE");
                fiveByFiveLook.setLayout(null);
                fiveByFiveLook.setBackground(Color.WHITE);
                fiveByFiveLook.setBounds(48, 75, 404, 404);
                fiveByFiveLook.setVisible(true);


                ArrayList<String> viewArray = new ArrayList<String>(Arrays.asList(viewSplit));


                JLabel fiveByFiveLabels[][] = new JLabel[5][5];

                for(int i = 0, k = 0; i <= 4; i++, k = k + 80) {
                    for(int j = 0, l = 0; j <= 4; j++, l = l + 80) {
                        fiveByFiveLabels[i][j] = new JLabel();
                        if(viewArray.get(0).equals("X")) {
                            fiveByFiveLabels[i][j].setIcon(new ImageIcon("Lantern.jpg"));
                            fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
                            viewArray.remove(0);
                        }
                        else if(viewArray.get(0).equals("#")) {
                            fiveByFiveLabels[i][j].setIcon(new ImageIcon("Wall.jpg"));
                            fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
                            viewArray.remove(0);
                        }
                        else if(viewArray.get(0).equals(".")) {
                            fiveByFiveLabels[i][j].setIcon(new ImageIcon("Floor.jpg"));
                            fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
                            viewArray.remove(0);
                        }
                        else if(viewArray.get(0).equals("E")) {
                            fiveByFiveLabels[i][j].setIcon(new ImageIcon("Exit.jpg"));
                            fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
                            viewArray.remove(0);
                        }
                        else if(viewArray.get(0).equals("G")) {
                            fiveByFiveLabels[i][j].setIcon(new ImageIcon("Gold.png"));
                            fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
                            viewArray.remove(0);
                        }
                        fiveByFiveLabels[i][j].setBounds(l, k, 85, 85);
                    }
                }
                humanGameWindow.add(fiveByFiveLook);
            }

            else if(viewSplit.length == 49) {

                sevenBySevenLook.setLayout(null);
                sevenBySevenLook.setBackground(Color.WHITE);
                sevenBySevenLook.setBounds(48, 75, 404, 404);
                sevenBySevenLook.setVisible(true);

                JButton sevenBySevenLabels[][] = new JButton[7][7];

                for(int i = 0, k = 0; i <= 6; i++, k = k + 57) {
                    for(int j = 0, l = 0; j <= 6; j++, l = l + 57) {
                        sevenBySevenLabels[i][j] = new JButton();
                        sevenBySevenLabels[i][j].setBounds(l, k, 62, 62);
                        //sevenBySevenLabels[i][j].setIcon(new ImageIcon("Sword copy.jpg"));
                        sevenBySevenLook.add(sevenBySevenLabels[i][j]);
                    }
                }
                humanGameWindow.add(sevenBySevenLook);
            }
        }
}

有人可以告知为什么会发生这种情况,非常感谢:)

P.S。对不起,长篇文章

0 个答案:

没有答案