Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How a JPanel displays different colors from an array of colors?

The problem is when I set the background color of the square JPanel as square.setBackground(colors[j]) the square draws only the first color of the list of colors without displaying the other 3. This is my code:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;

@SuppressWarnings({ "unused", "serial" })

public class RegionPartition extends JFrame
{
    JLayeredPane layeredPane;
    JPanel regionBoard;
    JLabel regionPiece;

    private static int DELAY = 200;

    private Color[] colors = new Color[]{Color.PINK, Color.GREEN, Color.BLACK, Color.RED};

    public RegionPartition()
    {
        Dimension boardSize = new Dimension(500, 500);

        //  Use a Layered Pane for this this application
        layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(boardSize);

        regionBoard = new JPanel();

        layeredPane.add(regionBoard, JLayeredPane.DEFAULT_LAYER);

        regionBoard.setLayout( new GridLayout(4, 4) );

        regionBoard.setPreferredSize( boardSize );
        regionBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        Random random = new Random();


        for (int i = 0; i < 16; i++) {          
            JPanel square = new JPanel(new BorderLayout());
            square.setBorder(BorderFactory.createLineBorder(Color.black));
            regionBoard.add( square );  

            square.setBackground(Color.green);

            int j=0;

            square.setBackground(colors[j]);

            j++;
        }
    }

    {
        JPanel panel = new JPanel()  
        {  
            Clients[] c = new Clients[128];

            Random random = new Random();

            private final int SIZE = 450;
            private int DELAY = 9999999;
            public void paintComponent (Graphics g)
            {
                super.paintComponent(g);

                for (int i=0; i<c.length; i++)
                {
                    int x = ( int ) ( random.nextFloat() * SIZE ) + 10;
                    int y = ( int ) ( random.nextFloat() * SIZE ) + 10;

                    g.drawOval( x, y, 10, 10 );
                    g.fillOval(x, y, 10, 10);
                }

                for (int j=0; j<DELAY; j++)
                {
                    repaint();
                }
            }
        };  

        panel.setOpaque(false);  

        //Set the glass pane in the JFrame  
        setGlassPane(panel);  

        //Display the panel  

        panel.setVisible(true);  
    }

    public static void main(String[] args)
    {
        JFrame frame = new RegionPartition();

        frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
        frame.pack();
        frame.setResizable(true);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);

    }

    protected void paintComponent(Graphics g)
    {
        // TODO Auto-generated method stub
    }
}
like image 227
user1633202 Avatar asked Dec 16 '22 20:12

user1633202


2 Answers

That is because you are always setting j to 0 on each iteration:

    int j=0;

    square.setBackground(colors[j]);

    j++;

you may want to change j for an i or do a nested loop, that depends on what you really want to do here.

If you want to make all 16 squares have all four colors in a grid like manner, you might want to change your loop to something like:

     for (int i = 0; i < 16; i++) {          
        JPanel square = new JPanel(new GridLayout(2,2));
        square.setBorder(BorderFactory.createLineBorder(Color.black));
        regionBoard.add( square );  



        for(int j=0; j<4; ++j){
          JPanel insideSquare = new JPanel();
          insideSquare.setBackground(colors[j]);
          square.add(insideSquare);
        }
    }
like image 171
Sednus Avatar answered Dec 22 '22 01:12

Sednus


Because you only have 4 colors in your color array, but your loop index exceeds this, you could use:

square.setBackground(colors[ i % colors.length]);

to alternate the colors of your squares.

like image 26
Reimeus Avatar answered Dec 21 '22 23:12

Reimeus