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 3 of 3

Thread: Need GUI help

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need GUI help

    Hey everyone I'm trying to make a brick wall and every second row has to be offset by half of the brick width. I don't know why my program isn't displaying anything all it shows is the grey background.This is my panel class
    import java.awt.*;
    import javax.swing.*;
     
    public class BrickWall extends JPanel
    {
        private final int BRICK_WIDTH = 50;
        private final int BRICK_HEIGHT = 25;
     
        public BrickWall()
        {
            setPreferredSize(new Dimension(500,500));
            setBackground(Color.darkGray);
        }
     
        public void PaintComponent(Graphics page)
        {
            super.paintComponent(page);
            int x,y;
            x=0;
            y=0;
            int xOffset = -1*(BRICK_WIDTH/2);
            int yOffset= BRICK_HEIGHT+y;
            int rows = 0;
            int rowLength = getWidth();
            while(x<=rowLength)
            {
                page.setColor(Color.red);
    		    page.fillRect(x, y, BRICK_WIDTH,BRICK_HEIGHT );
    		    x+=52;
    		    if(rows%2==1)
    		    {
    		        page.setColor (Color.red);
    		        page.fillRect (x, y, BRICK_WIDTH,BRICK_HEIGHT );
    		        x+=52;
    		    }
    		    if(x==getWidth())
    		    {
    		        x=0;
    		        y +=27;
    		        rows++;
    		    }
            }
        }
    }

    This is my driver class
    import javax.swing.*;
    import java.awt.*;
     
     
    public class BrickDriver
    {
     
    	public static void main(String[] args)
    	{
     
     
    		JFrame frame = new JFrame ("Brick Wall");
    		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
    		BrickWall panel = new BrickWall();
    		frame.getContentPane().add(panel);
     
    		frame.pack ();
    		frame.setVisible (true);
    	}
    }

    This is my first time dealing with GUIs so there could be a big problem.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,340
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Need GUI help

    One habit you need to acquire is to always add an @Override statement before any methods of a class that you override. Add one to the BrickWall's code and see what the compiler says.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    GregBrannon (October 25th, 2013)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,349
    My Mood
    Mellow
    Thanks
    254
    Thanked 784 Times in 769 Posts

    Default Re: Need GUI help

    Good catch. Took me longer than it should have.

Similar Threads

  1. GUI help
    By kprofgold in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 13th, 2012, 12:36 AM
  2. GUI
    By andaji in forum Object Oriented Programming
    Replies: 1
    Last Post: April 1st, 2010, 09:45 AM
  3. Replies: 3
    Last Post: February 1st, 2010, 12:24 AM