Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By jim829

Thread: Where to start. Loop drawRect.

  1. #1
    vuekong is offline Member
    Join Date
    Jul 2013
    Location
    MN
    Posts
    5
    Rep Power
    0

    Default Where to start. Loop drawRect.

    Hi guys. Total noob here... I'm trying to figure out how to loop a drawRect. Pretty much I have to build a brick wall based on users input of rows. I'm not sure where to begin. I've done some other examples similar but different. Any help would be great. I just need guide into what I should be looking at. Thanks.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Where to start. Loop drawRect.

    Well, we are here to help. However, if you are totally new to Java why are you starting off with Graphics? It would be in your best interest to read up on the basics of Java before undertaking a graphics program. It will make your future Graphics and painting experiences much more pleasant.

    Regards,
    Jim
    gimbal2 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    vuekong is offline Member
    Join Date
    Jul 2013
    Location
    MN
    Posts
    5
    Rep Power
    0

    Default Re: Where to start. Loop drawRect.

    Hi Jim, Thanks for replying so quickly. I'm taking my first java class and we're done in two weeks. I'm not a total noob but I still feel like one because I don't fully understand everything so far I have created this to start off with. But I'm stuck on where to go from there. Thanks in advanced.

    public class Wall extends JApplet implements ActionListener{

    //labels declared
    JLabel prompt, title;

    //textfield declared
    JTextField input;

    //buttons declared
    JButton generate;

    //panels declared
    JPanel userpanel, imagepanel, titlepanel;

    int x = 5;
    int y = 250;
    int width = 60;
    int height = 15;
    int spacing = 10;

    public void init() {

    setLayout(new BorderLayout());

    //label
    prompt = new JLabel("<HTML><CENTER>How many rows of<BR>"
    + "bricks would you<BR>"
    + "like to create?");
    title = new JLabel("<HTML><B><Font size=16><font color=red>Wall");

    //textfield
    input = new JTextField(5);

    //buttons
    generate = new JButton("Generate");

    //panels
    titlepanel = new JPanel();
    titlepanel.setLayout(new FlowLayout());
    titlepanel.add(title);
    add(titlepanel, BorderLayout.NORTH);

    userpanel = new JPanel();
    userpanel.setLayout(new GridLayout(10, 1, 5,5));
    add(userpanel, BorderLayout.WEST);
    userpanel.add(prompt);
    userpanel.add(input);
    userpanel.add(generate);

    generate.addActionListener(this);

    imagepanel = new JPanel();
    imagepanel.setLayout(new FlowLayout());
    add(imagepanel, BorderLayout.CENTER);


    }

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Where to start. Loop drawRect.

    Okay but where are you stuck? I am guessing on the imagepanel, you want to draw your brick wall. But how do you want to do that? By drawing rectangles? If so, you are entering the territory of custom painting.

    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,251
    Rep Power
    19

    Default Re: Where to start. Loop drawRect.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    vuekong is offline Member
    Join Date
    Jul 2013
    Location
    MN
    Posts
    5
    Rep Power
    0

    Default Re: Where to start. Loop drawRect.

    Sorry about that. Got it now.

  7. #7
    vuekong is offline Member
    Join Date
    Jul 2013
    Location
    MN
    Posts
    5
    Rep Power
    0

    Default Re: Where to start. Loop drawRect.

    Erik,
    Yes I want to draw a brick wall in the imagepanel using for loop statement. This is what I have so far for the drawing, but I'm lost on what to put in for the for loop statment. I'm not sure if it is right but this is what I have so far.

    Java Code:
    	public void paint(Graphics g) 
    	{
    		super.paint(g);
    		int width = this.getWidth(imagepanel);
    		for ()
    		{
    			if()
    				g.drawRect(x, y, width, height);
    				x = x + spacing + x;
    				y = y + spacing + x;
    				width = width;
    				height = height;		
    		}
    			
    	}

  8. #8
    vuekong is offline Member
    Join Date
    Jul 2013
    Location
    MN
    Posts
    5
    Rep Power
    0

    Default Re: Where to start. Loop drawRect.

    I'm looking for something like this. I'm stuck in two places... the actionevent and drawing the brick part. At the bottom is my current code. Thanks in advance.

    https://dl.dropboxusercontent.com/u/56683865/Wall

    Java Code:
    public class Wall extends JApplet implements ActionListener{
    		
    		//labels declared
    		JLabel prompt, title;
    
    		//textfield declared
    		JTextField input;
    			
    		//buttons declared
    		JButton generate;
    		
    		//panels declared
    		JPanel userpanel, titlepanel, imagepanel;
    
    	public void init() {
    		
    		setLayout(new BorderLayout());
    		
    		//label
    		prompt = new JLabel("<HTML><CENTER>Please enter<BR>"
    			+ "a number<BR>" 
    			+ "between 1-20");
    		title = new JLabel("<HTML><B><Font size=16><font color=red>Wall");
    		
    		//textfield
    		input = new JTextField(5);
    		
    		//buttons
    		generate = new JButton("Generate");
    		
    		//panels
    		titlepanel = new JPanel();
    		titlepanel.setLayout(new FlowLayout());
    		titlepanel.add(title);
    		add(titlepanel, BorderLayout.NORTH);
    		
    		//user panel added
    		userpanel = new JPanel();
    		userpanel.setLayout(new GridLayout(10, 1, 5,5));
    		add(userpanel, BorderLayout.WEST);
    		userpanel.add(prompt);
    		userpanel.add(input);
    		userpanel.add(generate);
    		
    		//actionlistener added to button
    		generate.addActionListener(this);
    	
    		//image panel added
    		imagepanel = new JPanel();
    		imagepanel.setLayout(new FlowLayout());
    		add(imagepanel, BorderLayout.CENTER);
    		
    	}
    	
    	public void actionPerformed(ActionEvent ae)
    		{
    			Object source = ae.getSource();
        		if( source == generate )
        		{	
    	  			int rowsinput = (Integer.parseInt(input.getText()));
    	  			{
    	  				if(input.
    	  				
    	  			}
    			}
    		}
    
    	public void paint(Graphics g) 
    	{
    		super.paint(g);
    		g.drawRect(5, 250, 60,20);
    
    			}
    			
    		
    }

Similar Threads

  1. For Loop illegal start of expression error
    By Syed Rizvi in forum New To Java
    Replies: 11
    Last Post: 07-18-2013, 03:56 PM
  2. For Loop, illegal start of expression
    By Slinkie45 in forum New To Java
    Replies: 6
    Last Post: 05-11-2011, 01:35 AM
  3. Replies: 3
    Last Post: 01-07-2011, 12:23 AM
  4. Random drawRect
    By Wallsurfer in forum New To Java
    Replies: 5
    Last Post: 03-10-2009, 09:20 AM
  5. Replies: 4
    Last Post: 09-05-2008, 04:47 PM

Posting Permissions

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