Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ChessBoardView with JLabel squares doesnt fill the JPanel

  1. #1
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default ChessBoardView with JLabel squares doesnt fill the JPanel

    Hi guys

    i am working on a chessboard. I want to fill the JPanel squares (64) with ImageIcons. When i use the Jpanel setBackgroundColor it works just fine. But when i want to use a JLabel with image i get the wrong result
    this is what is looks like and it should fill the whole board with squares.

    chessboard.jpg

    The following is my code, what am i doing wrong?

     
    package view;
     
    import java.awt.*;
     
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
     
    import controller.Controller;
     
    public class ChessBoardView extends JPanel
    {
        private final static ImageIcon DARK_BROWN = new ImageIcon 
        (ChessBoardView.class.getResource("assets/sqb.gif"));
        private final static ImageIcon LIGHT_BROWN = new ImageIcon 
        (ChessBoardView.class.getResource("assets/sqw.gif"));;
        private JLabel sqb;
        private JLabel sqw;
     
    	public ChessBoardView (Controller controller)
        {
     
            Dimension boardSize = new Dimension(300, 300);
     
            setLayout( new GridLayout(8, 8) );
            setPreferredSize( boardSize );
            setBounds(0, 0, boardSize.width, boardSize.height);
     
            sqb = new JLabel (DARK_BROWN);
            sqw = new JLabel (LIGHT_BROWN);
     
            for (int i = 0; i < 64; i++) {
                JPanel square = new JPanel( new BorderLayout() );
                super.add( square );
     
                int row = (i / 8) % 2;
                if (row == 0)
     
                square.add( i % 2 == 0 ? sqw : sqb );
                else
                square.add( i % 2 == 0 ? sqb : sqw );
            }
     
        }  
    }


  2. #2
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: ChessBoardView with JLabel squares doesnt fill the JPanel

    I allready fixed it. I make new JLabels when i add them to the square an anonymous object

Similar Threads

  1. Collision Detection Between Two Squares
    By thegreatzo in forum Loops & Control Statements
    Replies: 7
    Last Post: August 22nd, 2012, 09:13 AM
  2. Loop through JLabel and change JLabel
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:52 PM
  3. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 07:37 PM
  4. Magic Squares, input confusion
    By bengiles89 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 08:40 PM
  5. Funny business with JFrame, JPanel and JLabel
    By JeffC in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2010, 01:26 PM