0

Whenever a new connect four piece label is placed, the previous one disappears. How do I ensure that all are continuously visible?

JFrame method

import javax.swing.JFrame; 
import javax.swing.SwingUtilities;

public class C4_JFrame {
public static void main(String[] argS) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new Connect_Four("Connect Four");
            frame.setSize(1400, 775);
            frame.setLayout(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.setResizable(false);
        }
    });

}

}

Main method (turn method, where labels are dealt, is at the end)

    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;

    public class Connect_Four extends JFrame {
    static JLabel background, label1, label2, label3, label4, label5, label6, label7, label8, label9;
    static JLabel[] piecelabel;
    static int piececounter;
    static boolean turn_flag;
    private static final long serialVersionUID = 1L;

    public Connect_Four(String title) {
        String board[][];
        board = new String[7][6];
        int counter[];
        piececounter = 0;
        turn_flag = false;
        counter = new int[7];
        for (int x = 0; x < 7; x++) {
            counter[x] = 0;
            for (int y = 0; y < 6; y++) {
                board[x][y] = "empty";
            }
        }

        JPanel panel = new JPanel();
        setContentPane(panel);
        getLayeredPane();

        JTextArea display = new JTextArea();
        display.setBounds(1175, 550, 190, 30);
        Font font = new Font("Verdana", Font.BOLD, 12);
        display.setFont(font);
        display.setText("Too many pieces in that row\nPlease select a different row");
        panel.add(display);
        display.setVisible(false);

        background = new JLabel();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        label9 = new JLabel();

        background.setIcon(new ImageIcon("F:/Connect4Board.png"));
        background.setBounds(0, 100, 1175, 650);
        background.setOpaque(false);
        panel.setLayout(null);
        panel.add(background);

        // label1-label7 are all the same image
        label1.setIcon(new ImageIcon("F:/arrow.jpg"));
        label1.setBounds(75, 20, 40, 40);
        panel.add(label1);

        label2.setIcon(new ImageIcon("F:/arrow.jpg"));
        label2.setBounds(240, 20, 40, 40);
        panel.add(label2);

        label3.setIcon(new ImageIcon("F:/arrow.jpg"));
        label3.setBounds(405, 20, 40, 40);
        panel.add(label3);

        label4.setIcon(new ImageIcon("F:/arrow.jpg"));
        label4.setBounds(580, 20, 40, 40);
        panel.add(label4);

        label5.setIcon(new ImageIcon("F:/arrow.jpg"));
        label5.setBounds(755, 20, 40, 40);
        panel.add(label5);

        label6.setIcon(new ImageIcon("F:/arrow.jpg"));
        label6.setBounds(930, 20, 40, 40);
        panel.add(label6);

        label7.setIcon(new ImageIcon("F:/arrow.jpg"));
        label7.setBounds(1109, 20, 40, 40);
        panel.add(label7);

        label8.setIcon(new ImageIcon("F:/yourturn.png"));
        label8.setBounds(1175, 300, 190, 240);
        panel.add(label8);

        label9.setIcon(new ImageIcon("F:/blackturn.png"));
        label9.setBounds(1175, 300, 190, 240);
        panel.add(label9);

        piecelabel = createLabels();

        JButton row1 = new JButton();
        JButton row2 = new JButton();
        JButton row3 = new JButton();
        JButton row4 = new JButton();
        JButton row5 = new JButton();
        JButton row6 = new JButton();
        JButton row7 = new JButton();

        panel.add(row1);
        row1.setBounds(75, 20, 40, 40);
        row1.setOpaque(false);

        panel.add(row2);
        row2.setBounds(240, 20, 40, 40);
        row2.setOpaque(false);

        panel.add(row3);
        row3.setBounds(405, 20, 40, 40);
        row3.setOpaque(false);

        panel.add(row4);
        row4.setBounds(580, 20, 40, 40);
        row4.setOpaque(false);

        panel.add(row5);
        row5.setBounds(755, 20, 40, 40);
        row5.setOpaque(false);

        panel.add(row6);
        row6.setOpaque(false);
        row6.setBounds(930, 20, 40, 40);

        panel.add(row7);
        row7.setBounds(1109, 20, 40, 40);
        row7.setOpaque(false);

        row1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int x = 0;
                turn(x, counter, board, panel, display);

            }
        });

        row2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int x = 1;
                turn(x, counter, board, panel, display);

            }

        });
        row3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int x = 2;
                turn(x, counter, board, panel, display);

            }

        });
        row4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int x = 3;
                turn(x, counter, board, panel, display);

            }

        });
        row5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int x = 4;
                turn(x, counter, board, panel, display);

            }
        });
        row6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int x = 5;
                turn(x, counter, board, panel, display);

            }
        });
        row7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int x = 6;
                turn(x, counter, board, panel, display);

            }
        });
    }

    public JLabel[] createLabels() {
        JLabel[] labels = new JLabel[42];
        for (int i = 0; i < 42; i++) {
            labels[i] = new JLabel();
        }
        return labels;
    }

    public static void turn(int x, int counter[], String board[][], JPanel panel, JTextArea display) {
        if (counter[x] < 6) {
            display.setVisible(false);
            if (turn_flag == false) {
                board[x][counter[x]] = "red";
                piecelabel[piececounter].setIcon(new ImageIcon("F:/redpiece.png"));
                turn_flag = true;
                label8.setVisible(false);
            }

            else if (turn_flag == true) {
                board[x][counter[x]] = "black";
                piecelabel[piececounter].setIcon(new ImageIcon("F:/blackpiece.jpg"));
                turn_flag = false;
                label8.setVisible(true);
            }
            piecelabel[piececounter].setBounds(25 + (x * 169), (650 - (counter[x] * 109)), 135, 100);

            panel.add(piecelabel[piececounter]);
            panel.setComponentZOrder(piecelabel[piececounter], 0);
            // adding Z order so the buttons are in front of the board
            panel.setComponentZOrder(background, 1);

            panel.repaint(); // makes the first connect four piece visible
                                // (didn't appear for some reason)
            if(piececounter==1)
            counter[x] = counter[x] + 1;
            piececounter++; 
            System.out.println(piececounter);                               
        } else
            display.setVisible(true);
    }
}
Bredward
  • 7
  • 3
  • 6
    I would do things completely differently from what it appears that you are doing. I would place all my JLabels into the GUI on program start up, placing them in a JPanel that uses GridLayout, I'd give all my token JLabels an ImageIcon that has a blank image on it. And then when I wanted to place a color disk in a location, I'd swap Icons, placing a colored disk icon where the blank one was. I'd avoid null layouts like the plague, I'd try to separate program logic from GUI as much as possible. – Hovercraft Full Of Eels Jun 21 '16 at 16:45
  • 1
    `frame.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jun 22 '16 at 02:06
  • .................hello? – Hovercraft Full Of Eels Jun 22 '16 at 21:42
  • I ended up just removing the background and adding it again since there was problems with overriding the initial placement of the label. – Bredward Jun 26 '16 at 15:01

0 Answers0