Jump to content

Java Grid Help

- - - - -

  • Please log in to reply
3 replies to this topic

#1
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Is there any API or class in java with which i can simply draw a grid of x by y dimension and then i can paint any specific box?

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Here's an example using JLabels as box, it can also be a JPanel, canvas, button ... Anything

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridLayout;

import javax.swing.JFrame;

import javax.swing.JLabel;


public class MyGui extends JFrame {

    private JLabel[][] labels;


    public Menus1(){

        super();

        setLayout(new GridLayout(10,0));


        labels= new JLabel[10][10];


        for(int i=0 ; i<10 ; i++){

            for(int j=0 ; j<10 ; j++){

                labels[i][j] = new JLabel();

                labels[i][j].setPreferredSize(new Dimension(50,50));

                labels[i][j].setOpaque(true);  //Otherwise won't show background colors.

                add(labels[i][j]);

            }

        }


        for(int i=0, j=0 ; i<10 ; i++, j++){

            labels[i][j].setBackground(Color.RED);

        }


        pack();

        setVisible(true);

    }




    public static void main(String[] args) {

        MyGui gui = new MyGui ();

    }

}



#3
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
thanks , i have tried like that using JButtons , but the problem is that i have a 200 by 200 array , and that much buttons is kind of heavy i think. So thats why i asked for some API :) . thanks anyways if nothing works then i will have to stick with this :)

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Can always manually paint on 1 JPanel

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;


public class Menus1 extends JPanel {

    private boolean[][] values;


    public Menus1(){

        super();    

        values= new boolean[200][200];


        for(int i=0, j=0 ; i<100 ; i++, j++){

            values[i][j] = true;

        }

        for(int i=50, j=20 ; i<150 ; i++, j++){

            values[i][j] = true;

        }


        setPreferredSize(new Dimension(1000,1000));

    }


    @Override

    public void paintComponent(Graphics g) {

        int boxWidth = getWidth() / values.length;

        int boxHeight = getHeight() / values[0].length;


        for(int i=0 ; i<values.length ; i++){

            for(int j=0 ; j<values[i].length ; j++){

                if(values[i][j]){

                    g.setColor(Color.RED);

                } else {

                    g.setColor(Color.GREEN);

                }

                g.fillRect(i*boxWidth, j*boxHeight, boxWidth, boxHeight);

            }

        }

    }


    public static void main(String[] args) {

        JFrame frame = new JFrame();

        frame.add(new Menus1());

        frame.pack();

        frame.setVisible(true);

    }

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users