Results 1 to 17 of 17
  1. #1
    jls7884 is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Filling a frame with objects

    I am working on a bubble breaker style game and want to fill the frame completely with squares of different colors. After I create a square object is there a way to automatically fill the frame with multiple copies of the same object or do I have to set a position for each and fill the available space manually?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Filling a frame with objects

    a way to automatically fill the frame with multiple copies of the same object
    The only technique I can think of that is close to that would be to use a layout manager and have a loop that creates copies and adds them to the container where the layout manager would handle filling the container.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Filling a frame with objects

    Well, if I were going to write the game, I would probably create classes for each object. I would simply create a model of the objects which
    are painted in a JPanel inside the frame. Any mouse events would affect the model and then the model would be repainted as required.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    jls7884 is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Filling a frame with objects

    Quote Originally Posted by Norm View Post
    The only technique I can think of that is close to that would be to use a layout manager and have a loop that creates copies and adds them to the container where the layout manager would handle filling the container.
    I wouldn't even know where to start as I haven't had much experience with layout managers. I am beginning to think this might be too complicated for a college project due in a couple of weeks. Can I create a loop to create each square and increment on the x or y values using the size of the square and fill the container like that? I was thinking about hard coding each square but I would have to create 384 objects for my frame size and that doesn't seem like a realistic approach not to mention time consuming.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Filling a frame with objects

    Can I create a loop to create each square and increment on the x or y values using the size of the square and fill the container like that?
    Yes that could work.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jls7884 is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Filling a frame with objects

    I'll try that first. Heading to work now. I will work on implementing a loop and post back sometime this weekend when I have something or if I am still having trouble.

  7. #7
    jls7884 is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Filling a frame with objects

    This ended up working for me:
    Java Code:
    for (int x = 0; x < 400; x += 25) {
                for (int y = 0; y < 600; y += 25) {
                    g.fill3DRect(x, y, 25, 25, true);
                    if (x == 400 || y == 600) {
                        break;
                    }
                }
            }
    Now I am having issues with the color. Currently it sets a random color on startup but it is the same color for all bricks.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Filling a frame with objects

    Which makes sense: setting a color once, everything will be drawn in that color; set the (random) color just above the line g.fill3DRect( ... ) if you want each brick drawn in a different color.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    jls7884 is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Filling a frame with objects

    Tried that. Didn't work for me.

    Currently this is what I have which I would think should work:
    Java Code:
    public class Brick {    
        
    Color color = new Color(
        (int)(Math.random() * 256), (int)(Math.random() * 256),
        (int)(Math.random() * 256));    
    }
    Java Code:
    class BrickPanel extends JPanel {
      
        public void Restart () {
            repaint ();
        }
        
    class TimerListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    }
    
        @Override
        protected void paintComponent (Graphics g) {
            super.paintComponent(g);
            
            Brick brick = new Brick();
            
            for (int x = 0; x < 400; x += 25) {
                for (int y = 0; y < 600; y += 25) {
                    g.setColor(brick.color);
                    g.fill3DRect(x, y, 25, 25, true);
                    if (x == 400 || y == 600) {
                        break;
                    }
                }
            }
    
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
            
        }
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Filling a frame with objects

    Didn't work for me.
    The code on line 22 sets the color, to the current value of brick.color. If you want different colors, the value of brick.color needs to be changed for every time you want a new color.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jls7884 is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Filling a frame with objects

    Ah, Got it now. If I use a set of colors using an array how can I convert the color array in order to be able to use it here or can I not:
    Java Code:
    g.setColor(color);

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Filling a frame with objects

    how can I convert the color array in order to be able to use it
    You use the contents of an array by indexing into it:
    Java Code:
      theArray[theIndex] // get element in theArray at theIndex
      theIndex++;  // move to next element in the array
    If the requirements are for more values than the length of the array, the index would need to be tested to see if it is at the end of the array,
    and if it is at the end, the indexes value would be set to zero to start at the beginning.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    jls7884 is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Filling a frame with objects

    Ok. Here is my array:
    Java Code:
    Color[] colors = new Color[] {
        Color.BLUE, Color.WHITE, Color.ORANGE, Color.PINK
     };
    I know I can use
    Java Code:
    g.setColor (colors[1]);
    to set the color at that index.
    How do I use a random index in this case to select a random color out of those in the array?

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Filling a frame with objects

    Two ways.

    Java Code:
    Random r = new Random();
    int n = r.nextInt(3); // get number from 0 to 2 inclusive
    or

    Java Code:
    int n = (int)(Math.random()*3);  // same
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    jls7884 is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Filling a frame with objects

    That's what I tried but just realized I was using it wrong. Was passing the variable for the index in graphics instead of the color array using the index variable. What I need to do now is to create the mouse click events but I don't even know where to start. I have to be able to click on one of the colored bricks and have that clear all of the bricks touching it that have the same color and shift the remaining bricks down.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Filling a frame with objects

    To me that says you need a non-graphic model that you do the work with.

    All the GUI does is display that model.
    That model will use your Brick class and is (presumably) an array of arrays of Bricks.
    It can then do the work of figuring out which Bricks are next to the one selected.
    Forget about the mouse clicks for that bit.

    Once you have that BrickManager (for want of a better name) class, that can handle a removing a Brick via a removeBrick method then you are in a good position to do the GUI bit.

    For that I would suggest JButtons, that way you don't have to hand craft the mouse clicking.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    jls7884 is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Filling a frame with objects

    I'm fairly new to graphics and objects as this is only my second java programming class so I don't understand what you mean by a non graphic model. I can post my code to show what I am working with and maybe you can better point me in the right direction.

Similar Threads

  1. How to add Multiple Objects in Frame
    By AryanArs in forum New To Java
    Replies: 5
    Last Post: 02-26-2012, 08:05 PM
  2. Unable to Display Two Objects in Frame
    By AryanArs in forum AWT / Swing
    Replies: 4
    Last Post: 02-26-2012, 06:56 PM
  3. Unable to Display Two Objects in Frame
    By AryanArs in forum New To Java
    Replies: 2
    Last Post: 02-26-2012, 06:43 AM
  4. Draw multiple class objects in one frame
    By jj007 in forum AWT / Swing
    Replies: 2
    Last Post: 01-14-2012, 06:32 PM
  5. filling an array of Address objects..?
    By hiei_yasha in forum New To Java
    Replies: 9
    Last Post: 02-20-2011, 11:25 PM

Tags for this Thread

Posting Permissions

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