Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.EventQueue;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- public class ColoredBoxes2 {
- public static void main(String[] args) {
- new ColoredBoxes2();
- }
- public ColoredBoxes2() {
- 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 static class TestPane extends JPanel {
- // dimensions of the box
- // changed rows to 20 to make a square
- protected static final int ROWS = 20;
- protected static final int COLS = 20;
- protected static final int BOX_SIZE = 30;
- private List<Color> colors;
- // initialize the 2D array
- Color[][] myColor = new Color[ROWS][COLS];
- public TestPane() {
- int length = ROWS * COLS;
- colors = new ArrayList<>(length);
- for (int row = 0; row < ROWS; row++) {
- for (int col = 0; col < COLS; col++) {
- // rgb triple values
- // initialize them randomly to get random colors
- int c1 = (int) (Math.random() * 255);
- int c2 = (int) (Math.random() * 255);
- int c3 = (int) (Math.random() * 255);
- // if rows is equal to columns then set color of square to red
- // in order to get a red diagonal from top left to bottom right
- // also if rows = 0 or length - 1 or columns = 0 or length - 1
- // then set square to red to make a red border
- if (row == 0 || col == 0 || row == ROWS - 1 || col == COLS - 1
- || row == col)
- {
- // make the border and diagonal all red
- c1 = 255;
- c2 = 0;
- c3 = 0;
- }
- // set the color at each specific point in board to respective RGB
- myColor[row][col] = new Color(c1, c2, c3);
- }
- }
- }
- @Override
- public Dimension getPreferredSize() {
- return new Dimension(COLS * BOX_SIZE, ROWS * BOX_SIZE);
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2d = (Graphics2D) g.create();
- int xOffset = (getWidth() - (COLS * BOX_SIZE)) / 2;
- int yOffset = (getHeight() - (ROWS * BOX_SIZE)) / 2;
- for (int row = 0; row < ROWS; row++) {
- for (int col = 0; col < COLS; col++) {
- int index = (row * COLS) + col;
- g2d.setColor(myColor[row][col]);
- g2d.fillRect(xOffset + (col * BOX_SIZE),
- yOffset + (row * BOX_SIZE),
- BOX_SIZE, BOX_SIZE);
- }
- }
- g2d.dispose();
- }
- }
- }
Add Comment
Please, Sign In to add comment