2012-04-25 93 views
0

我正在編寫一個4x4內存遊戲的程序。在這16個盒子裏將會有一對從0到7的整數。我已經將所有這些都進行了隨機化並正確編碼。現在我試圖弄清楚如何在每次鼠標點擊盒子時將顏色與相應的整數進行配對。內存遊戲圖形java

這裏的大部分代碼。我知道這個遊戲的邏輯尚未開始,但我現在更關注displayHit方法和setColor方法。只是發佈整個代碼,因爲也許我在其他地方搞砸了。

/*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; 
    } 



} 

}

+0

爲什麼你有兩個代碼塊是做同樣的事情?爲什麼不直接'在'temp'上切換',調用'g.setcolor',甚至不用'color'來打擾? – 2012-04-25 06:51:52

+0

不知道哈哈我會解決這個問題。我在看以前的實驗室,並且包含了一個稍有不同的switch語句。 – user1215307 2012-04-25 06:53:12

+0

解決了這個問題並且調用了將0-7分配給2D陣列的buildBoard方法,但是現在看起來好像它只是選擇隨機顏色 – user1215307 2012-04-25 07:02:08

回答

0

從你的代碼沒有太多瞭解您那裏做什麼,

,但我想你應該嘗試以下方法:

1)確保您有一個組件說我們稱之爲cmp。

2)確保每次被點擊框cmp.repaint()被調用

3)確保CMP的的paintComponent(圖形摹內調用displayHit(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 

    } 

} 
+0

我會更新代碼 – user1215307 2012-04-25 07:06:46

+0

然後它並不奇怪它不起作用 – 2012-04-25 07:07:30

+0

我發起臨時性頂部是一個私有靜態int – user1215307 2012-04-25 07:10:53