การสร้างเขาวงกตโดยใช้เมทริกซ์ (JAVA)

ดังนั้นฉันจึงพยายามสร้างเขาวงกตเดี่ยว (ไม่มีเครื่องกำเนิดไฟฟ้า) ใน Java และฉันก็เจอสิ่งกีดขวางบนถนนแล้ว โค้ดปัจจุบันที่ฉันมีจะสร้างเขาวงกตและสร้าง jframe แต่มันจะไม่ลงสี...มีวิธีทำให้การระบายสีได้ผลไหม??

    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;
         }
       }  
    }
    }
    }

ความช่วยเหลือใด ๆ จะทำ ขอบคุณ!


person CSanto98    schedule 10.05.2016    source แหล่งที่มา
comment
คุณเพิ่มสี่เหลี่ยมของคุณลงในเฟรมหรือไม่?   -  person YCF_L    schedule 10.05.2016
comment
โปรแกรมสวิงควรแทนที่ paintComponent() แทนที่จะแทนที่ paint().—จิตรกรรมใน AWT และ Swing: วิธีการทาสี   -  person trashgod    schedule 10.05.2016
comment
นอกจากนี้ ให้พิจารณาแนวทางที่ได้ตรวจสอบแล้วที่นี่   -  person trashgod    schedule 10.05.2016


คำตอบ (1)


1-) อย่าสร้างเคส Classes บน Switch มันไม่ใช่แนวทางปฏิบัติที่ดี

2-) หากคลาสไม่สืบทอด JComponent จะไม่สามารถแทนที่วิธีการทาสีหรือสีองค์ประกอบได้เนื่องจากไม่มี

3-) ตัวอักษรตัวแรกของชื่อคลาสเป็นตัวพิมพ์ใหญ่ และใช้ชื่อที่มีความหมาย

4-) แก้ไขโค้ดของคุณดังนี้:

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;
            }
        }
    }
}
}
person TiyebM    schedule 10.05.2016
comment
ขอบคุณ! การแก้ไขที่คุณทำนั้นยอดเยี่ยมมาก! อย่างไรก็ตาม วิธี setColor ให้สีเฉพาะด้านนอกเท่านั้น ไม่ใช่ด้านใน ดังนั้นฉันจะแก้ไขปัญหานั้น... - person CSanto98; 12.05.2016
comment
@ CSanto98 นั่นเป็นเพราะการใช้ drawRect แทน fillRect ฉันได้อัปเดตตามนั้น - person TiyebM; 12.05.2016
comment
มันเยี่ยมมาก! ขอบคุณสำหรับการแก้ไข ฉันแทบจะบ้าไปแล้วที่พยายามจะคิดเรื่องนี้ - person CSanto98; 16.05.2016