Search code examples
javaswingjpanelpaintcomponentpreferredsize

Java, Grid of Objects


I need help with drawing the grids to the GUI as well as the program later letting me change the colour of the boxes drawn. I know i will have to use paintComponent(Graphics g), but i have no idea how or where.

So here is a copy of the code i have got so far ( even though i have been told it can be quite daunting just being given code i think it is the best way for people to help and not just do it for me). From the top it sets values, creates the GUI, calls the GUI, fills a 2d array with boxes( i think). Then in the Boxes class setting values the boxes class will need, then the start of how to draw them (didn't know how to work it out), then some seta methods for the x and y coordinates.

what i would like you to do is show how to have the boxes be drawn to the Jpanel, to make a grid and then to show me how to change the colour to different shades of blue, depending on a external value.

import java.awt.*;
import java.awt.Graphics;
import java.util.*;
import javax.swing.*;

public class NewGrid {

   Boxes[][] Boxs;
   int BoxesX;
   int BoxesY;

   NewGrid() {
      buildtheGUI();

   }
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();

    public void buildtheGUI() {
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
         new NewGrid();
    }

    public void addboxes() {
        Boxs = new Boxes[panel.getWidth() / 10][panel.getHeight() / 10];
        for (int i = 0; i < panel.getWidth() / 10; i++) {
           for (int j = 0; j < panel.getHeight() / 10; j++) {
               Boxs[i][j] = new Boxes();
               Boxs[i][j].setx(i * (panel.getWidth() / 10));
               Boxs[i][j].sety(j * (panel.getHeight() / 10));
               Boxs[i][j].draw(null);
           }

       }
   }
}


public class Boxes extends JPanel {
int x;
int y;
int width = 10;
int hieight = 10;
Color colour = Color.BLACK;




 public void draw(Graphics g) {
    g.setColor(colour);
    g.fillRect(x, y, width, hieight);
}
 public void setx(int i ){
     x = i;
 }
 public void sety(int i ){
     y = i;
 }


}

Solution

  • .

    JComponent comp = event.getComponent();
    String strRow = (String) comp.getClientProperty("row");
    String strColumn = (String) comp.getClientProperty("column");
    

    simple code

    import java.awt.*;
    import java.awt.Graphics;
    import java.util.*;
    import javax.swing.*;
    
    public class NewGrid {
    
        private int row = 10;
        private int column = 10;
        private JFrame frame = new JFrame();
        private JPanel panel = new JPanel();
    
        private NewGrid() {
            addboxes();
            panel.setLayout(new GridLayout(row, column));
            frame.add(panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    
        private void addboxes() {
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < column; j++) {
                    Boxes box = new Boxes();
                    box.putClientProperty("row", row);
                    box.putClientProperty("column", column);
                    panel.add(box);
                }
            }
        }
    
        public static void main(String[] args) {
            Runnable doRun = new Runnable() {
                @Override
                public void run() {
                    new NewGrid();
                }
            };
            SwingUtilities.invokeLater(doRun);
        }
    }
    
    class Boxes extends JPanel {
    
        private static final long serialVersionUID = 1L;
    
        @Override
        public Dimension getMinimumSize() {
            return new Dimension(20, 20);
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(20, 20);
        }
    
        @Override
        public Dimension getMaximumSize() {
            return new Dimension(20, 20);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            int margin = 2;
            Dimension dim = getSize();
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fillRect(margin, margin, dim.width - margin * 2, 
               dim.height - margin * 2);
        }
    }