pattmorter
pattmorter

Reputation: 991

mouseActionListener and mouseClick(MouseEvent e)

I implemented a MouseListener into my game for obvious reasons; the user needs to be able to click the board.

The problem isn't with clicking the board itself, its with the reset button resetBoard and the mouse listener for that. When I click the button, the board changes but when the click is released the board changes again. I can't find anywhere that explains why it changes on both the down click and release considering all my actions are under the public void mouseClicked(MouseEvent e) block. I have empty mouseReleased, mouseEntered, mouseExited, mousePressed.

Basically, can someone help me understand why the board is reset both on mouse down and Up (in the same motion) when only mouseClicked is not empty.

Oh and I know I don't need all of those imports, its just I did use them at one point and I forgot to take them out because I got too consumed with this current issue.

Sorry for the repetitive... well everything within this. Its the only way I currently know how to do it.

Thanks!

public class Blox implements ActionListener {

    private final String gameVersion = "Blox - v1.0.2";

    private ImageIcon purpleBlock = new ImageIcon();
    private ImageIcon redBlock = new ImageIcon();
    private ImageIcon blueBlock = new ImageIcon();
    private ImageIcon greenBlock = new ImageIcon();
    private ImageIcon closeImage = new ImageIcon();

    private JButton block1 = new JButton();
    private JButton block2 = new JButton();
    private JButton block3 = new JButton();
    private JButton block4 = new JButton();
    private JButton block5 = new JButton();
    private JButton block6 = new JButton();
    private JButton block7 = new JButton();

    private JButton block8 = new JButton();
    private JButton block9 = new JButton();
    private JButton block10 = new JButton();
    private JButton block11 = new JButton();
    private JButton block12 = new JButton();
    private JButton block13 = new JButton();
    private JButton block14 = new JButton();

    private JButton block15 = new JButton();
    private JButton block16 = new JButton();
    private JButton block17 = new JButton();
    private JButton block18 = new JButton();
    private JButton block19 = new JButton();
    private JButton block20 = new JButton();
    private JButton block21 = new JButton();

    private JButton block22 = new JButton();
    private JButton block23 = new JButton();
    private JButton block24 = new JButton();
    private JButton block25 = new JButton();
    private JButton block26 = new JButton();
    private JButton block27 = new JButton();
    private JButton block28 = new JButton();

    private JButton block29 = new JButton();
    private JButton block30 = new JButton();
    private JButton block31 = new JButton();
    private JButton block32 = new JButton();
    private JButton block33 = new JButton();
    private JButton block34 = new JButton();
    private JButton block35 = new JButton();

    private JButton block36 = new JButton();
    private JButton block37 = new JButton();
    private JButton block38 = new JButton();
    private JButton block39 = new JButton();
    private JButton block40 = new JButton();
    private JButton block41 = new JButton();
    private JButton block42 = new JButton();

    private JButton block43 = new JButton();
    private JButton block44 = new JButton();
    private JButton block45 = new JButton();
    private JButton block46 = new JButton();
    private JButton block47 = new JButton();
    private JButton block48 = new JButton();
    private JButton block49 = new JButton();

    // Error console init
    private JLabel errorMessage = new JLabel();
    private JPanel errorBg = new JPanel();
    private JButton closeError = new JButton();

    // Score/UI Inits
    private JButton resetBoard = new JButton("Reset Board");

