1

So i'm trying to make a Single maze (No generators) In Java, and I've hit a roadblock. the current code i have will make a maze, and make a jframe, but it won't color it... is there a way to make the coloring work??

    import java.awt.*;
    import javax.swing.*;
    import java.lang.*;

    public class ayy{

    public static void main(String [] args){

    JFrame frame = new JFrame("Maze");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(1000,1000);
    frame.setVisible(true);

    int width = 1;
    int height = 1;

    int [][] map= {
             {0,0,0,0,0,0,0,0,0,0,},
             {0,0,0,0,0,0,0,0,0,0,},
             {2,1,1,1,0,0,0,0,0,0,},
             {0,0,0,1,0,0,0,1,1,2,},
             {0,0,0,1,0,0,0,1,0,0,},
             {0,0,0,1,0,0,0,1,0,0,},
             {0,0,0,1,1,1,1,1,0,0,},
             {0,0,0,0,0,0,0,0,0,0,},
             {0,0,0,0,0,0,0,0,0,0,},
             {0,0,0,0,0,0,0,0,0,0,}
           };

    for(int i=0;i<map.length;i++){
       for(int j=0;j<map.length;j++){
         switch(map[i][j]){
          case 0:
          class rectangle{

       public void paint(Graphics g){
      g.drawRect(1,1,1,1);
      g.setColor(Color.red);
    }  
    }
   break;
  case 1:
  class rectangle2{

       public void paint(Graphics g){
      g.drawRect(1,1,1,1);
      g.setColor(Color.yellow);
    }  
    }       break;
  case 2:
 class rectangle3{

       public void paint(Graphics g){
      g.drawRect(1,1,1,1);
      g.setColor(Color.blue);
    }  
    }       break;
         }
       }  
    }
    }
    }

Any help will do Thanks!

CSanto98
  • 21
  • 5
  • Do you add your rectangles to the Frame? – Youcef LAIDANI May 10 '16 at 13:38
  • 1
    "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). – trashgod May 10 '16 at 14:18
  • Also consider the approach examined [here](http://stackoverflow.com/q/11553461/230513). – trashgod May 10 '16 at 14:18

1 Answers1

1

1-)Do not create Classes on Switch cases, it is not a good practice.

2-)If the Class is not inheriting JComponent then it will not be able to override paint nor paintComponent methods because it does not have them.

3-)Uppercase the first letter of class names, and use meaningful names.

4-)Modify your code like the following:

public class MazeApp extends JFrame {

public static void main(String[] args) {

    JFrame frame = new JFrame("Maze");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(1000, 1000);
    Maze brd = new Maze();
    frame.add(brd);
    frame.setVisible(true);
}
}


class Maze extends JPanel {

public Maze() {
}

protected void paintComponent(Graphics g) {
    int width = 1;
    int height = 1;

    int[][] map = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 1, 0, 0, 0, 1, 1, 2, },
            { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, },
            { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, },
            { 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, },
            { 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, },
            { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, } };

    for (int i = 0; i < map.length; i++) {
        for (int j = 0; j < map.length; j++) {
            int factori = i * 50;
            int factorj = j * 50;
            switch (map[i][j]) {
            case 0: {
                g.setColor(Color.red);
                g.fillRect(factori, factorj, 2, 2);

            }
                break;
            case 1: {
                g.setColor(Color.yellow);
                g.fillRect(factori, factorj, 2, 2);

            }
                break;
            case 2: {
                g.setColor(Color.blue);
                g.fillRect(factori, factorj, 2, 2);

            }
                break;
            }
        }
    }
}
}
TiyebM
  • 2,684
  • 3
  • 40
  • 66
  • Thanks! the fixes you made are fantastic! however, the setColor method only colors the outside, not the inside, so I'm going to fix that... – CSanto98 May 12 '16 at 14:21
  • @CSanto98 that was because of using `drawRect` instead of `fillRect`, I have updated accordingly. – TiyebM May 12 '16 at 16:26
  • It's great! thanks for the fix. I was going crazy trying to figure this out. – CSanto98 May 16 '16 at 14:24