Results 1 to 9 of 9
  1. #1
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default Don't know what to check for

    Hi, I want to make a white box and have a rectangle fill up the box with a different color (blue right now). I want it to fill up in a square pattern. Like starting at (0,0) go to (xmax, 0), then (xmax, ymax), then (0, ymax), and then fill up the rest in the same fashion until it gets to the middle. This is what I have so far --

    Java Code:
    package javaapplication3;
    
    
    import java.awt.*;
    import javax.swing.*;
    
    /**
     *
     * @author Sterling
     */
    public class FillBox extends JPanel{
    
        private int RECT_WIDTH = 20;
        private int RECT_HEIGHT = 20;
        private int rectX = 0;
        private int rectY = 0;
        private static final Dimension PREFERRED_SIZE = new Dimension(400, 200);
        private int deltaX = 0;
        private int deltaY = 0;
    
        public FillBox() {
            setBackground(Color.white);
            setPreferredSize(PREFERRED_SIZE);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.blue);    
            g.fillRect(rectX, rectY, RECT_WIDTH, RECT_HEIGHT);
            findNextSpot(this);         //set up the deltas
            rectX += deltaX;            //change positions
            rectY += deltaY;
        }
    
        public void findNextSpot(FillBox rect) {
            //check if the next x position is available
            //if so change deltaX and if not check the next y position and change deltaX to 0
            //if the next y position is available
            //if so change deltaY and if not change deltaY to 0
            
    
        }
    
        public void createAndShowGUI() {
    
        }
    
    
    }
    I'm stuck on the findNextSpot(FillBox rect) method. I don't know how to check if I have already gone over the next spot. When it goes from (0, ymax) back up to (0,0), I want it to recognize that (0,0) is already a different color and proceed to fill up under the (0,0) -> (xmax, 0) line. Is there a way I check for colors? Am I able to check for adjacent rectangles? I'm pretty lost as to how I can check what has been colored and hasn't. I just want to ask the computer "Is the next space available?" Any help is appreciated.

  2. #2
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    bump. any help is appreciated

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    Not much at all has changed. I just added some stuff in createAndShowGUI() and main().

    Java Code:
    package javaapplication3;
    
    
    import java.awt.*;
    import javax.swing.*;
    
    /**
     *
     * @author Sterling
     */
    public class FillBox extends JPanel{
    
        private int RECT_WIDTH = 20;
        private int RECT_HEIGHT = 20;
        private int rectX = 0;
        private int rectY = 0;
        private static final Dimension PREFERRED_SIZE = new Dimension(400, 200);
        private int deltaX = 0;
        private int deltaY = 0;
    
        public FillBox() {
            setBackground(Color.white);
            setPreferredSize(PREFERRED_SIZE);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.blue);
            g.fillRect(rectX, rectY, RECT_WIDTH, RECT_HEIGHT);
            findNextSpot(this);         //set up the deltas
            rectX += deltaX;
            rectY += deltaY;
        }
    
        public void findNextSpot(FillBox rect) {
            //check if the next x position is available
            //if so change deltaX and if not check the next y position
            //if next y position is available, change deltaY
    
            
    
        }
    
        public void createAndShowGUI() {
            JFrame frame = new JFrame("Filling Box");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new FillBox());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String args[]) {
            FillBox box = new FillBox();
            box.createAndShowGUI();
        }
    
    
    }

  5. #5
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    Actually here is some more updated code I just did. A problem I am having now is where to put the call to repaint().

    Java Code:
    package javaapplication3;
    
    
    import java.awt.*;
    import javax.swing.*;
    
    /**
     *
     * @author Sterling
     */
    public class FillBox extends JPanel{
    
        private int RECT_WIDTH = 20;
        private int RECT_HEIGHT = 20;
        private int rectX = 0;
        private int rectY = 0;
        private static final Dimension PREFERRED_SIZE = new Dimension(400, 200);
        private int deltaX = 0;
        private int deltaY = 0;
        private int xStopper = 400;
        private int yStopper = 200;
    
        public FillBox() {
            setBackground(Color.white);
            setPreferredSize(PREFERRED_SIZE);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.blue);
            g.fillRect(rectX, rectY, RECT_WIDTH, RECT_HEIGHT);
            findNextSpot(this);         //set up the deltas
            rectX += deltaX;
            rectY += deltaY;
            xStopper -= 5;      //update the last spot marked on the x axis
            yStopper -= 5;      //update the last spot marked on the y axis
        }
    
        public void findNextSpot(FillBox rect) {
            if(rectX < (xStopper - 5)) {
                deltaX = 5;
            }
            else deltaX = 0;
    
            if(rectY < (yStopper - 5)) {
                deltaY = 5;
            }
            else deltaY = 0;
    
        }
    
        public void createAndShowGUI() {
            JFrame frame = new JFrame("Filling Box");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new FillBox());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String args[]) {
            FillBox box = new FillBox();
            box.createAndShowGUI();
        }
    
    
    }

  6. #6
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    Sorry a couple more lines added to findNextSpot()...

    Java Code:
    package javaapplication3;
    
    
    import java.awt.*;
    import javax.swing.*;
    
    /**
     *
     * @author Sterling
     */
    public class FillBox extends JPanel{
    
        private int RECT_WIDTH = 20;
        private int RECT_HEIGHT = 20;
        private int rectX = 0;
        private int rectY = 0;
        private static final Dimension PREFERRED_SIZE = new Dimension(400, 200);
        private int deltaX = 0;
        private int deltaY = 0;
        private int xStopper = 400;
        private int yStopper = 200;
    
        public FillBox() {
            setBackground(Color.white);
            setPreferredSize(PREFERRED_SIZE);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.blue);
            g.fillRect(rectX, rectY, RECT_WIDTH, RECT_HEIGHT);
            findNextSpot(this);         //set up the deltas
            rectX += deltaX;
            rectY += deltaY;
            xStopper -= 5;      //update the last spot marked on the x axis
            yStopper -= 5;      //update the last spot marked on the y axis
        }
    
        public void findNextSpot(FillBox rect) {
            if(rectX < (xStopper - 5)) {
                deltaX = 5;
            }
            else deltaX = 0;
            
            if(rectY >= 100) {     //check if the rect needs to go left
                deltaX = -5;
            }
    
            if(rectY < (yStopper - 5)) {
                deltaY = 5;
            }
            else deltaY = 0;
            
            if(rectX <= 200) {      //check if the rect needs to go up
                deltaY = -5;
            }
    
        }
    
        public void createAndShowGUI() {
            JFrame frame = new JFrame("Filling Box");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new FillBox());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String args[]) {
            FillBox box = new FillBox();
            box.createAndShowGUI();
        }
    
    
    }

  7. #7
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    Well I had a program slightly similar to this that used a Timer. So I tried to change this application to use a Timer. The program with this way is that now when the JFrame comes up, no rectangles show up at all. Before the rectangle would sit at (0,0) and not move, but this way it doesn't even show up. I feel like this still might be the better way to do what I want to do, but eh.. I don't see why no rectangles are showing up.
    Java Code:
    package javaapplication3;
    
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    /**
     *
     * @author Sterling
     */
    public class FillBox extends JPanel{
    
        private int RECT_WIDTH = 20;
        private int RECT_HEIGHT = 20;
        private int rectX = 0;
        private int rectY = 0;
        private static final Dimension PREFERRED_SIZE = new Dimension(400, 200);
        private int deltaX = 0;
        private int deltaY = 0;
        private int xStopper = 400;
        private int yStopper = 200;
    
        public FillBox() {
            setBackground(Color.white);
            setPreferredSize(PREFERRED_SIZE);
            new Timer(10, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                if(rectX < (xStopper - 5)) {
                        deltaX = 5;
                    }
                else deltaX = 0;
    
                if(rectY >= 100) {     //check if the rect needs to go left
                    deltaX = -5;
                }
    
                if(rectY < (yStopper - 5)) {
                    deltaY = 5;
                }
                else deltaY = 0;
    
                if(rectX <= 200) {      //check if the rect needs to go up
                    deltaY = -5;
                    }
                rectX += deltaX;
                rectY += deltaY;
                xStopper -= RECT_WIDTH;      //update the last spot marked on the x axis
                yStopper -= RECT_HEIGHT;      //update the last spot marked on the y axis
                FillBox.this.repaint();
                }
            }).start();
    
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.blue);
            g.fillRect(rectX, rectY, RECT_WIDTH, RECT_HEIGHT);
            findNextSpot(this);         //set up the deltas
        }
    
        public void findNextSpot(FillBox rect) {
           
    
        }
    
        public void createAndShowGUI() {
            JFrame frame = new JFrame("Filling Box");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new FillBox());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String args[]) {
            FillBox box = new FillBox();
            box.createAndShowGUI();
        }
    
    
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I know I shouldn't do this, but interesting problem...
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FillBox2 extends JPanel {
       private static final int SIDE = 20;
       private static final int SIDE_SIZE = 20;
       private static final int TIMER_DELAY = 50;
       public static final Color INIT_COLOR = Color.white;
       public static final Color FILLED_COLOR = Color.blue;
       private MyBox[][] boxGrid = new MyBox[SIDE][SIDE];
       private Timer boxFillTimer;
       
       public FillBox2() {
          for (int row = 0; row < boxGrid.length; row++) {
             for (int col = 0; col < boxGrid[row].length; col++) {
                int x = col * SIDE_SIZE;
                int y = row * SIDE_SIZE;
                int w = SIDE_SIZE;
                int h = SIDE_SIZE;
                MyBox box = new MyBox(x, y, w, h, INIT_COLOR);
                boxGrid[row][col] = box; 
             }
          }
          boxFillTimer = new Timer(TIMER_DELAY, new BoxFillListener());
          
          addMouseListener(new MouseAdapter() {
    
             public void mousePressed(MouseEvent e) {
                if (boxFillTimer == null) {
                   return;
                }
                if (!boxFillTimer.isRunning()) {
                   boxFillTimer.start();               
                } else {
                   boxFillTimer.stop();
                }
             }
          });
          
          
          int width = SIDE * SIDE_SIZE;
          int height = width;
          setPreferredSize(new Dimension(width, height));
       }
       
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          for (int row = 0; row < boxGrid.length; row++) {
             for (int col = 0; col < boxGrid[row].length; col++) {
                boxGrid[row][col].paint(g);
             }
          }
       }
       
       private class BoxFillListener implements ActionListener {
          Point p = new Point(0, 0);
          MyDirection direction = MyDirection.RIGHT;
          int lastFillCount = 0;
          
          public void actionPerformed(ActionEvent e) {
             boxGrid[p.y][p.x].setColor(FILLED_COLOR);
             FillBox2.this.repaint();
             Point newP = direction.next(p);
             if (newP.x >= SIDE || newP.y >= SIDE || newP.x < 0 || newP.y < 0) {
                direction = direction.nextDirection();
                newP = direction.next(p);
             } else if (boxGrid[newP.y][newP.x].getColor() == FILLED_COLOR) {
                direction = direction.nextDirection();
                newP = direction.next(p);
                lastFillCount++;
             } else {
                lastFillCount = 0;
             }
             p = newP;
             if (lastFillCount > 2) {
                boxFillTimer.stop();
                boxFillTimer = null;
             }
          }
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Click Me!");
          frame.getContentPane().add(new FillBox2());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class MyBox {
       private int x;
       private int y;
       private int width;
       private int height;
       private Color color;;
       
       public MyBox(int x, int y, int w, int h, Color c) {
          this.x = x;
          this.y = y;
          this.width = w;
          this.height = h;
          this.color = c;
       }
       
       public void paint(Graphics g) {
          g.setColor(color);
          g.fillRect(x, y, width, height);
       }
       
       public void setColor(Color c) {
          color = c;
       }
       
       public Color getColor() {
          return color;
       }
       
    }
    
    enum MyDirection {
       RIGHT, DOWN, LEFT, UP;
       
       public MyDirection nextDirection() {
          switch (this) {
             case RIGHT: return DOWN;
             case DOWN: return LEFT;
             case LEFT: return UP;
             case UP: return RIGHT;
    
             default: return null;
          }
       }
       
       public Point next(Point p) {
          switch (this) {
             case RIGHT: return new Point(p.x + 1, p.y);
             case DOWN: return new Point(p.x, p.y + 1);
             case LEFT: return new Point(p.x - 1, p.y);
             case UP: return new Point(p.x, p.y - 1);
    
             default: return null;
          }
       }
    }

  9. #9
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    Wow that was a lot more complicated than I expected it to be. Thanks for the code. Don't worry it wasn't an assignment or anything...just something I wanted to see if I could do.

Similar Threads

  1. pls check it
    By aRTx in forum New To Java
    Replies: 0
    Last Post: 03-24-2009, 03:35 PM
  2. Plz Some one check my code
    By TamTam in forum AWT / Swing
    Replies: 1
    Last Post: 02-08-2009, 12:24 AM
  3. Check for null int
    By SnarfSnarf in forum New To Java
    Replies: 5
    Last Post: 01-31-2009, 12:12 AM
  4. Check for printer job
    By pjmorce in forum Advanced Java
    Replies: 0
    Last Post: 11-07-2008, 09:26 AM
  5. Check box tag
    By elizaabru in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-26-2008, 03:37 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
  •