SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. protected static final int ROWS = 20;
  41. protected static final int COLS = 20;
  42. protected static final int BOX_SIZE = 30;
  43.  
  44. private List<Color> colors;
  45. Color[][] Background = new Color[ROWS][COLS];  ////????
  46. //********************************Y2D array is created htere******
  47. Color[][] myColor = new Color[ROWS][COLS];
  48.  
  49. public TestPane() {
  50. int length = ROWS * COLS;
  51. colors = new ArrayList<>(length);
  52.  
  53. for (int row = 0; row < ROWS; row++) {
  54.     for (int col = 0; col < COLS; col++) {
  55.         int c1 = (int) (Math.random() * 255);
  56.         int c2 = (int) (Math.random() * 255);
  57.         int c3 = (int) (Math.random() * 255);
  58.         myColor[row][col] = new Color(c1, c2, c3);
  59.         if (row==ROWS-1 || row==0){
  60.             myColor[row][col] = new Color (0,0,255);
  61.         }
  62.         if (col==COLS-1 || col==0){
  63.             myColor[row][col] = new Color (0,0,255);
  64.         }
  65.         if (col == row){
  66.             myColor[row][col] = new Color (0,0,255);
  67.  
  68.         }
  69.     }}
  70. }
  71.  
  72. @Override
  73. public Dimension getPreferredSize() {
  74. return new Dimension(COLS * BOX_SIZE, ROWS * BOX_SIZE);
  75. }
  76.  
  77. @Override
  78. protected void paintComponent(Graphics g) {
  79. super.paintComponent(g);
  80. Graphics2D g2d = (Graphics2D) g.create();
  81.  
  82. int xOffset = (getWidth() - (COLS * BOX_SIZE)) / 2;
  83. int yOffset = (getHeight() - (ROWS * BOX_SIZE)) / 2;
  84.  
  85.  
  86. for (int row = 0; row < ROWS; row++) {
  87. for (int col = 0; col < COLS; col++) {
  88. int index = (row * COLS) + col;
  89. g2d.setColor(myColor[row][col]);
  90. g2d.fillRect(xOffset + (col * BOX_SIZE),
  91. yOffset + (row * BOX_SIZE),
  92. BOX_SIZE, BOX_SIZE);
  93. }
  94. }
  95. g2d.dispose();
  96. }
  97.  
  98. }
  99.  
  100. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top