Results 1 to 3 of 3
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Trying to print a two dimensional matrix on Jpanel

    Hello everyone, I am currently studying Swing independently, and having trouble with a simple thing, taking a two dimensional array and print it on the graphical interface.
    At the moment the values aren't important, I just want it to draw squares for each cell in the array. Paint component works fine by itself, however when I call the method I created to draw additional squares, nothing appears on the panel.

    Thank you for any help, this is not homework I'm self studying.

    Java Code:
    package gmatrix;
    
    import javax.swing.JFrame;
    
    public class T {
    
    	public static void main(String[] args) {
    		JFrame frame = new JFrame("Graphical Matrix");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(800, 800);
    		
    		M mPanel = new M();
    		frame.add(mPanel);
    		frame.setVisible(true);
    		
    		
    		int [][] m = new int [4][4];
    				
    		mPanel.matrixToG(m);
    
    	}
    
    }
    Java Code:
    package gmatrix;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    import javax.swing.JPanel;
    
    public class M extends JPanel{
    	
    	private final int WIDTH = 50;
    	private final int HEIGHT = 50;
    	private int xLoc=0; 
    	private int yLoc=0;
    	Color curColor = Color.RED;
    	
    	public M (){
    		
    	}
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		
    		g.setColor(curColor);
    		g.fillRect(xLoc, yLoc, HEIGHT, WIDTH);
    	}
    	
    	
    	public void matrixToG (int [][] matrix){
    		for(int i=0; i<matrix.length; i++){
    			for(int j=0; j<matrix[0].length; j++){
    				repaint();
    				xLoc+=50; yLoc+=50;				
    			}
    		}
    		
    	}

  2. #2
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Trying to print a two dimensional matrix on Jpanel

    Quick fix on the for loop

    public void matrixToG (int [][] matrix){
    for(int i=0; i<matrix.length; i++){
    for(int j=0; j<matrix[0].length; j++){
    repaint();
    xLoc+=50;
    }
    yLoc+=50;
    }

    }

  3. #3
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Trying to print a two dimensional matrix on Jpanel

    I got it, never mind thank anyway

Similar Threads

  1. Replies: 1
    Last Post: 01-06-2014, 04:45 PM
  2. to print a matrix recursively until all its elemets are zero
    By priyankakusuma in forum New To Java
    Replies: 1
    Last Post: 03-24-2013, 08:00 AM
  3. print out jpanel
    By slamdon in forum AWT / Swing
    Replies: 1
    Last Post: 03-13-2013, 08:15 PM
  4. two dimensional array print response
    By bayan in forum New To Java
    Replies: 7
    Last Post: 04-27-2010, 05:43 AM
  5. two dimensional array print
    By Myles in forum New To Java
    Replies: 0
    Last Post: 04-26-2010, 11:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •