8
\$\begingroup\$

My program detects what tile is being clicked and prints it to the console

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGrid implements MouseListener {

int scl = 50;
JPanel p;

public TestGrid() {

    JFrame frame = new JFrame("tenprint");
    JFrame.setDefaultLookAndFeelDecorated(true);
    frame.setSize(501, 501);
    frame.setUndecorated(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);

    p = new JPanel() {

        private static final long serialVersionUID = 1234567890L;

        @Override
        public void paintComponent(Graphics g) {
            p.setBackground(Color.black);
            Graphics2D g2 = (Graphics2D) g;
            for (int y = 0; y < 10; y++) {
                for (int x = 0; x < 10; x++) {
                    g2.setStroke(new BasicStroke(1f));
                    g2.setColor(Color.WHITE);
                    g2.fillRect(x * scl, y * scl, scl, scl);
                    g2.setColor(Color.BLACK);
                    g2.drawRect(x * scl, y * scl, scl, scl);

                }
            }
        }
    };
    frame.add(p);
    frame.addMouseListener(this);
    frame.setVisible(true);
}

@Override
public void mouseClicked(MouseEvent e) {
    System.out.println(e.getClickCount());

    System.out.println("Y: " + e.getY() + "\nX: " + e.getX());

    if (e.getX() <= 50 && e.getY() <= 50) {
        System.out.println("firstSqaure.");
    } else if (e.getX() <= 50 && e.getY() <= 100) {
        System.out.println("SecondSqaure.");
    } else if (e.getX() <= 50 && e.getY() <= 150) {
        System.out.println("thirdSqaure.");
    } else if (e.getX() <= 50 && e.getY() <= 200) {
        System.out.println("fourthSqaure.");
    } else if (e.getX() <= 50 && e.getY() <= 250) {
        System.out.println("fifthSqaure.");
    } else if (e.getX() <= 50 && e.getY() <= 300) {
        System.out.println("sixthSqaure.");
    } else if (e.getX() <= 50 && e.getY() <= 350) {
        System.out.println("seventhSqaure.");
    } else if (e.getX() <= 50 && e.getY() <= 400) {
        System.out.println("eighthSqaure.");
    } else if (e.getX() <= 50 && e.getY() <= 450) {
        System.out.println("ninthSqaure.");
    } else if (e.getX() <= 50 && e.getY() <= 500) {
        System.out.println("tenthSqaure.");
        // end of column 1

        // start of column 2
    } else if (e.getX() <= 100 && e.getY() <= 50) {
        System.out.println("eleventhSqaure.");
    } else if (e.getX() <= 100 && e.getY() <= 100) {
        System.out.println("twelthSqaure.");
    } else if (e.getX() <= 100 && e.getY() <= 150) {
        System.out.println("thirteenthSqaure.");
    } else if (e.getX() <= 100 && e.getY() <= 200) {
        System.out.println("fourteenthSqaure.");
    } else if (e.getX() <= 100 && e.getY() <= 250) {
        System.out.println("fifteenthSqaure.");
    } else if (e.getX() <= 100 && e.getY() <= 300) {
        System.out.println("sixteenthSqaure.");
    } else if (e.getX() <= 100 && e.getY() <= 350) {
        System.out.println("seventeenthSqaure.");
    } else if (e.getX() <= 100 && e.getY() <= 400) {
        System.out.println("eighteenthSqaure.");
    } else if (e.getX() <= 100 && e.getY() <= 450) {
        System.out.println("nineteenthSqaure.");
    } else if (e.getX() <= 100 && e.getY() <= 500) {
        System.out.println("twentiethSqaure.");
        // end of column 2

        // start of column 3
    } else if (e.getX() <= 150 && e.getY() <= 50) {
        System.out.println("twentyfirstSqaure.");
    } else if (e.getX() <= 150 && e.getY() <= 100) {
        System.out.println("twentysecondSqaure.");
    } else if (e.getX() <= 150 && e.getY() <= 150) {
        System.out.println("twentythirdSqaure.");
    } else if (e.getX() <= 150 && e.getY() <= 200) {
        System.out.println("twentyfourthSqaure.");
    } else if (e.getX() <= 150 && e.getY() <= 250) {
        System.out.println("twentyfifthSqaure.");
    } else if (e.getX() <= 150 && e.getY() <= 300) {
        System.out.println("twentysixthSqaure.");
    } else if (e.getX() <= 150 && e.getY() <= 350) {
        System.out.println("twentyseventhSqaure.");
    } else if (e.getX() <= 150 && e.getY() <= 400) {
        System.out.println("twentyeighthSqaure.");
    } else if (e.getX() <= 150 && e.getY() <= 450) {
        System.out.println("twentyninethSqaure.");
    } else if (e.getX() <= 150 && e.getY() <= 500) {
        System.out.println("thirtiethSqaure.");
        // end of column 3

        // start of column 4
    }

    else if (e.getX() <= 200 && e.getY() <= 50) {
        System.out.println("31");
    } else if (e.getX() <= 200 && e.getY() <= 100) {
        System.out.println("32");
    } else if (e.getX() <= 200 && e.getY() <= 150) {
        System.out.println("33");
    } else if (e.getX() <= 200 && e.getY() <= 200) {
        System.out.println("34");
    } else if (e.getX() <= 200 && e.getY() <= 250) {
        System.out.println("35");
    } else if (e.getX() <= 200 && e.getY() <= 300) {
        System.out.println("36");
    } else if (e.getX() <= 200 && e.getY() <= 350) {
        System.out.println("37");
    } else if (e.getX() <= 200 && e.getY() <= 400) {
        System.out.println("38");
    } else if (e.getX() <= 200 && e.getY() <= 450) {
        System.out.println("39");
    } else if (e.getX() <= 200 && e.getY() <= 500) {
        System.out.println("40");
        // end of column 4

        // start of column 5
    } else if (e.getX() <= 250 && e.getY() <= 50) {
        System.out.println("41");
    } else if (e.getX() <= 250 && e.getY() <= 100) {
        System.out.println("42");
    } else if (e.getX() <= 250 && e.getY() <= 150) {
        System.out.println("43");
    } else if (e.getX() <= 250 && e.getY() <= 200) {
        System.out.println("44");
    } else if (e.getX() <= 250 && e.getY() <= 250) {
        System.out.println("45");
    } else if (e.getX() <= 250 && e.getY() <= 300) {
        System.out.println("46");
    } else if (e.getX() <= 250 && e.getY() <= 350) {
        System.out.println("47");
    } else if (e.getX() <= 250 && e.getY() <= 400) {
        System.out.println("48");
    } else if (e.getX() <= 250 && e.getY() <= 450) {
        System.out.println("49");
    } else if (e.getX() <= 250 && e.getY() <= 500) {
        System.out.println("50");
        // end of column 5

        // start of column 6
    } else if (e.getX() <= 300 && e.getY() <= 50) {
        System.out.println("51");
    } else if (e.getX() <= 300 && e.getY() <= 100) {
        System.out.println("52");
    } else if (e.getX() <= 300 && e.getY() <= 150) {
        System.out.println("53");
    } else if (e.getX() <= 300 && e.getY() <= 200) {
        System.out.println("54");
    } else if (e.getX() <= 300 && e.getY() <= 250) {
        System.out.println("55");
    } else if (e.getX() <= 300 && e.getY() <= 300) {
        System.out.println("56");
    } else if (e.getX() <= 300 && e.getY() <= 350) {
        System.out.println("57");
    } else if (e.getX() <= 300 && e.getY() <= 400) {
        System.out.println("58");
    } else if (e.getX() <= 300 && e.getY() <= 450) {
        System.out.println("59");
    } else if (e.getX() <= 300 && e.getY() <= 500) {
        System.out.println("60.");
        // end of column 6

        // start of column 7
    } else if (e.getX() <= 350 && e.getY() <= 50) {
        System.out.println("60's");
    } else if (e.getX() <= 350 && e.getY() <= 100) {
        System.out.println("60's");
    } else if (e.getX() <= 350 && e.getY() <= 150) {
        System.out.println("60's");
    } else if (e.getX() <= 350 && e.getY() <= 200) {
        System.out.println("60's");
    } else if (e.getX() <= 350 && e.getY() <= 250) {
        System.out.println("60's");
    } else if (e.getX() <= 350 && e.getY() <= 300) {
        System.out.println("60's");
    } else if (e.getX() <= 350 && e.getY() <= 350) {
        System.out.println("60's");
    } else if (e.getX() <= 350 && e.getY() <= 400) {
        System.out.println("60's");
    } else if (e.getX() <= 350 && e.getY() <= 450) {
        System.out.println("60's");
    } else if (e.getX() <= 350 && e.getY() <= 500) {
        System.out.println("70's.");
        // end of column 7

        // start of column 8
    } else if (e.getX() <= 400 && e.getY() <= 50) {
        System.out.println("70's");
    } else if (e.getX() <= 400 && e.getY() <= 100) {
        System.out.println("70's");
    } else if (e.getX() <= 400 && e.getY() <= 150) {
        System.out.println("70's");
    } else if (e.getX() <= 400 && e.getY() <= 200) {
        System.out.println("70's");
    } else if (e.getX() <= 400 && e.getY() <= 250) {
        System.out.println("70's");
    } else if (e.getX() <= 400 && e.getY() <= 300) {
        System.out.println("70's");
    } else if (e.getX() <= 400 && e.getY() <= 350) {
        System.out.println("70's");
    } else if (e.getX() <= 400 && e.getY() <= 400) {
        System.out.println("70's");
    } else if (e.getX() <= 400 && e.getY() <= 450) {
        System.out.println("70's");
    } else if (e.getX() <= 400 && e.getY() <= 500) {
        System.out.println("80's.");
        // end of column 8

        // start of column 9
    } else if (e.getX() <= 450 && e.getY() <= 50) {
        System.out.println("80's");
    } else if (e.getX() <= 450 && e.getY() <= 100) {
        System.out.println("80's");
    } else if (e.getX() <= 450 && e.getY() <= 150) {
        System.out.println("80's");
    } else if (e.getX() <= 450 && e.getY() <= 200) {
        System.out.println("80's");
    } else if (e.getX() <= 450 && e.getY() <= 250) {
        System.out.println("80's");
    } else if (e.getX() <= 450 && e.getY() <= 300) {
        System.out.println("80's");
    } else if (e.getX() <= 450 && e.getY() <= 350) {
        System.out.println("80's");
    } else if (e.getX() <= 450 && e.getY() <= 400) {
        System.out.println("80's");
    } else if (e.getX() <= 450 && e.getY() <= 450) {
        System.out.println("80's");
    } else if (e.getX() <= 450 && e.getY() <= 500) {
        System.out.println("90's.");
        // end of column 9

        // start of column 10
    } else if (e.getX() <= 500 && e.getY() <= 50) {
        System.out.println("90's");
    } else if (e.getX() <= 500 && e.getY() <= 100) {
        System.out.println("90's");
    } else if (e.getX() <= 500 && e.getY() <= 150) {
        System.out.println("90's");
    } else if (e.getX() <= 500 && e.getY() <= 200) {
        System.out.println("90's");
    } else if (e.getX() <= 500 && e.getY() <= 250) {
        System.out.println("90's");
    } else if (e.getX() <= 500 && e.getY() <= 300) {
        System.out.println("90's");
    } else if (e.getX() <= 500 && e.getY() <= 350) {
        System.out.println("90's");
    } else if (e.getX() <= 500 && e.getY() <= 400) {
        System.out.println("90's");
    } else if (e.getX() <= 500 && e.getY() <= 450) {
        System.out.println("90's");
    } else if (e.getX() <= 500 && e.getY() <= 500) {
        System.out.println("100's.");
        // end of column 10
    }

}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new TestGrid();
        }
    });
}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {
    // do nothing here
}

@Override
public void mouseExited(MouseEvent e) {
    // do nothing here
}

}

Please let me know how awful this is and how to avoid using ~100 if statements

\$\endgroup\$
  • \$\begingroup\$ Are the tiles identical by design, or can they be different? \$\endgroup\$ – TimSparrow Oct 6 '17 at 12:40
  • \$\begingroup\$ They can be different @TimSparrow \$\endgroup\$ – Alex Ritchie Oct 6 '17 at 13:22
  • 1
    \$\begingroup\$ Please don't include solution in the question. You accepted the answer so this is enough to show that it's a good review. \$\endgroup\$ – Marc-Andre Oct 6 '17 at 13:36
15
\$\begingroup\$

The entirety of the if/else statements are redundent if we consider that if we divide the current location by the tile_size we get the correct tile number

int tile_size = 50;

int tile_x = e.getX() / tile_size;
int tile_y = e.getY() / tile_size;
System.out.println(String.format("square %d%d", tile_x, tile_y));

I am not a Java Developer so pardon my Java

\$\endgroup\$
  • \$\begingroup\$ Note that this will only work for non-negative coordinates. (Which is fine in this case) \$\endgroup\$ – CodesInChaos Oct 7 '17 at 9:18
7
\$\begingroup\$

Instead of implementing MouseListener, you can use the MouseAdapter class to implement only the method you need.

frame.addMouseListener( new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        //your implementation  
    }
});
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.