    /**
     *  700 x 480 Frame Size
     *  Sets Up and displays initial scene.
     *
     */
    private void game() {

        // Initialize mainFrame
        JFrame mainFrame = new JFrame(gameVersion);
        Dimension minSize = new Dimension(700,480);
        mainFrame.setMinimumSize(minSize);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setLayout(null);
        mainFrame.setResizable(false);
        try {
            mainFrame.setContentPane(new JLabel(new ImageIcon(
                ImageIO.read(new File("bggrad.jpg")))));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Setup Block Images
        try {
            purpleBlock.setImage(ImageIO.read(new File("purpleBlock.png")));
            redBlock.setImage(ImageIO.read(new File("redBlock.png")));
            blueBlock.setImage(ImageIO.read(new File("blueBlock.png")));
            greenBlock.setImage(ImageIO.read(new File("greenBlock.png")));
            closeImage.setImage(ImageIO.read(new File("close.jpg")));
        } catch (IOException e) {
            e.printStackTrace();
        }   

        // Sets up error message panel
        // Only displayed if error occurs on ActionListener
        errorMessage.setText("");
        errorMessage.setForeground(Color.black);
        Font newLabelFont = new Font(errorMessage.getFont().getName(),
            Font.BOLD,errorMessage.getFont().getSize());
        errorMessage.setFont(newLabelFont); 
        closeError.setIcon(closeImage); 
        closeError.setBounds(0,0,24,24);
        errorBg.setBounds(0,430,700,30);
        errorBg.setBackground(Color.lightGray);
        errorBg.add(closeError);
        errorBg.add(errorMessage);
        errorBg.setVisible(false);

        // Sets up Score/UI Portions of Screen
        resetBoard.setBounds(60,363,175,25);
        resetBoard.setVisible(true);
        resetBoard.addActionListener(this);

        // Randomize and add Images to each JButton
        gameReset();

        // Add Listeners to each block
        block1.addActionListener(this);
        block2.addActionListener(this);
        block3.addActionListener(this);
        block4.addActionListener(this);
        block5.addActionListener(this);
        block6.addActionListener(this);
        block7.addActionListener(this);

        block8.addActionListener(this);
        block9.addActionListener(this);
        block10.addActionListener(this);
        block11.addActionListener(this);
        block12.addActionListener(this);
        block13.addActionListener(this);
        block14.addActionListener(this);

        block15.addActionListener(this);
        block16.addActionListener(this);
        block17.addActionListener(this);
        block18.addActionListener(this);
        block19.addActionListener(this);
        block20.addActionListener(this);
        block21.addActionListener(this);

        block22.addActionListener(this);
        block23.addActionListener(this);
        block24.addActionListener(this);
        block25.addActionListener(this);
        block26.addActionListener(this);
        block27.addActionListener(this);
        block28.addActionListener(this);

        block29.addActionListener(this);
        block30.addActionListener(this);
        block31.addActionListener(this);
        block32.addActionListener(this);
        block33.addActionListener(this);
        block34.addActionListener(this);
        block35.addActionListener(this);

        block36.addActionListener(this);
        block37.addActionListener(this);
        block38.addActionListener(this);
        block39.addActionListener(this);
        block40.addActionListener(this);
        block41.addActionListener(this);
        block42.addActionListener(this);

        block43.addActionListener(this);
        block44.addActionListener(this);
        block45.addActionListener(this);
        block46.addActionListener(this);
        block47.addActionListener(this);
        block48.addActionListener(this);
        block49.addActionListener(this);


        // Set Block Locations
        block1.setBounds(300,50,50,50);
        block2.setBounds(350,50,50,50);
        block3.setBounds(400,50,50,50);
        block4.setBounds(450,50,50,50);
        block5.setBounds(500,50,50,50);
        block6.setBounds(550,50,50,50);
        block7.setBounds(600,50,50,50);

        block8.setBounds(300,100,50,50);
        block9.setBounds(350,100,50,50);
        block10.setBounds(400,100,50,50);
        block11.setBounds(450,100,50,50);
        block12.setBounds(500,100,50,50);
        block13.setBounds(550,100,50,50);
        block14.setBounds(600,100,50,50);

        block15.setBounds(300,150,50,50);
        block16.setBounds(350,150,50,50);
        block17.setBounds(400,150,50,50);
        block18.setBounds(450,150,50,50);
        block19.setBounds(500,150,50,50);
        block20.setBounds(550,150,50,50);
        block21.setBounds(600,150,50,50);

        block22.setBounds(300,200,50,50);
        block23.setBounds(350,200,50,50);
        block24.setBounds(400,200,50,50);
        block25.setBounds(450,200,50,50);
        block26.setBounds(500,200,50,50);
        block27.setBounds(550,200,50,50);
        block28.setBounds(600,200,50,50);

        block29.setBounds(300,250,50,50);
        block30.setBounds(350,250,50,50);
        block31.setBounds(400,250,50,50);
        block32.setBounds(450,250,50,50);
        block33.setBounds(500,250,50,50);
        block34.setBounds(550,250,50,50);
        block35.setBounds(600,250,50,50);

        block36.setBounds(300,300,50,50);
        block37.setBounds(350,300,50,50);
        block38.setBounds(400,300,50,50);
        block39.setBounds(450,300,50,50);
        block40.setBounds(500,300,50,50);
        block41.setBounds(550,300,50,50);
        block42.setBounds(600,300,50,50);

        block43.setBounds(300,350,50,50);
        block44.setBounds(350,350,50,50);
        block45.setBounds(400,350,50,50);
        block46.setBounds(450,350,50,50);
        block47.setBounds(500,350,50,50);
        block48.setBounds(550,350,50,50);
        block49.setBounds(600,350,50,50);

        // Add blocks to frame
        mainFrame.getContentPane().add(block1);
        mainFrame.getContentPane().add(block2);
        mainFrame.getContentPane().add(block3);
        mainFrame.getContentPane().add(block4);
        mainFrame.getContentPane().add(block5);
        mainFrame.getContentPane().add(block6);
        mainFrame.getContentPane().add(block7);

        mainFrame.getContentPane().add(block8);
        mainFrame.getContentPane().add(block9);
        mainFrame.getContentPane().add(block10);
        mainFrame.getContentPane().add(block11);
        mainFrame.getContentPane().add(block12);
        mainFrame.getContentPane().add(block13);
        mainFrame.getContentPane().add(block14);

        mainFrame.getContentPane().add(block15);
        mainFrame.getContentPane().add(block16);
        mainFrame.getContentPane().add(block17);
        mainFrame.getContentPane().add(block18);
        mainFrame.getContentPane().add(block19);
        mainFrame.getContentPane().add(block20);
        mainFrame.getContentPane().add(block21);

        mainFrame.getContentPane().add(block22);
        mainFrame.getContentPane().add(block23);
        mainFrame.getContentPane().add(block24);
        mainFrame.getContentPane().add(block25);
        mainFrame.getContentPane().add(block26);
        mainFrame.getContentPane().add(block27);
        mainFrame.getContentPane().add(block28);

        mainFrame.getContentPane().add(block29);
        mainFrame.getContentPane().add(block30);
        mainFrame.getContentPane().add(block31);
        mainFrame.getContentPane().add(block32);
        mainFrame.getContentPane().add(block33);
        mainFrame.getContentPane().add(block34);
        mainFrame.getContentPane().add(block35);

        mainFrame.getContentPane().add(block36);
        mainFrame.getContentPane().add(block37);
        mainFrame.getContentPane().add(block38);
        mainFrame.getContentPane().add(block39);
        mainFrame.getContentPane().add(block40);
        mainFrame.getContentPane().add(block41);
        mainFrame.getContentPane().add(block42);

        mainFrame.getContentPane().add(block43);
        mainFrame.getContentPane().add(block44);
        mainFrame.getContentPane().add(block45);
        mainFrame.getContentPane().add(block46);
        mainFrame.getContentPane().add(block47);
        mainFrame.getContentPane().add(block48);
        mainFrame.getContentPane().add(block49);

        mainFrame.getContentPane().add(errorBg);

        mainFrame.getContentPane().add(resetBoard);

        //mainFrame.pack();
        mainFrame.setVisible(true);  
    }


    /**
     *  Checks onClick event for which JButton was clicked
     *  and changes JButton.setIcon() accordingly.
     *
     *  @param  none
     *  @return none 
     *  @see    java.awt.event.*
     *  @see    JButton
     *  @see    setIcon()
     *  @see    getIcon()
     *
     */
    public void actionPerformed(ActionEvent e) {

        if(e.getSource()==resetBoard) {
            System.out.println("Resetting");
            gameReset();
        }

        if(e.getSource()==errorBg || e.getSource()==errorMessage) {
            errorBg.setVisible(false);
        }

        if(e.getSource()==block1) {
            if(block1.getIcon()==purpleBlock) {
                block1.setIcon(blueBlock);
                block2.setIcon(redBlock);
                block8.setIcon(redBlock);
            } else if(block1.getIcon()==blueBlock) {
                block1.setIcon(redBlock);
                block2.setIcon(greenBlock);
                block8.setIcon(greenBlock);
            } else if(block1.getIcon()==redBlock) {
                block1.setIcon(greenBlock);
                block2.setIcon(blueBlock);
                block8.setIcon(blueBlock);
            } else if(block1.getIcon()==greenBlock) {
                block1.setIcon(blueBlock);
                block2.setIcon(redBlock);
                block8.setIcon(redBlock);
            } else {
                // Woops! Something went wrong!
                errorBg.setVisible(true);
                errorMessage.setText("Woops! Something went wrong! @" + e.getSource());
            }   
        }

        if(e.getSource()==block2) {
            if(block2.getIcon()==purpleBlock) {
                block2.setIcon(blueBlock);
                block1.setIcon(redBlock);
                block3.setIcon(redBlock);
                block9.setIcon(redBlock);
            } else if(block2.getIcon()==blueBlock) {
                block2.setIcon(redBlock);
                block1.setIcon(greenBlock);
                block3.setIcon(greenBlock);
                block9.setIcon(greenBlock);
            } else if(block2.getIcon()==redBlock) {
                block2.setIcon(greenBlock);
                block1.setIcon(blueBlock);
                block3.setIcon(blueBlock);
                block9.setIcon(blueBlock);
            } else if(block2.getIcon()==greenBlock) {
                block2.setIcon(blueBlock);
                block1.setIcon(redBlock);
                block3.setIcon(redBlock);
                block9.setIcon(redBlock);
            } else {
                // Woops! Something went wrong!
                errorBg.setVisible(true);
                errorMessage.setText("Woops! Something went wrong! @" + e.getSource());
            }   
        }

        if(e.getSource()==block3) {
            if(block3.getIcon()==purpleBlock) {
                block3.setIcon(blueBlock);
                block2.setIcon(redBlock);
                block4.setIcon(redBlock);
                block10.setIcon(redBlock);
            } else if(block3.getIcon()==blueBlock) {
                block3.setIcon(redBlock);
                block2.setIcon(greenBlock);
                block4.setIcon(greenBlock);
                block10.setIcon(greenBlock);
            } else if(block3.getIcon()==redBlock) {
                block3.setIcon(greenBlock);
                block2.setIcon(blueBlock);
                block4.setIcon(blueBlock);
                block10.setIcon(blueBlock);
            } else if(block3.getIcon()==greenBlock) {
                block3.setIcon(blueBlock);
                block2.setIcon(redBlock);
                block4.setIcon(redBlock);
                block10.setIcon(redBlock);
            } else {
                // Woops! Something went wrong!
                errorBg.setVisible(true);
                errorMessage.setText("Woops! Something went wrong! @" + e.getSource());
            }   
        }

        if(e.getSource()==block4) {
            if(block4.getIcon()==purpleBlock) {
                block4.setIcon(blueBlock);
                block3.setIcon(redBlock);
                block5.setIcon(redBlock);
                block11.setIcon(redBlock);
            } else if(block4.getIcon()==blueBlock) {
                block4.setIcon(redBlock);
                block3.setIcon(greenBlock);
                block5.setIcon(greenBlock);
                block11.setIcon(greenBlock);
            } else if(block4.getIcon()==redBlock) {
                block4.setIcon(greenBlock);
                block3.setIcon(blueBlock);
                block5.setIcon(blueBlock);
                block11.setIcon(blueBlock);
            } else if(block4.getIcon()==greenBlock) {
                block4.setIcon(blueBlock);
                block3.setIcon(redBlock);
                block5.setIcon(redBlock);
                block11.setIcon(redBlock);
            } else {
                // Woops! Something went wrong!
                errorBg.setVisible(true);
                errorMessage.setText("Woops! Something went wrong! @" + e.getSource());
            }   
        }

        if(e.getSource()==block5) {
            if(block5.getIcon()==purpleBlock) {
                block5.setIcon(blueBlock);
                block4.setIcon(redBlock);
                block6.setIcon(redBlock);
                block12.setIcon(redBlock);
            } else if(block5.getIcon()==blueBlock) {
                block5.setIcon(redBlock);
                block4.setIcon(greenBlock);
                block6.setIcon(greenBlock);
                block12.setIcon(greenBlock);
            } else if(block5.getIcon()==redBlock) {
                block5.setIcon(greenBlock);
                block4.setIcon(blueBlock);
                block6.setIcon(blueBlock);
                block12.setIcon(blueBlock);
            } else if(block5.getIcon()==greenBlock) {
                block5.setIcon(blueBlock);
                block4.setIcon(redBlock);
                block6.setIcon(redBlock);
                block12.setIcon(redBlock);
            } else {
                // Woops! Something went wrong!
                errorBg.setVisible(true);
                errorMessage.setText("Woops! Something went wrong! @" + e.getSource());
            }   
        }

        if(e.getSource()==block5) {
            if(block5.getIcon()==purpleBlock) {
                block5.setIcon(blueBlock);
                block4.setIcon(redBlock);
                block6.setIcon(redBlock);
                block12.setIcon(redBlock);
            } else if(block5.getIcon()==blueBlock) {
                block5.setIcon(redBlock);
                block4.setIcon(greenBlock);
                block6.setIcon(greenBlock);
                block12.setIcon(greenBlock);
            } else if(block5.getIcon()==redBlock) {
                block5.setIcon(greenBlock);
                block4.setIcon(blueBlock);
                block6.setIcon(blueBlock);
                block12.setIcon(blueBlock);
            } else if(block5.getIcon()==greenBlock) {
                block5.setIcon(blueBlock);
                block4.setIcon(redBlock);
                block6.setIcon(redBlock);
                block12.setIcon(redBlock);
            } else {
                // Woops! Something went wrong!
                errorBg.setVisible(true);
                errorMessage.setText("Woops! Something went wrong! @" + e.getSource());
            }   
        }

        if(e.getSource()==block6) {
            if(block6.getIcon()==purpleBlock) {
                block6.setIcon(blueBlock);
                block5.setIcon(redBlock);
                block7.setIcon(redBlock);
                block13.setIcon(redBlock);
            } else if(block6.getIcon()==blueBlock) {
                block6.setIcon(redBlock);
                block5.setIcon(greenBlock);
                block7.setIcon(greenBlock);
                block13.setIcon(greenBlock);
            } else if(block6.getIcon()==redBlock) {
                block6.setIcon(greenBlock);
                block5.setIcon(blueBlock);
                block7.setIcon(blueBlock);
                block13.setIcon(blueBlock);
            } else if(block6.getIcon()==greenBlock) {
                block6.setIcon(blueBlock);
                block5.setIcon(redBlock);
                block7.setIcon(redBlock);
                block13.setIcon(redBlock);
            } else {
                // Woops! Something went wrong!
                errorBg.setVisible(true);
                errorMessage.setText("Woops! Something went wrong! @" + e.getSource());
            }   
        }

        if(e.getSource()==block7) {
            if(block7.getIcon()==purpleBlock) {
                block7.setIcon(blueBlock);
                block6.setIcon(redBlock);
                block14.setIcon(redBlock);
                //block13.setIcon(redBlock);
            } else if(block7.getIcon()==blueBlock) {
                block7.setIcon(redBlock);
                block6.setIcon(greenBlock);
                block14.setIcon(greenBlock);
                //block13.setIcon(greenBlock);
            } else if(block7.getIcon()==redBlock) {
                block7.setIcon(greenBlock);
                block6.setIcon(purpleBlock);
                block14.setIcon(purpleBlock);
                //block13.setIcon(blueBlock);
            } else if(block7.getIcon()==greenBlock) {
                block7.setIcon(purpleBlock);
                block6.setIcon(blueBlock);
                block14.setIcon(blueBlock);
                //block13.setIcon(redBlock);
            } else {
                // Woops! Something went wrong!
                errorBg.setVisible(true);
                errorMessage.setText("Woops! Something went wrong! @" + e.getSource());
            }   
        }

        if(e.getSource()==block8) {
            if(block8.getIcon()==purpleBlock) {
                block8.setIcon(blueBlock);
                block1.setIcon(redBlock);
                block9.setIcon(redBlock);
                block15.setIcon(redBlock);
            } else if(block8.getIcon()==blueBlock) {
                block8.setIcon(redBlock);
                block1.setIcon(greenBlock);
                block9.setIcon(greenBlock);
                block15.setIcon(greenBlock);
            } else if(block8.getIcon()==redBlock) {
                block8.setIcon(greenBlock);
                block1.setIcon(purpleBlock);
                block9.setIcon(purpleBlock);
                block15.setIcon(purpleBlock);
            } else if(block8.getIcon()==greenBlock) {
                block8.setIcon(purpleBlock);
                block1.setIcon(blueBlock);
                block9.setIcon(blueBlock);
                block15.setIcon(blueBlock);
            } else {
                // Woops! Something went wrong!
                errorBg.setVisible(true);
                errorMessage.setText("Woops! Something went wrong! @" + e.getSource());
            }   
        }

        if(e.getSource()==block9) {
            if(block9.getIcon()==purpleBlock) {
                block9.setIcon(blueBlock);
                block2.setIcon(redBlock);
                block10.setIcon(redBlock);
                block8.setIcon(redBlock);
                block16.setIcon(redBlock);
            } else if(block9.getIcon()==blueBlock) {
                block9.setIcon(redBlock);
                block2.setIcon(greenBlock);
                block10.setIcon(greenBlock);
                block8.setIcon(greenBlock);
                block16.setIcon(greenBlock);
            } else if(block9.getIcon()==redBlock) {
                block9.setIcon(greenBlock);
                block2.setIcon(purpleBlock);
                block10.setIcon(purpleBlock);
                block8.setIcon(purpleBlock);
                block16.setIcon(purpleBlock);
            } else if(block9.getIcon()==greenBlock) {
                block9.setIcon(purpleBlock);
                block2.setIcon(blueBlock);
                block10.setIcon(blueBlock);
                block8.setIcon(blueBlock);
                block16.setIcon(blueBlock);
            } else {
                // Woops! Something went wrong!
                errorBg.setVisible(true);
                errorMessage.setText("Woops! Something went wrong! @" + e.getSource());
            }   
        }
    }

    public void gameReset() {

        // Add Tiles to mainFrame
        block1.setIcon(randomizer());
        block2.setIcon(randomizer());
        block3.setIcon(randomizer());
        block4.setIcon(randomizer());
        block5.setIcon(randomizer());
        block6.setIcon(randomizer());
        block7.setIcon(randomizer());

        block8.setIcon(randomizer());
        block9.setIcon(randomizer());
        block10.setIcon(randomizer());
        block11.setIcon(randomizer());
        block12.setIcon(randomizer());
        block13.setIcon(randomizer());
        block14.setIcon(randomizer());

        block15.setIcon(randomizer());
        block16.setIcon(randomizer());
        block17.setIcon(randomizer());
        block18.setIcon(randomizer());
        block19.setIcon(randomizer());
        block20.setIcon(randomizer());
        block21.setIcon(randomizer());

        block22.setIcon(randomizer());
        block23.setIcon(randomizer());
        block24.setIcon(randomizer());
        block25.setIcon(randomizer());
        block26.setIcon(randomizer());
        block27.setIcon(randomizer());
        block28.setIcon(randomizer());

        block29.setIcon(randomizer());
        block30.setIcon(randomizer());
        block31.setIcon(randomizer());
        block32.setIcon(randomizer());
        block33.setIcon(randomizer());
        block34.setIcon(randomizer());
        block35.setIcon(randomizer());

        block36.setIcon(randomizer());
        block37.setIcon(randomizer());
        block38.setIcon(randomizer());
        block39.setIcon(randomizer());
        block40.setIcon(randomizer());
        block41.setIcon(randomizer());
        block42.setIcon(randomizer());

        block43.setIcon(randomizer());
        block44.setIcon(randomizer());
        block45.setIcon(randomizer());
        block46.setIcon(randomizer());
        block47.setIcon(randomizer());
        block48.setIcon(randomizer());
        block49.setIcon(randomizer());
    }

    public ImageIcon randomizer() {

        Random r = new Random();
        int rNum = r.nextInt(4);

        if(rNum==0) {
            return purpleBlock;
        } else if(rNum==1) {
            return redBlock;
        } else if(rNum==2) {
            return greenBlock;
        } else {
            return blueBlock;
        }
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Blox().game();
            }
        });
    }
}

