0
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JPanel;

public class Grid extends JComponent
{

    public void paint(Graphics g){

        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g;
        int w = 1024*2;
        int h = 1024*2;

        for(int i=0; i<1024; i++)
        {
            graphics.drawLine(i, 0, i, 1024);
            //graphics.setColor(Color.red);

        }

        for(int j=0; j<1024; j++)
        {
            graphics.drawLine(0, j, 1024, j);
        }

    }

}

I need to draw 1024 cross 1024 cells and color few cells. The cells should be displayed on a JFrame. What is the best way to do it in java?. Please post some code...

adrian
  • 1,439
  • 1
  • 15
  • 23

2 Answers2

3

You could use some JTable features:

class CellCoords{
    public int x, y;
    public CellCoords(x, y){
        this.x = x; this.y = y;
    }
}
TableModel dataModel = new AbstractTableModel() {
    public int getColumnCount() { return 1024; }
    public int getRowCount() { return 1024;}
    public Object getValueAt(int row, int col) { return new CellCoords(row, col); }
};
JTable table = new JTable(dataModel);

More examples from the Swing Tutorials

public class ColorRenderer extends JLabel
                           implements TableCellRenderer {
    ...
    public ColorRenderer(boolean isBordered) {
        this.isBordered = isBordered;
        setOpaque(true); //MUST do this for background to show up.
    }

    public Component getTableCellRendererComponent(
                            JTable table, Object color,
                            boolean isSelected, boolean hasFocus,
                            int row, int column) {
        // Do things based on row and column to decide color
        Color newColor = (Color)color;
        setBackground(newColor);

        return this;
    }
}

Generally, the How to Use Tables documentation will help out a lot.

Jeromy French
  • 11,812
  • 19
  • 76
  • 129
David Souther
  • 8,125
  • 2
  • 36
  • 53
  • @ Hovercraft Full Of Eels my requirement is a 1024 cross 1024 grid and to color a few. I tried JPanel with grid layout. But every time u create a new Jpanel it takes a lot of memory in heap. Imagine 1024 * 1024 instances in heap and eventually app hangs or slowes down –  May 17 '12 at 18:12
  • @ Hovercraft Full Of Eels sorry its not 1024 cross 1024 grid. But i need 1024 cross 1024 cells in a Jframe. I will edit my question –  May 17 '12 at 18:20
  • @RaghunandanKavi did you mean to post those comments on your question? Because creating a table will be much smaller than panels in a grid layout. – David Souther May 17 '12 at 18:38
  • @ David Souther i have considered your suggestion. However i have a jtable already in my app. The Jtable is scrollable. So depending on values set from jtable i need a view that shows exactly which cell has values. Something like a topView of a Jtable. –  May 17 '12 at 18:44
  • Kill the jScollable and set your column widths much smaller (1 px) – David Souther May 17 '12 at 18:48
  • @ David Souther wen u look at the Jframe u should know which cells have values set in jtable without scrolling. –  May 17 '12 at 18:48
  • @ David Souther i have been asked to show grids by my guide. 95% of the projects done except this 1024 cross 1024 cells in a Jframe. I don't know wat they call grids or cells. –  May 17 '12 at 18:52
  • I'm still not sure what effect exactly you're trying to achieve. Could you provide a link to project that uses something similar? – David Souther May 17 '12 at 18:53
  • @ David Souther i will make it simple. I have a Jtable whose values r set. Some cells are colored based on some calculation. Now the jtable has 1024 cross 1024 cells in it. Some cells are colored. Jtalbe is scrollable. My guide needs a topview. U shud be able to find out which cells are colored @ a glance. –  May 17 '12 at 18:57
  • So you need a control that is 1024x1024 pixels, with each pixel set according to the correlated cell in the full table. You want to use Vizier's answer; create a single jcomponent and override the paint() method. – David Souther May 17 '12 at 18:59
  • @ David Souther yes. Each pixel as a cell. How do i implement this? I went through Vizier's post. Can u give me some more input on this. –  May 17 '12 at 19:03
  • I would write the new swing component, have it implement the TableListener interface, and use table selection events to manage an internal bitmap of selected pixels. The paint() method would render that map. – David Souther May 17 '12 at 19:11
1

I suggest developing a custom swing component. Then add this component to a JFrame.

Creating a custom component is a lot easier than it sounds. Just create a new class extending JComponent or Component and override paint(Graphics) method.

And in the paint method just use a for loop to draw the grid using Graphics methods drawLine, fillRect etc. It is very easy and flexible.

This gives you a good start, then you can work on resizing, scrolling etc as required.

Hakan Serce
  • 11,198
  • 3
  • 29
  • 48
  • My guide suggested to open a bmp image and draw lines on top of image. So u can scale image. does this idea work for forming 1024 cross 1024 cells –  May 18 '12 at 02:05
  • For the question you asked, yo do not require opening an image. Just put two for loops nested for columns and rows of the grid, and use the methods of Graphics class. – Hakan Serce May 18 '12 at 09:53
  • vizier i tried ur idea. I don't know how to implement. Can u plz post implemented code. A sample code.... –  May 21 '12 at 14:24