1

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;
 }


}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Andrew
  • 29
  • 3
  • 8
  • Take a look at this post for an example of how to draw on JPanels.http://stackoverflow.com/questions/6118737/how-to-draw-in-jpanel-swing-graphics-java – WilliamShatner Feb 28 '13 at 20:41

1 Answers1

3

.

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);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • It's close to what i need but i need there to be no margins, and the boxes not change size the amount of boxes to grow and when i tried to implement that to the code it didn't work . – Andrew Mar 03 '13 at 18:44
  • call container.validate() & container.repaint() as last code lines, when all changes in the container (JFrame, JPanel e.i. ) are done, JFrame.pack() if required to change bounds based on preferredsize came from all JCOmponents layed in JFrame – mKorbel Mar 03 '13 at 18:47
  • what, i don't get it really – Andrew Mar 03 '13 at 18:58
  • isn't there an easier way as i have to incorporate it with – Andrew Mar 03 '13 at 22:00
  • isn't there a easier way as i have to include it along with http://stackoverflow.com/questions/14911958/java-selecting-objects-that-have-been-repainted – Andrew Mar 03 '13 at 22:00