Upvotes: 1

Views: 566

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

Don't use MouseListener with JButtons but instead use ActionListeners. This is all well spelled out in the button tutorial, and I strongly urge you to read them ASAP.

One major problem that I see with your code is to be found here:

public ImageIcon randomizer() {

    try {
        purpleBlock.setImage(ImageIO.read(new File("purpleBlock.png")));
        redBlock.setImage(ImageIO.read(new File("redBlock.png")));
        blueBlock.setImage(ImageIO.read(new File("blueBlock.png")));
        greenBlock.setImage(ImageIO.read(new File("greenBlock.png")));
    } catch (IOException e) {
        e.printStackTrace();
    }   

    Random r = new Random();
    int rNum = r.nextInt(4);

    if(rNum==0) {
        return purpleBlock;
    } else if(rNum==1) {
        return redBlock;
    } else if(rNum==2) {
        return greenBlock;
    } else {
        return blueBlock;
    }
}

You re-read all your color block images from the file whenever the randomizer() method is called. There's no need to do this since the ImageIcons have already been created with the proper images, but not only that, this can potentially slow your program down painfully.

Upvotes: 3

Code-Apprentice
Code-Apprentice

Reputation: 83517

Use an ActionListener for buttons, rather than a MouseListener. Also, when you have variable names that only differ by a number suffix, you should look at using an array instead. You can significantly reduce the number of lines of code. You will only need 1 declaration instead of 49 and most sections of code that access the button variables can be reduced to 1 line each plus an enclosing for loop (1 line plus a closing brace).

Upvotes: 1

Related Questions