0

This is in Java 8, using java swing and netbeans ide. When I run it, the positions of the shapes are messing up, and not all the clicks are detected. Here's my code:

public class mouselistenerandtictactoe extends JPanel 
    implements ActionListener, MouseListener 
{

    static boolean clicked = false;

    static int clicks = 0;

    static int[][] rowcolumn = {{3,3,3},
                                {3,3,3},
                                {3,3,3}};

    //gonna make these arrays

    static int mousex;
    static int mousey;

    //not these, these are fine
    static int[] x = new int[9];
    static int[] y = new int[9];

    private int checkwin(){

        for (int i = 0; i < rowcolumn[0].length; i++){
            if (rowcolumn[i][0] == rowcolumn[i][1] && rowcolumn[i][1] == rowcolumn[i][2] && rowcolumn[i][0] != 3){
                return rowcolumn[i][0];
            }
        }
        for (int i = 0; i < rowcolumn[0].length; i++){
            if (rowcolumn[0][i] == rowcolumn[1][i] && rowcolumn[1][i] == rowcolumn[2][i] && rowcolumn[0][i] != 3){
                return rowcolumn[0][i];
            }
        }
        if (rowcolumn[0][0] == rowcolumn[1][1] && rowcolumn[1][1] == rowcolumn[2][2] && rowcolumn[0][0] != 3){
                return rowcolumn[0][0];
        }
        if (rowcolumn[0][2] == rowcolumn[1][1] && rowcolumn[1][1] == rowcolumn[2][0] && rowcolumn[1][1] != 3){
                return rowcolumn[0][2];
        }

        if (clicks == 9){
            return 2;
        }

        return 3;
    }

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.add(new mouselistenerandtictactoe());
    frame.setBounds(100, 100, 700, 700);
    frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

Timer timer;

public mouselistenerandtictactoe() {
    addMouseListener(this);

    timer = new Timer(0, this);
    timer.setInitialDelay(0);
    timer.setDelay(50);
    timer.start();
}

    @Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
            doDrawing(g);
}

    private void doDrawing(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.BLACK);

        Dimension size = getSize();
        Insets insets = getInsets();

        int w = size.width - insets.left - insets.right;
        int h = size.height - insets.top - insets.bottom;

        g2d.drawLine(0, h/3, w, h/3);
        g2d.drawLine(0, h/3*2, w, h/3*2);
        g2d.drawLine(w/3, 0, w/3, h);
        g2d.drawLine(w/3*2, 0, w/3*2, h);

        clicking(g);

        int counter;
        counter = 0;

        for (int[] rowcolumn1 : rowcolumn) {
            for (int j = 0; j < rowcolumn1.length; j++) {
                if (rowcolumn1[j] == 1) {
                    drawshape(g, x[counter], y[counter], "C");
                }
                else if (rowcolumn1[j] == 0) {
                    drawshape(g, x[counter], y[counter], "X");
                }
                counter++;
            }
        }



        if (checkwin() == 0){
            g2d.setColor(Color.red);
            g2d.fillRect(0, 0, w, h);
            g2d.setColor(Color.white);
            g2d.drawString("X WINS!!", w/2-"X WINS!!".length()*4, h/2);

        } else if (checkwin() == 1){
            g2d.setColor(Color.BLUE);
            g2d.fillRect(0, 0, w, h);
            g2d.setColor(Color.white);
            g2d.drawString("O WINS!!", w/2-"O WINS!!".length()*4, h/2);

        } else if (checkwin() == 2){
            g2d.setColor(Color.MAGENTA);
            g2d.fillRect(0, 0, w, h);
            g2d.setColor(Color.white);
            String o = "CATS GAME! NO ONE WINS!!";
            g2d.drawString("CATS GAME! NO ONE WINS!!", w/2-o.length()*4, h/2);

        }


    }


    private void clicking(Graphics g){

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.BLACK);

        Dimension size = getSize();
        Insets insets = getInsets();

        int w = size.width - insets.left - insets.right;
        int h = size.height - insets.top - insets.bottom;

        if (clicked == true){
            if (mousex <= w/3 && (rowcolumn[0][0] == 3 || rowcolumn[1][0] == 3 || rowcolumn[2][0] == 3)){
                x[clicks] = w/6;

                if (mousey <= h/3 && rowcolumn[0][0] == 3){
                    y[clicks] = h/6;
                    rowcolumn[0][0] = clicks%2;
                    clicks++;
                }

                if (mousey >= h/3*2 && rowcolumn[1][0] == 3){
                    y[clicks] = h/6*5;
                    rowcolumn[1][0] = clicks%2;
                    clicks++;
                }

                if (mousey > h/3 && mousey < h/3*2 && rowcolumn[2][0] == 3){
                    y[clicks] = h/6*3;
                    rowcolumn[2][0] = clicks%2;
                    clicks++;
                }

            }

            if (mousex >= w/3*2 && (rowcolumn[0][1] == 3 || rowcolumn[1][1] == 3 || rowcolumn[2][1] == 3)){
                x[clicks] = w/6*5;

                if (mousey <= h/6*2 && rowcolumn[0][1] == 3){
                    y[clicks] = h/6;
                    rowcolumn[0][1] = clicks%2;
                    clicks++;
                }

                if (mousey >= h/3*2 && rowcolumn[1][1] == 3){
                    y[clicks] = h/6*5;
                    rowcolumn[1][1] = clicks%2;
                    clicks++;
                }

                if (mousey > h/3 && mousey < h/3*2 && rowcolumn[2][1] == 3){
                    y[clicks] = h/6*3;
                    rowcolumn[2][1] = clicks%2;
                    clicks++;
                }

            }

            if (mousex < w/3*2 && mousex > w/3 && (rowcolumn[0][2] == 3 || rowcolumn[1][2] == 3 || rowcolumn[2][2] == 3)){
                x[clicks] = w/6*3;

                if (mousey <= h/3 && rowcolumn[0][2] == 3){
                    y[clicks] = h/6;
                    rowcolumn[0][2] = clicks%2;
                    clicks++;
                }

                if (mousey >= h/3*2 && rowcolumn[1][2] == 3){
                    y[clicks] = h/6*5;
                    rowcolumn[1][2] = clicks%2;
                    clicks++;
                }

                if (mousey > h/3 && mousey < h/3*2 && rowcolumn[2][2] == 3){
                    y[clicks] = h/6*3;
                    rowcolumn[2][2] = clicks%2;
                    clicks++;
                }

            }

            clicked = false;
        }
    }

    private void drawshape(Graphics g, int x, int y, String Shape){
        Graphics2D g2d = (Graphics2D) g;

        Dimension size = getSize();
        Insets insets = getInsets();

        int w = size.width - insets.left - insets.right;
        int h = size.height - insets.top - insets.bottom;

        System.out.println(x+" "+ y);
        if (Shape.equals("C")){
            g2d.setColor(Color.blue);
            g2d.drawOval(x-w/6, y-h/6, w/3, h/3);
        }
        if (Shape.equals("X")){
            g2d.setColor(Color.red);
            g2d.drawLine(x-w/6, y-h/6, x+w/6, y+h/6);
            g2d.drawLine(x-w/6, y+h/6, x+w/6, y-h/6);
        }

    }

public void actionPerformed(ActionEvent e) {
    repaint();
}

    @Override
public void mouseClicked(MouseEvent e) {
        mousex = e.getX();
        mousey = e.getY();

        clicked = true;

}

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

}

Could someone explain why my shapes don't look like a tic-tac-toe board (i.e. 3x3 matrix) and why not all mouse clicks are bring detected?

J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
  • 1
    Seems to me like an issue with integer division. In general though, the code could be improved. A good improvement would be using rectangles and an even better would be components in a [GridLayout](http://docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html). Depends on what you want to use. – Obicere Jan 14 '15 at 17:24
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). But I have to agree with the suggestion of @Obicere re. components in a grid layout. That's how I'd approach this problem. That is how [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556) is achieved. – Andrew Thompson Jan 14 '15 at 23:27
  • You don't need a `Timer`, that's kind of overkill. When `mouseClicked` is called, you can execute ALL you basic logic right there and repaint the component. Take a look at [Working with Geometry](http://docs.oracle.com/javase/tutorial/2d/geometry/index.html), which provides you with a means by which you can draw shapes, but also perform "click" detection on them... – MadProgrammer Jan 15 '15 at 01:02

0 Answers0