Question: I have this code for 2d array demo but the rows and cols must be coming from user input someone help me to implement that

I have this code for 2d array demo but the rows and cols must be coming from user input someone help me to implement that

package swingDemo; 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 Array { public static void main(String[] args) {   new Array(); } public Array() { 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("Array"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public static class TestPane extends JPanel {  //how many squares across and down protected static final int ROWS = 15; protected static final int COLS = 15; protected static final int BOX_SIZE = 15; private List colors; //********************************this is the Color array****** Color[][] myColor = new Color[ROWS][COLS]; public TestPane() {  int length = ROWS * COLS;  colors = new ArrayList<>(length);  //these for loops create each color for the squares  for (int row = 0; row < ROWS; row++) {    for (int col = 0; col < COLS; col++) {      int c1 = (int) (Math.random() * 101); //these decide the random colors      int c2 = (int) (Math.random() * 101);      int c3 = (int) (Math.random() * 101);  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)) / 1; int yOffset = (getHeight() - (ROWS * BOX_SIZE)) / 1; for (int row = 0; row < ROWS; row++) {  for (int col = 0; col < COLS; col++) {    if(row == 0 || col == 0 || row == 14 || col == 14 || row == col){ //this if statement makes the color red on the outskirts and for the diagonal line      g2d.setColor(Color.blue);      g2d.fillRect(xOffset + (col * BOX_SIZE),      yOffset + (row * BOX_SIZE),      BOX_SIZE, BOX_SIZE);    }    else{ //this statement make a box with a random color      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(); } } }


 

Step by Step Solution

3.41 Rating (151 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To permit the client to include the quantity of lines and sections for the 2D cluster you can adjust the TestPane constructor to acknowledge these qua... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!