Guest User

Untitled

a guest
Feb 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.EventQueue;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10. import javax.swing.UIManager;
  11. import javax.swing.UnsupportedLookAndFeelException;
  12.  
  13. public class ColoredBoxes2 {
  14.  
  15. public static void main(String[] args) {
  16. new ColoredBoxes2();
  17. }
  18.  
  19. public ColoredBoxes2() {
  20. EventQueue.invokeLater(new Runnable() {
  21. @Override
  22. public void run() {
  23. try {
  24. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  25. } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
  26. ex.printStackTrace();
  27. }
  28.  
  29. JFrame frame = new JFrame("Testing");
  30. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31. frame.add(new TestPane());
  32. frame.pack();
  33. frame.setLocationRelativeTo(null);
  34. frame.setVisible(true);
  35. }
  36. });
  37. }
  38.  
  39. public static class TestPane extends JPanel {
  40.  
  41. // dimensions of the box
  42. // changed rows to 20 to make a square
  43. protected static final int ROWS = 20;
  44. protected static final int COLS = 20;
  45. protected static final int BOX_SIZE = 30;
  46.  
  47. private List<Color> colors;
  48. // initialize the 2D array
  49. Color[][] myColor = new Color[ROWS][COLS];
  50.  
  51. public TestPane() {
  52. int length = ROWS * COLS;
  53. colors = new ArrayList<>(length);
  54.  
  55. for (int row = 0; row < ROWS; row++) {
  56. for (int col = 0; col < COLS; col++) {
  57.  
  58. // rgb triple values
  59. // initialize them randomly to get random colors
  60. int c1 = (int) (Math.random() * 255);
  61. int c2 = (int) (Math.random() * 255);
  62. int c3 = (int) (Math.random() * 255);
  63.  
  64. // if rows is equal to columns then set color of square to red
  65. // in order to get a red diagonal from top left to bottom right
  66. // also if rows = 0 or length - 1 or columns = 0 or length - 1
  67. // then set square to red to make a red border
  68. if (row == 0 || col == 0 || row == ROWS - 1 || col == COLS - 1
  69. || row == col)
  70. {
  71. // make the border and diagonal all red
  72. c1 = 255;
  73. c2 = 0;
  74. c3 = 0;
  75. }
  76.  
  77. // set the color at each specific point in board to respective RGB
  78. myColor[row][col] = new Color(c1, c2, c3);
  79. }
  80. }
  81. }
  82.  
  83. @Override
  84. public Dimension getPreferredSize() {
  85. return new Dimension(COLS * BOX_SIZE, ROWS * BOX_SIZE);
  86. }
  87.  
  88. @Override
  89. protected void paintComponent(Graphics g) {
  90. super.paintComponent(g);
  91. Graphics2D g2d = (Graphics2D) g.create();
  92.  
  93. int xOffset = (getWidth() - (COLS * BOX_SIZE)) / 2;
  94. int yOffset = (getHeight() - (ROWS * BOX_SIZE)) / 2;
  95.  
  96.  
  97. for (int row = 0; row < ROWS; row++) {
  98. for (int col = 0; col < COLS; col++) {
  99. int index = (row * COLS) + col;
  100. g2d.setColor(myColor[row][col]);
  101. g2d.fillRect(xOffset + (col * BOX_SIZE),
  102. yOffset + (row * BOX_SIZE),
  103. BOX_SIZE, BOX_SIZE);
  104. }
  105. }
  106. g2d.dispose();
  107. }
  108.  
  109. }
  110.  
  111. }
Add Comment
Please, Sign In to add comment