Reputation: 1
for(int i = 0; i < sizex/10; i++)
{
for(int u = 0; u < sizey/10; u++)
{
JPanel temp = new JPanel();
//temp.setSize(10, 10);
temp.setBounds(i*10,u*10, 10, 10);
//temp.setLocation(i*10, u*10);
Random r = new Random();
int rand = r.nextInt(4-0);
if(rand == 0)
{
temp.setBackground(Color.GREEN);
}
else if(rand == 1)
{
temp.setBackground(Color.BLUE);
}
else if(rand == 2)
{
temp.setBackground(Color.RED);
}
else if(rand == 3)
{
temp.setBackground(Color.MAGENTA);
}
frame.add(temp);
}
}
In my code here the logic behind it works(in my head) and this code works if I instead divide sizex
and y
by 100 and making the size of the box 100 instead of 10.
In the case that it is currently in it produces boxes which seem to be the right size but there are only a few down the side of the application instead of a full screen.
Here is a pic of the app:
Upvotes: 0
Views: 3026
Reputation: 2091
You can achieve this without using GridLayout
and simply by redefining the paintComponent
method of JPanel
component and filling it with random colors in sequential rectangles like this :
Here's the code used to achieve the above image :
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
@SuppressWarnings("serial")
public class RandomPaint extends JFrame
{
private JPanel panel;
private int dx, dy;
private Random random;
private Color color;
public RandomPaint()
{
dx = 50;
dy = 50;
random = new Random();
setBounds(100, 100, 500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel()
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (int i = 0; i < getHeight(); i = i + dx)
{
for (int j = 0; j < getWidth(); j = j + dy)
{
color = new Color(random.nextInt(255),
random.nextInt(255), random.nextInt(255));
g2d.setColor(color);
g2d.fillRect(j, i, dx, dy);
}
}
}
};
add(panel);
setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException | InstantiationException
| IllegalAccessException
| UnsupportedLookAndFeelException e)
{
}
new RandomPaint();
}
});
}
}
Upvotes: 2
Reputation: 347204
Use a GridLayout
...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class RandomCells {
public static void main(String[] args) {
new RandomCells();
}
public RandomCells() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridLayout(10, 10, 0, 0));
Random rnd = new Random();
Color[] colors = new Color[]{Color.GREEN, Color.BLUE, Color.RED, Color.MAGENTA};
for (int col = 0; col < 10; col++) {
for (int row = 0; row < 10; row++) {
JPanel cell = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(10, 10);
}
};
int color = rnd.nextInt(4);
cell.setBackground(colors[color]);
add(cell);
}
}
}
}
}
See How to Use GridLayout for more details
Use a GridBagLayout
...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class RandomCells {
public static void main(String[] args) {
new RandomCells();
}
public RandomCells() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
Random rnd = new Random();
Color[] colors = new Color[]{Color.GREEN, Color.BLUE, Color.RED, Color.MAGENTA};
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 0;
for (int col = 0; col < 10; col++) {
gbc.gridx = 0;
for (int row = 0; row < 10; row++) {
JPanel cell = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(10, 10);
}
};
int color = rnd.nextInt(4);
cell.setBackground(colors[color]);
add(cell, gbc);
gbc.gridx++;
}
gbc.gridy++;
}
}
}
}
See How to Use GridBagLayout for more details.
GridLayout
will always size it's components evenly so that they fill the available space, so if your resize the window for example, all the panels will change size in an attempt to fill the available space.
GridBagLayout
(in the configuration I've shown) will continue to honor the preferredSize
of the panels, so as you resize the window, the panels won't change size
Upvotes: 5
Reputation: 2167
Code:
JFrame frame = new JFrame("example");
frame.setSize(100,100);
frame.setLayout(new GridLayout(10,10));
for(int i = 0; i < 100/10; i++)
{
for(int u = 0; u < 100/10; u++)
{
JPanel temp = new JPanel();
temp.setBounds(i*10,u*10, 10, 10);
Random r = new Random();
int rand = r.nextInt(4-0);
if(rand == 0)
{
temp.setBackground(Color.GREEN);
}
else if(rand == 1)
{
temp.setBackground(Color.BLUE);
}
else if(rand == 2)
{
temp.setBackground(Color.RED);
}
else if(rand == 3)
{
temp.setBackground(Color.MAGENTA);
}
frame.add(temp);
}
frame.setVisible(true);
}
Upvotes: 0