Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 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. //may need to change values below for step 2***********
  42. protected static final int ROWS = 20;
  43. protected static final int COLS = 20;
  44. protected static final int BOX_SIZE = 30;
  45.  
  46. private List<Color> colors;
  47. //********************************Y2D array is created htere******
  48. Color[][] myColor = new Color[ROWS][COLS];
  49.  
  50. public TestPane() {
  51. int length = ROWS * COLS;
  52. colors = new ArrayList<>(length);
  53.  
  54. for (int row = 0; row < ROWS; row++) {
  55. for (int col = 0; col < COLS; col++) {
  56. int c1 = (int) (Math.random() * 255);
  57. int c2 = (int) (Math.random() * 255);
  58. int c3 = (int) (Math.random() * 255);
  59. //********************************YOU WILL NEED TO ADD CODE INSIDE THE For LOOPs Here******
  60. myColor[row][col] = new Color(c1, c2, c3);
  61.  
  62. if(row==ROWS-1 || row==0){
  63. myColor[row][col] = new Color(255, 0, 0);
  64. }if(col==COLS-1 || col==0){
  65. myColor[row][col] = new Color(255, 0, 0);
  66. }if(col==COLS-1 || col==0){
  67. myColor[row][col] = new Color(255, 0, 0);
  68. }if(col==row){
  69. myColor[row][col] = new Color(255, 0, 0);
  70. }
  71. }
  72. }
  73. }
  74. @Override
  75. public Dimension getPreferredSize() {
  76. return new Dimension(COLS * BOX_SIZE, ROWS * BOX_SIZE);
  77. }
  78.  
  79. @Override
  80. protected void paintComponent(Graphics g) {
  81. super.paintComponent(g);
  82. Graphics2D g2d = (Graphics2D) g.create();
  83.  
  84. int xOffset = (getWidth() - (COLS * BOX_SIZE)) / 2;
  85. int yOffset = (getHeight() - (ROWS * BOX_SIZE)) / 2;
  86.  
  87.  
  88. for (int row = 0; row < ROWS; row++) {
  89. for (int col = 0; col < COLS; col++) {
  90. int index = (row * COLS) + col;
  91. g2d.setColor(myColor[row][col]);
  92. g2d.fillRect(xOffset + (col * BOX_SIZE),
  93. yOffset + (row * BOX_SIZE),
  94. BOX_SIZE, BOX_SIZE);
  95. }
  96. }
  97. g2d.dispose();
  98. }
  99.  
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement