Java 从不同数组(3个数组)中搜索相同位置(i,j)的相同颜色(RGB)并将其锁定

Java 从不同数组(3个数组)中搜索相同位置(i,j)的相同颜色(RGB)并将其锁定,java,arrays,swing,arraylist,Java,Arrays,Swing,Arraylist,我制作了一个简单的学习程序来创建3个数组,将JPanel放入其中,然后为每个数组指定随机颜色(每个数组都有自己的“随机发生器”JtoggleButton) 为了继续学习,我考虑再创建一个按钮,从数组中搜索相同位置的相同颜色,如array1[0][0](r,g,b)=array2[0][0](r,g,b),然后锁定它(因此循环无法重新验证其颜色) 我的问题是: 是否可以仅锁定类似于数组[0][9]的数组位置 我想我应该先让随机循环同步,是吗 这是有条不紊的,对吗 如果可能,我应该从哪里开始构建它

我制作了一个简单的学习程序来创建3个数组,将
JPanel
放入其中,然后为每个数组指定随机颜色(每个数组都有自己的“随机发生器”
JtoggleButton

为了继续学习,我考虑再创建一个按钮,从数组中搜索相同位置的相同颜色,如array1[0][0](r,g,b)=array2[0][0](r,g,b),然后锁定它(因此循环无法重新验证其颜色)

我的问题是:

  • 是否可以仅锁定类似于数组[0][9]的数组位置
  • 我想我应该先让随机循环同步,是吗 这是有条不紊的,对吗
  • 如果可能,我应该从哪里开始构建它
  • 这不是一个问题,但是,如果我做了不必要的“//复制方法”,请鼓励我,除了JToggleButtons
  • 另外,唯一的问题是,这可能是该方法的下一步,其他人使用不同的语言,我不认为这有相同的问题

    守则:

    Main:

    public class Main {
    
        private static void createMainFrame() {
            new Window().initMainFrame();
        }
    
        @SuppressWarnings("Convert2Lambda")
        public static void main (String [] args) {
            SwingUtilities.invokeLater (new Runnable() {
                @Override
                public void run() {
                    createMainFrame();
                }
            });
        }
    }
    
    public class Window {
    
        public void initMainFrame() {
            Container container = mainFrame.getContentPane();
            container.setLayout(null);
        }
    
        private static void createBlocks1() {
            new Blocks().initBlocks1();
        }
        // createBlocks2
        // createBlocks3
    
        public static JFrame mainFrame = new JFrame("learning 43");
        public static JPanel viewerBoard1 = new JPanel();
        // viewerBoard2
        // viewerBoard3
    
        public static JToggleButton randomize1 = new JToggleButton("RANDOMIZER");
        // randomize2
        // randomize3
    
        @SuppressWarnings("Convert2Lambda")
        public Window() {
            initMainFrame();
            mainFrame.setSize(676,357);
            mainFrame.setLocationRelativeTo(null);
            mainFrame.setResizable(false);
            mainFrame.setAlwaysOnTop(true);
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrame.setVisible(true);
    
            mainFrame.add(viewerBoard1);
            viewerBoard1.setBounds(15, 15, 200, 200);
            viewerBoard1.setBackground(new Color(222, 222, 222));
            viewerBoard1.setVisible(true);
            // viewerBoard2
            // viewerBoard3
    
            JPanel viewerPanel1 = new JPanel();
            mainFrame.add(viewerPanel1);
            viewerPanel1.setBounds(10, 10, 210, 210);
            viewerPanel1.setBackground(new Color(111, 111, 111));
            viewerPanel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
            viewerPanel1.setVisible(true);
            // viewerpanel2
            // viewerpanel3
    
            mainFrame.add(randomize1);
            randomize1.setFocusPainted(false);
            randomize1.setRolloverEnabled(true);
            randomize1.setForeground(Color.GRAY);
            randomize1.setFont(new Font("Courier", Font.BOLD, 16));
            randomize1.setMargin(new Insets(0, 0, 0, 0));
            randomize1.setBounds(10, 230, 210, 90);
            randomize1.setVisible(true);
            randomize1.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent randomEvent) {
                    if (randomEvent.getStateChange() == ItemEvent.SELECTED){
                        System.out.println("Randomize Button 1 selected: " + randomize1.isSelected());
                        System.out.println("randomizing panel 1...");
                        while (randomize1.isSelected()) {
                            createBlocks1();
                            break;
                        }
                    }
                    if (randomEvent.getStateChange() == ItemEvent.DESELECTED){
                        System.out.println("Randomize Button 1 deselcted!");
                        System.out.println("eraseing panel 1...");
                        viewerBoard1.removeAll();
                        viewerBoard1.revalidate();
                        viewerBoard1.repaint();
                    }
                }
            });
            // randomize2
            // randomize3
        }
    }
    
    public class Blocks {
    
        public void initBlocks1() {
            BlockThread1 thread1 = new BlockThread1();
            thread1.start();
        }
        // initBlocks2
        // initBlocks3
    
        Container container = Window.viewerBoard1;
        JPanel [][] blocksArray1 = new JPanel[4][4];
        JPanel colsPanel;
        JPanel rowsPanel;
        Random random1 = new Random();
        private Color RColor1() {
            return new Color(random1.nextInt(255), random1.nextInt(255), random1.nextInt(255)/*, random.nextInt(255)*/);
        }
        class BlockThread1 extends Thread {
            @Override
            public void run(){
                container.setLayout(new GridLayout(4, 4));
                do {
                    try {
                        for (int cols1 = 0; cols1 < blocksArray1.length; cols1++) {
                            colsPanel = new JPanel();
                            colsPanel.setBackground(RColor1());
                            Window.viewerBoard1.revalidate();
                            Window.viewerBoard1.add(colsPanel);
    
                            for (int rows1 = 1; rows1 < blocksArray1[cols1].length; rows1++) {
                                rowsPanel = new JPanel();
                                rowsPanel.setBackground(RColor1());
                                Window.viewerBoard1.revalidate();
                                Window.viewerBoard1.add(rowsPanel);
                            }
                        }
                        Thread.sleep(1000);
                        Window.viewerBoard1.removeAll();
                        Window.viewerBoard1.revalidate();
                        Window.viewerBoard1.repaint();
                    }
                    catch (InterruptedException ex) {
                        java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                    }
                } while (Window.randomize1.isSelected());
            }
        }
        // blocksArray2
        // blocksArray3
    }
    
    窗口:

    public class Main {
    
        private static void createMainFrame() {
            new Window().initMainFrame();
        }
    
        @SuppressWarnings("Convert2Lambda")
        public static void main (String [] args) {
            SwingUtilities.invokeLater (new Runnable() {
                @Override
                public void run() {
                    createMainFrame();
                }
            });
        }
    }
    
    public class Window {
    
        public void initMainFrame() {
            Container container = mainFrame.getContentPane();
            container.setLayout(null);
        }
    
        private static void createBlocks1() {
            new Blocks().initBlocks1();
        }
        // createBlocks2
        // createBlocks3
    
        public static JFrame mainFrame = new JFrame("learning 43");
        public static JPanel viewerBoard1 = new JPanel();
        // viewerBoard2
        // viewerBoard3
    
        public static JToggleButton randomize1 = new JToggleButton("RANDOMIZER");
        // randomize2
        // randomize3
    
        @SuppressWarnings("Convert2Lambda")
        public Window() {
            initMainFrame();
            mainFrame.setSize(676,357);
            mainFrame.setLocationRelativeTo(null);
            mainFrame.setResizable(false);
            mainFrame.setAlwaysOnTop(true);
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrame.setVisible(true);
    
            mainFrame.add(viewerBoard1);
            viewerBoard1.setBounds(15, 15, 200, 200);
            viewerBoard1.setBackground(new Color(222, 222, 222));
            viewerBoard1.setVisible(true);
            // viewerBoard2
            // viewerBoard3
    
            JPanel viewerPanel1 = new JPanel();
            mainFrame.add(viewerPanel1);
            viewerPanel1.setBounds(10, 10, 210, 210);
            viewerPanel1.setBackground(new Color(111, 111, 111));
            viewerPanel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
            viewerPanel1.setVisible(true);
            // viewerpanel2
            // viewerpanel3
    
            mainFrame.add(randomize1);
            randomize1.setFocusPainted(false);
            randomize1.setRolloverEnabled(true);
            randomize1.setForeground(Color.GRAY);
            randomize1.setFont(new Font("Courier", Font.BOLD, 16));
            randomize1.setMargin(new Insets(0, 0, 0, 0));
            randomize1.setBounds(10, 230, 210, 90);
            randomize1.setVisible(true);
            randomize1.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent randomEvent) {
                    if (randomEvent.getStateChange() == ItemEvent.SELECTED){
                        System.out.println("Randomize Button 1 selected: " + randomize1.isSelected());
                        System.out.println("randomizing panel 1...");
                        while (randomize1.isSelected()) {
                            createBlocks1();
                            break;
                        }
                    }
                    if (randomEvent.getStateChange() == ItemEvent.DESELECTED){
                        System.out.println("Randomize Button 1 deselcted!");
                        System.out.println("eraseing panel 1...");
                        viewerBoard1.removeAll();
                        viewerBoard1.revalidate();
                        viewerBoard1.repaint();
                    }
                }
            });
            // randomize2
            // randomize3
        }
    }
    
    public class Blocks {
    
        public void initBlocks1() {
            BlockThread1 thread1 = new BlockThread1();
            thread1.start();
        }
        // initBlocks2
        // initBlocks3
    
        Container container = Window.viewerBoard1;
        JPanel [][] blocksArray1 = new JPanel[4][4];
        JPanel colsPanel;
        JPanel rowsPanel;
        Random random1 = new Random();
        private Color RColor1() {
            return new Color(random1.nextInt(255), random1.nextInt(255), random1.nextInt(255)/*, random.nextInt(255)*/);
        }
        class BlockThread1 extends Thread {
            @Override
            public void run(){
                container.setLayout(new GridLayout(4, 4));
                do {
                    try {
                        for (int cols1 = 0; cols1 < blocksArray1.length; cols1++) {
                            colsPanel = new JPanel();
                            colsPanel.setBackground(RColor1());
                            Window.viewerBoard1.revalidate();
                            Window.viewerBoard1.add(colsPanel);
    
                            for (int rows1 = 1; rows1 < blocksArray1[cols1].length; rows1++) {
                                rowsPanel = new JPanel();
                                rowsPanel.setBackground(RColor1());
                                Window.viewerBoard1.revalidate();
                                Window.viewerBoard1.add(rowsPanel);
                            }
                        }
                        Thread.sleep(1000);
                        Window.viewerBoard1.removeAll();
                        Window.viewerBoard1.revalidate();
                        Window.viewerBoard1.repaint();
                    }
                    catch (InterruptedException ex) {
                        java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                    }
                } while (Window.randomize1.isSelected());
            }
        }
        // blocksArray2
        // blocksArray3
    }
    
    块:

    public class Main {
    
        private static void createMainFrame() {
            new Window().initMainFrame();
        }
    
        @SuppressWarnings("Convert2Lambda")
        public static void main (String [] args) {
            SwingUtilities.invokeLater (new Runnable() {
                @Override
                public void run() {
                    createMainFrame();
                }
            });
        }
    }
    
    public class Window {
    
        public void initMainFrame() {
            Container container = mainFrame.getContentPane();
            container.setLayout(null);
        }
    
        private static void createBlocks1() {
            new Blocks().initBlocks1();
        }
        // createBlocks2
        // createBlocks3
    
        public static JFrame mainFrame = new JFrame("learning 43");
        public static JPanel viewerBoard1 = new JPanel();
        // viewerBoard2
        // viewerBoard3
    
        public static JToggleButton randomize1 = new JToggleButton("RANDOMIZER");
        // randomize2
        // randomize3
    
        @SuppressWarnings("Convert2Lambda")
        public Window() {
            initMainFrame();
            mainFrame.setSize(676,357);
            mainFrame.setLocationRelativeTo(null);
            mainFrame.setResizable(false);
            mainFrame.setAlwaysOnTop(true);
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrame.setVisible(true);
    
            mainFrame.add(viewerBoard1);
            viewerBoard1.setBounds(15, 15, 200, 200);
            viewerBoard1.setBackground(new Color(222, 222, 222));
            viewerBoard1.setVisible(true);
            // viewerBoard2
            // viewerBoard3
    
            JPanel viewerPanel1 = new JPanel();
            mainFrame.add(viewerPanel1);
            viewerPanel1.setBounds(10, 10, 210, 210);
            viewerPanel1.setBackground(new Color(111, 111, 111));
            viewerPanel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
            viewerPanel1.setVisible(true);
            // viewerpanel2
            // viewerpanel3
    
            mainFrame.add(randomize1);
            randomize1.setFocusPainted(false);
            randomize1.setRolloverEnabled(true);
            randomize1.setForeground(Color.GRAY);
            randomize1.setFont(new Font("Courier", Font.BOLD, 16));
            randomize1.setMargin(new Insets(0, 0, 0, 0));
            randomize1.setBounds(10, 230, 210, 90);
            randomize1.setVisible(true);
            randomize1.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent randomEvent) {
                    if (randomEvent.getStateChange() == ItemEvent.SELECTED){
                        System.out.println("Randomize Button 1 selected: " + randomize1.isSelected());
                        System.out.println("randomizing panel 1...");
                        while (randomize1.isSelected()) {
                            createBlocks1();
                            break;
                        }
                    }
                    if (randomEvent.getStateChange() == ItemEvent.DESELECTED){
                        System.out.println("Randomize Button 1 deselcted!");
                        System.out.println("eraseing panel 1...");
                        viewerBoard1.removeAll();
                        viewerBoard1.revalidate();
                        viewerBoard1.repaint();
                    }
                }
            });
            // randomize2
            // randomize3
        }
    }
    
    public class Blocks {
    
        public void initBlocks1() {
            BlockThread1 thread1 = new BlockThread1();
            thread1.start();
        }
        // initBlocks2
        // initBlocks3
    
        Container container = Window.viewerBoard1;
        JPanel [][] blocksArray1 = new JPanel[4][4];
        JPanel colsPanel;
        JPanel rowsPanel;
        Random random1 = new Random();
        private Color RColor1() {
            return new Color(random1.nextInt(255), random1.nextInt(255), random1.nextInt(255)/*, random.nextInt(255)*/);
        }
        class BlockThread1 extends Thread {
            @Override
            public void run(){
                container.setLayout(new GridLayout(4, 4));
                do {
                    try {
                        for (int cols1 = 0; cols1 < blocksArray1.length; cols1++) {
                            colsPanel = new JPanel();
                            colsPanel.setBackground(RColor1());
                            Window.viewerBoard1.revalidate();
                            Window.viewerBoard1.add(colsPanel);
    
                            for (int rows1 = 1; rows1 < blocksArray1[cols1].length; rows1++) {
                                rowsPanel = new JPanel();
                                rowsPanel.setBackground(RColor1());
                                Window.viewerBoard1.revalidate();
                                Window.viewerBoard1.add(rowsPanel);
                            }
                        }
                        Thread.sleep(1000);
                        Window.viewerBoard1.removeAll();
                        Window.viewerBoard1.revalidate();
                        Window.viewerBoard1.repaint();
                    }
                    catch (InterruptedException ex) {
                        java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                    }
                } while (Window.randomize1.isSelected());
            }
        }
        // blocksArray2
        // blocksArray3
    }
    
    公共类块{
    公共无效initBlocks1(){
    BlockThread1 thread1=新的BlockThread1();
    thread1.start();
    }
    //initBlocks2
    //initBlocks3
    容器容器=Window.Viewboard1;
    JPanel[][]blocksArray1=新JPanel[4][4];
    杰帕内尔·科尔斯潘;
    杰帕内尔·罗斯潘;
    Random random1=新的Random();
    专用颜色RColor1(){
    返回新颜色(random1.nextInt(255)、random1.nextInt(255)、random1.nextInt(255)/*、random.nextInt(255)*/);
    }
    类BlockThread1扩展线程{
    @凌驾
    公开募捐{
    container.setLayout(新的GridLayout(4,4));
    做{
    试一试{
    for(int cols1=0;cols1
    我不知道你为什么需要锁。你不能把你已经比较过的点存储在一个集合中,然后在比较任何一对点时做一个if检查,看看其中一个是否在那个集合中吗?@cricket_007我不知道这个词,我的意思是当我点击“matcher”按钮时,它会在第一个数组中锁定[0][0]的颜色,在第二个数组中搜索[0][0]的颜色,然后在第三个数组上。等等。。。。。。。。。。。。。。。我在回答我的第三个问题吗?集合,散列。这些Java类不允许重复,并且有一个常量时间
    contains
    方法。如果将坐标存储在此数据结构中,可以快速查看是否比较了两对坐标。如果有,那么不要重新分配它们的颜色,使它们“锁定”@cricket\u 007谢谢,我将学习如何使用
    HashSet