从使用鼠标侦听器选择的对象获取信息

时间:2014-02-09 23:33:41

标签: java

所以基本上我试图制作Set卡游戏的想法是从4x3板卡中选择3张牌来确定它们是全部匹配还是全部相反并显示消息。我已经制作了卡片,它们都具有阴影,形状,颜色和数量的值。我遇到麻烦的是当我点击卡片时我能够将其删除。我想要做的是点击一张2个红色虚线曲线的卡片,让它返回2,1,2,3并将其加到每个的总和上。然后在选择三张牌后确定它们是否匹配。我甚至不知道如何去做这件事。我唯一真正尝试过的是将我点击的卡片保存到一个新对象。

package assignment3;
import acm.program.GraphicsProgram;
import acm.graphics.*;

import java.awt.event.MouseEvent;
import java.util.Random;

public class SetGame extends GraphicsProgram
{
    private int APP_WIDTH = 470;
    private int APP_HEIGHT = 300;
    GObject Card[][];

    public void run()
    {
        setSize(APP_WIDTH,APP_HEIGHT);

        for(int row = 0;row < 3; row++)
        {
            for(int col = 0; col < 4; col++)
            {
                Card[][] setCard = new Card[4][3];
                setCard[col][row] = new Card(col*60, row*60, getRandomNum()+1,getRandomNum()+1,getRandomNum()+1,getRandomNum()+1);
                add(setCard[col][row]);
            }
            println();
        }
        addMouseListeners();
    }


    public int getPigment(int num)
    {
        int pigment = num;
        String[] color = new String[3];     
        color[0] = "red";
        color[1] = "green";
        color[2] = "purple";
        return pigment;     
    }

    //gets a random shape
    public int getShape(int num)
    {
        int shape = num;
        String[] Shape = new String[3];     
        Shape[0] = "circle";
        Shape[1] = "diamond";
        Shape[2] = "squiggle";
        return shape;           
    }

    public int getShade(int num)
    {
        int shade = num;
        String[] Shade = new String[3];     
        Shade[0] = "solid";
        Shade[1] = "dashed";
        Shade[2] = "hollow";
        return shade;
    }

    public int getRandomNum()
    {
        Random number = new Random();
        int num = number.nextInt(3);
        return num;
    }


    public void mouseClicked(MouseEvent e)
    {
        GObject whichCard = getElementAt(e.getX(), e.getY());
        if(whichCard == null)return;
        remove(whichCard);
    }

}

这是另一个类的构造函数

package assignment3;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Random;

import acm.program.GraphicsProgram;
import acm.graphics.*;

//Written by Dan Mattwig
//This program is built to display properties for cards

public class Card extends GCompound
{       
    private int color;
    private int shape;
    private int shading;
    private int number;


    public Card(int X,int Y,int color,int shape, int shading, int number)
    {
        setLocation(X,Y);
        this.color = color;
        this.shape = shape;
        this.shading = shading;
        this.number = number;
        int image = (color * 3) + (shape * 9) + (shading * 27) + (number - 39);
        GImage card = new GImage("images/"+image+".gif");
        add(card,X,Y);      
    }




}

1 个答案:

答案 0 :(得分:0)

点击后,您需要确定事件来源是否代表Card,例如......

if (whichCard instanceof Card) {
    ...
}

然后,您需要确定Card的属性,但是您没有提供执行此操作的方法。您需要提供一系列的getter,以便获得所需的信息,例如......

public int getColor() {
    return color;
}

public int getShape() {
    return shape;
}

public int getShading() {
    return shading;
}

public int getNumber() {
    return number;
}

此时,您需要开始做出决定。您可以在点击时将每个Card添加到某种List并在稍后阶段处理它,或者在点击时进行比较,这取决于您。

无论如何,我很想使用Comparable界面......