内存游戏图形java [英] memory game graphics java

查看:262
本文介绍了内存游戏图形java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写的程序是一个4x4的内存游戏。在这16个框中将是一对从0-7的整数。我已经将所有已经随机化和正确编码。现在我想弄清楚如何配合颜色与相应的整数每次鼠标单击框。

I'm coding a program that is a 4x4 memory game. Within these 16 boxes will be a pair of integers from 0-7. I have all of that already randomized and coded correctly. Now I'm trying to figure out how to pair the colors with the corresponding integers each time the mouse clicks over the box.

这里大部分的代码。我知道这个游戏的逻辑还没有开始,但我现在更专注于displayHit方法和setColor方法。

Here most of the code. I know the logic for this game isn't started yet, but I'm more focused on the displayHit method and setColor method right now. Just posting the whole code because maybe I messed up somewhere else.

/*Sets the background of your memory board to black*/
public void init() 
{
    setSize(400,400);
    setBackground(Color.BLACK);
    buildBoard(4);

}   
/*This is main in java applets
    You may need to add (not change) a couple things in this method
 */
public void paint(Graphics canvas)
{
    if(firstRun) //for the first run we need to build our random board
    {

        print2DArray(board);
        buildBoard(4);
        firstRun = false;
    } 
    else // once our board is built we will display the game
    {
        displayGame(canvas);
        if (mouseClicked) // if the mouse has been clicked
        {
            displayHit(canvas);//find which box the user clicked
            mouseClicked = false;
        }
    }
}

/*
    DO NOT change this method
    determines if the mouse has been pressed
    sets x and y Mouse to the location of the mouse arrow
    redraws the image
 */
public boolean mouseDown(Event e, int x, int y ) 
{
    mouseClicked = true; 
    xMouse = x;
    yMouse = y;
    repaint();
    return true;
}

/*DO NOT change this method
    redraws the scene
 */
public void update ( Graphics g ) 
{
    paint(g);

}

/*
    pre: none
    post: build an array that holds the memory values for a board of size x size
    the board will hold two of each int from 0 to size randomly placed in the array
 */


public void buildBoard(int s)

{
    int a = 4;
    for (int row = 0; row < a; row++)
        for (int column = 0; column < a; column++)
        {

            board[row][column] = count++ % 8;
        }
    for(int row = 0; row < 4; row++)

        for(int column = 0; column < 4; column ++) 
        {
            int x = (int)Math.floor(Math.random()*4);
            int y = (int)Math.floor(Math.random()*4);
            temp = board[row][column];
            board[row][column] = board[x][y];
            board[x][y] = temp;


        }
}
public static void print2DArray(int[][] arr)
{
    for (int row = 0; row < arr.length; row++)
    {
        for (int col = 0; col < arr[row].length; col++)
        {
            System.out.print(arr[row][col] + " ");
        }
        System.out.println();
    }
}





public void displayGame(Graphics canvas)
{
    canvas.setColor(Color.WHITE);

    for(int i =0; i < 400; i+= WIDTH)
        for(int j = 0; j < 400; j+= WIDTH)
            canvas.drawRect(i, j, WIDTH, WIDTH);
}

/*
    Pre: xMouse and yMouse have been initialized
    Post: A circle is displayed in the correct box on the screen
    Currently the circle is displayed at the mouse location
 */
public void displayHit(Graphics g)
{
    buildBoard(temp);
    setColor(g);
    centerHit(xMouse, xMouse);
    g.fillOval(xMouse, yMouse, 40, 40);
}

public void setColor(Graphics g)
{

    switch(temp)
    {
    case 0: g.setColor(Color.RED);
    break;
    case 1: g.setColor(Color.GREEN);
    break;
    case 2: g.setColor(Color.BLUE);
    break;
    case 3: g.setColor(Color.ORANGE);
    break;
    case 4: g.setColor(Color.CYAN);
    break;
    case 5: g.setColor(Color.MAGENTA);
    break;
    case 6: g.setColor(Color.PINK);
    break;
    case 7: g.setColor(Color.YELLOW);
    break;
    }

}
public void centerHit(int centerX, int centerY)
{
    {
        if ((xMouse > 0) && (xMouse <=100))
            xMouse = 33;
        else if ((xMouse > 100) && (xMouse <=200))
            xMouse = 133;
        else if ((xMouse > 200) && (xMouse <=300))
            xMouse = 233;
        else if ((xMouse > 300) && (xMouse <=400))
            xMouse = 333;
    }
    {
        if ((yMouse > 0) && (yMouse <=100))
            yMouse = 33;
        else if ((yMouse > 100) && (yMouse <=200))
            yMouse = 133;
        else if ((yMouse > 200) && (yMouse <=300))
            yMouse = 233;
        else if ((yMouse > 300) && (yMouse <=400))
            yMouse = 333;
    }



}

}

推荐答案

从您的代码中看不出您在那里做了什么,

From your code there is not much to see about what you did there,

但我想你应该尝试以下:

but I guess you should try the following:

1)确保你有一个组件,我们称之为cmp。

1)make sure you have a component say we call it cmp.

2)确保每次点击框时cmp.repaint()被调用

2)make sure every time the box is clicked cmp.repaint() is called

3)确保调用displayHit (g)的函数从cmp的paintComponent(Graphics g)

3)make sure to call the displayHit(g) function from within cmp's paintComponent(Graphics g)

应该做

被操纵?

这应该是有帮助的:

public class ColorChanger extends JFrame implements MouseListener {
    private Color c =  Color.black;
    private JPanel p = new JPanel () {
        public void paintComponent(Graphics g) {
            g.setColor(c );
            g.drawRect(100, 100, 100, 100);
        }

    };

    public  ColorChanger() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(1024, 700));
        addMouseListener(this);
        this.getContentPane().add(p);
        pack();
        setVisible(true);
    }


    public static void main (String arg[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ColorChanger();
            }
        });
    }
    @Override
    public void mouseClicked(MouseEvent arg0) {
        randomizeColor();
        p.repaint();

    }
    private void randomizeColor() {
        switch ((int)(3*Math.random())) {
        case 0:c=Color.red;return;
        case 1:c=Color.blue;return;
        case 2:c=Color.green;return;
        }
    }
    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

}

这篇关于内存游戏图形java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