954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to draw a grid of rectangles in swing ?

Hello people

I have a JFrame which container is a JPanel: can you give me a piece of code that shows how to create a grid of rectangles ?

I need this information for my poject
Thank you

begueradj
Junior Poster in Training
70 posts since Mar 2007
Reputation Points: 9
Solved Threads: 0
 

What is to go in the rectangles? You can place JLabels in a GridLayout and give them a line border if you want a grid of actual components.

Or you can simply override paintComponent() and draw lines on your JPanel.

The Oracle tutorials offer more information about both of those possibilities.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Hello people

I have a JFrame which container is a JPanel: can you give me a piece of code that shows how to create a grid of rectangles ?

I need this information for my poject Thank you


Thank you,

In fact, I want to draw that grid according to matrix data: I succeeded to do that, but I want to draw a rectangle in black if my matrix element is 1, and in white if my matrix element is 0. I did as follows but it draws all the small rectangles with the the color that corresponds to the last element of my matrix. Can you tell me how I can modify my following code to do that , please ?

package aiproject;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Random;


public class DrawWelcomeWindow extends JLabel{
	protected JFrame frame;
	protected DrawLabyrinthFromMatrix DessinLab;
	protected JMenuBar jmBar;
	
	protected JMenu jmHelp;
	protected JMenuItem jmiAbout;
	protected JMenu jmFile;
	protected JMenuItem jmiExit;
	protected JPanel jpanel;
	//protected JButton jb;
	StoreFileDataIntoIntegerMatrix sfm= new StoreFileDataIntoIntegerMatrix();
	protected int lescolonnes;
	protected int leslignes;
	protected int[][] lamatrice;
	protected int[][] thematrix;
	protected int hauteur=20;
	protected int largeur=20;
	protected int x;
	protected int y;
	private static final Random random = new Random();
	int b=1;
	public DrawWelcomeWindow(){
		sfm.storeIntoArray();
		sfm.storeAsIntegersIntoMatrix();
		lescolonnes=sfm.getColumnsNb();
		leslignes=sfm.getRowsNb();
		lamatrice=sfm.getLabyrinthMatrix();
		
		this.setOpaque(true);
		for(int b1=0;b1<leslignes;b1++){
			for(int b2=0;b2<lescolonnes;b2++){
				switch(lamatrice[b1][b2]){
					case 1:
						this.setBackground(Color.black);break;
					case 0:
						this.setBackground(Color.blue);break;
					case -1:
						this.setBackground(Color.red);break;
				}
			}
		}
		//this.setBackground(new Color(random.nextInt()));
		this.setPreferredSize(new Dimension(this.leslignes,lescolonnes));
	}
	
	
	/*
	 *draw my user interface
	 * */
	public  void initUI(){
		frame=new JFrame("Projet IA");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new GridLayout(leslignes, lescolonnes));
		for(int i=0;i<leslignes*lescolonnes;i++){
			DrawWelcomeWindow label=new DrawWelcomeWindow();
			frame.add(label);
		 
		}
		//DessinLab=new DrawLabyrinthFromMatrix();
		//jpanel=new JPanel();
		//jb=new JButton();
		//jpanel.add(jb);
		//frame.setContentPane(jpanel);
		//jpanel.setBackground(Color.BLACK);
		jmBar=new JMenuBar();
		
		ImageIcon iconExit=new ImageIcon(getClass().getResource("exit.jpeg"));
		ImageIcon iconAbout=new ImageIcon(getClass().getResource("help.png"));
		
		
		jmFile=new JMenu("File");
		jmFile.setMnemonic('F');
		jmHelp=new JMenu("Help");
		jmHelp.setMnemonic('H');
		
		jmiExit=new JMenuItem("Exit",iconExit);
		jmiExit.setMnemonic('E');
		jmiAbout=new JMenuItem("About",iconAbout);
		jmiAbout.setMnemonic('A');
		
		
		jmiExit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.exit(0);
			}
		});	
		
		jmiAbout.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				JOptionPane.showMessageDialog(null,"Billal BEGUERADJ & Mohammed EL BARBORI Master CRYPTIS 1ère Année","                            Projet IA 2011/2012",JOptionPane.PLAIN_MESSAGE);
			}
		});
		
		
		
		jmFile.add(jmiExit);
		jmHelp.add(jmiAbout);
		jmBar.add(jmFile);
		jmBar.add(jmHelp);		
				
		//DessinLab.paintComponent(Graphics g);
		frame.setJMenuBar(jmBar);
		frame.setSize(600, 600);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
	}/*END OF initUI() */
}//END OF my class
begueradj
Junior Poster in Training
70 posts since Mar 2007
Reputation Points: 9
Solved Threads: 0
 

Basically, you just need to set your label background colors when you create and place them in your frame. So in initUI() you can use something similar to the loop you put in the constructor

for(int b1=0;b1<leslignes;b1++){
   for(int b2=0;b2<lescolonnes;b2++){
      JLabel label = new JLabel();
      label.setOpaque(true);
      switch(lamatrice[b1][b2]){
	 case 1: label.setBackground(Color.black);
                 break;
         case 0: label.setBackground(Color.blue);
                 break;
         case -1: label.setBackground(Color.red);
                  break;
      }
      frame.add(label);
   }
}


There is no reason for your class to extend JLabel.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Basically, you just need to set your label background colors when you create and place them in your frame. So in initUI() you can use something similar to the loop you put in the constructor

for(int b1=0;b1<leslignes;b1++){
   for(int b2=0;b2<lescolonnes;b2++){
      JLabel label = new JLabel();
      label.setOpaque(true);
      switch(lamatrice[b1][b2]){
	 case 1: label.setBackground(Color.black);
                 break;
         case 0: label.setBackground(Color.blue);
                 break;
         case -1: label.setBackground(Color.red);
                  break;
      }
      frame.add(label);
   }
}
There is no reason for your class to extend JLabel.


Thank you very much !!!!! It works !!!!!!!!!!!!!!!
Thank you very much for your help

begueradj
Junior Poster in Training
70 posts since Mar 2007
Reputation Points: 9
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: