Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have wrote a code for game Minesweeper and there is no problem with the graphics,but there is some problems with MouseListener. Here is my code


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mines_v3.pkg0;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
/**
 *
 * @author Gevorg Hindoyan 
 * @version 3.0
 * @since 2012
 */
public class Mines_v30 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MineFrame frame = new MineFrame();
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        });
    }
}

class MineFrame extends JFrame {
    private int xq = 10;
    private int yq = 10;
    private int minq = 10;
    public MineFrame() {

        setSize((xq+1)*22-1, (yq+2)*22+44);
        setTitle("Mines v.3.0");

        Image ic = Toolkit.getDefaultToolkit().getImage("images/icon.png");

        setIconImage(ic);

        mainFields mf = new mainFields(xq,yq,minq);

        add(mf);
    }
}

class mainFields extends JComponent implements MouseListener{
    private int xq;
    private int yq;
    private int minq;
    private boolean loose;
    private int width;
    private int height;
    private boolean win() {
        int smq = 0;
        for(int i=1;i<=xq;i++) {
            for(int j=1;j<=yq;j++) {
                if(a[i][j]==-5 && b[i][j]==2) smq++;
            }
        }
        if(smq==minq) return true;
        return false;
    }
    private int a[][] = new int[100][100];
    private int b[][] = new int[100][100];
    private void init() {

        loose=false;
        // making all fields "0"
        for(int i=1;i<=xq;i++) {
            for(int j=1;j<=yq;j++) {
                a[i][j]=0;
                b[i][j]=0;
            }
        }

        //filing borders with -3
        for(int i=0;i<=xq+1;i++) {
            a[0][i]=-3;
            a[yq+1][i]=-3;
        }
        for(int i=0;i<=yq+1;i++) {
            a[i][0]=-3;
            a[i][yq+1]=-3;
        }

        //adding mines
        Random rand = new Random();
        int nx=0,ny=0;
        for(int i=0;i<minq;i++) {
            int 
            x = rand.nextInt()%xq+1,
            y = rand.nextInt()%yq+1;
            if(x==0 || y==0) {
                i--;
                continue;
            }
            if(x<0) x*=-1;
            if(y<0) y*=-1;

            if(i==0) {
                nx = x;
                ny = y;
            } else if(nx==x && ny==y) {
                i--;
                continue;
            }

            a[x][y]=-5;

        }

        //filling with numbers
        for(int i=1;i<=xq;i++) {
            for(int j=1;j<=yq;j++) {
                if(a[i][j]==-5) {
                    if(a[i-1][j]>=0) a[i-1][j]++;
                    if(a[i+1][j]>=0) a[i+1][j]++;
                    if(a[i][j+1]>=0) a[i][j+1]++;
                    if(a[i][j-1]>=0) a[i][j-1]++;
                    if(a[i+1][j+1]>=0) a[i+1][j+1]++;
                    if(a[i+1][j-1]>=0) a[i+1][j-1]++;
                    if(a[i-1][j+1]>=0) a[i-1][j+1]++;
                    if(a[i-1][j-1]>=0) a[i-1][j-1]++;
                }
            }
        }

        /*DEBUG: printing matrix*/
        for(int i=0;i<=xq+1;i++) {
            for(int j=0;j<=yq+1;j++) {
                if(a[i][j]<0)
                    System.out.print(a[i][j]+" ");
                else
                    System.out.print(" "+a[i][j]+" ");
            }
            System.out.println();
        }
        /* DEBUG */
    }
    public mainFields(int xq1,int yq1,int mq1) {

        xq=xq1;
        yq=yq1;
        minq=mq1;
        width = (xq+1)*22-1;
        height = (yq+2)*22+44;
        init();

    }

    @Override
    public void paintComponent(Graphics g) {

        //status bar & all fields
        Image smile = null;
        String smile_name = "images/smile";
        if(loose) smile_name+="2";
        else if(win()) smile_name+="3";
        else smile_name+="1";
        smile_name+=".png";
        try {
            smile = ImageIO.read(new File(smile_name));
        } catch(IOException e ) {
            e.printStackTrace();
        }        
        g.drawImage(smile, width/2-21,
                    0,
                    smile.getWidth(null), 
                    smile.getHeight(null), 
                    null);
        for(int i=1;i<=xq;i++) {
            for(int j=1;j<=yq;j++) {
                Image field = null;
                String fname = null;
                if(b[i][j]==0) {
                    fname = "datark";
                }
                else if(b[i][j]==2 && !loose) {
                    fname = "drosh";
                }
                else if(b[i][j]==2 && loose && a[i][j]!=-5) {
                    fname = "sxal_drosh";
                }
                else if(b[i][j]==1) {
                    if(a[i][j]==0) {
                        fname = "sxmats";
                    }
                    if(a[i][j]==1) {
                        fname = "mek";
                    }
                    if(a[i][j]==2) {
                        fname = "erku";
                    }
                    if(a[i][j]==3) {
                        fname = "ereq";
                    }
                    if(a[i][j]==4) {
                        fname = "chors";
                    }
                    if(a[i][j]==5) {
                        fname = "hing";
                    }
                    if(a[i][j]==6) {
                        fname = "vec";
                    }
                    if(a[i][j]==7) {
                        fname = "yot";
                    }
                    if(a[i][j]==8) {
                        fname = "ut";
                    }
                }
                fname+=".png";
                try {
                    field = ImageIO.read(new File("images/"+fname));
                } catch(IOException e) {
                    System.err.println("Error 1: can't find images");
                }
                g.drawImage(field,
                            (i-1)*22,
                            (j-1)*22+42,
                            null);
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {

    }

    @Override
    public void mouseReleased(MouseEvent me) {

    }

    @Override
    public void mouseEntered(MouseEvent me) {

    }

    @Override
    public void mouseExited(MouseEvent me) {

    }

    private void click(int i, int j) {

        b[i][j]=1;
        System.out.println("click() was called");
        repaint();

    }

    @Override
    public void mouseClicked(MouseEvent me) {
        int x = me.getX(),
            y = me.getY();

        int xrd = (int)x/22+1,
            yrd = (int)(y-42)/22+1;

        int but = me.getButton();

        System.out.println("Mouse clicked");

        if(but == MouseEvent.BUTTON1) {

            if(b[xrd][yrd]==1 || b[xrd][yrd]==2) {
                return;
            }
            click(xrd,yrd);
            repaint();
            this.repaint();

        }

        if(but == MouseEvent.BUTTON3) {

            if(b[xrd][yrd]==2) {
                b[xrd][yrd]=0;
            } else if(b[xrd][yrd]==0) {
                b[xrd][yrd]=2;
            }
            repaint(); 

        }
    }

}

EDIT

My function click() is not finished
And matrix b[][] is showing is that field is clicked matrix a[][] is showing is that filed mine(-5) or number(0,1,2,3,4,5,6,7,8)

The issue, as mentioned in chat, is that a click handler wasn't getting fired.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

You are probably forgetting to hook up your listener.

share|improve this answer
    
oh thank you!!!! –  geoh May 25 '12 at 4:46

You forgot to put addMouseListener(this); to your frame/panel

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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