2012-11-28 27 views
0
/** 
* 
* @author suchit 
* Date : 11/25/2012 
* History : 
*/ 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.GridLayout; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.io.File; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.JApplet; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.border.LineBorder; 

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


public class TicTacToe extends JApplet { 

    static Cell[] arrCell=new Cell[9]; 
    static int fsize=400; 
    static char drawCh; 
    static JLabel lblMsg = new JLabel("Start the game: Player1 mark X"); 
    static boolean gameOver = false; 
    static JFrame frame = new JFrame("TicTacToe Game"); 
    JButton btnStart = new JButton("New Game"); 
    static Image bg; 
    static File fn; 
    /** 
    * @param args the command line arguments 
    */ 
    public TicTacToe()  //default constructor 
    { 
     JPanel objPanel = new JPanel(new GridLayout(3, 3, 0, 0));  //creates j panel 
     for(int i=0;i<9;i++)    // add nine objects on jpanel 
     { 
      arrCell[i]=new Cell(); 
     } 
     for(int i=0;i<9;i++) 
     { 
      objPanel.add(arrCell[i]); 
     } 

     objPanel.setBorder(new LineBorder(Color.BLACK, 1));  // create border 
     //objPanel.add(btnStart);//add button 
     objPanel.add(lblMsg); // Display message 
     //btnStart.addActionListener(new StartAction());  // Add action listener to the button 



// Place the panel and the label to the applet 
     add(objPanel, BorderLayout.CENTER); 
     add(lblMsg, BorderLayout.SOUTH); 
     //add(btnStart,BorderLayout.NORTH); 
    } // end of constructor 
    public static void main(String[] args) { 
     // TODO code application logic here 

     frame.setSize(new Dimension(fsize,fsize)); 
     //frame.setPreferredSize(); 
     TicTacToe objTicTacToe = new TicTacToe(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 

     frame.add(objTicTacToe); 



    } // end of main 

class StartAction implements ActionListener 
    { 

     public void actionPerformed(ActionEvent e) 
     { 
      fsize++; 
      drawCh='A'; 
      gameOver=false; 
      for(int i=0; i<9;i++) 
      { 
       arrCell[i].value='A'; 
      } 
      frame.setSize(new Dimension(fsize,fsize)); 
      lblMsg.setText("Player1's Turn: mark X"); 
     } 
    } // end of class StartAction 

    public static boolean isGameOver()   // checks wather any one won and game is over 
{ 
    boolean winner=false; 
    boolean Draw=false; 
    //if any three consucutive rows will have same value then player won the game 
    if((arrCell[0].value ==drawCh && arrCell[1].value== drawCh &&arrCell[2].value==drawCh)) 
    { 
     winner=true; 
    } 
    if((arrCell[3].value ==drawCh && arrCell[4].value== drawCh &&arrCell[5].value==drawCh)) 
    { 
     winner=true; 
    } 
    if((arrCell[6].value ==drawCh && arrCell[7].value== drawCh &&arrCell[8].value==drawCh)) 
    { 
     winner=true; 
    } 
    if((arrCell[0].value ==drawCh && arrCell[3].value== drawCh &&arrCell[6].value==drawCh)) 
    { 
     winner=true; 
    } 
    if((arrCell[1].value ==drawCh && arrCell[4].value== drawCh &&arrCell[7].value==drawCh)) 
    { 
     winner=true; 
    } 
    if((arrCell[2].value ==drawCh && arrCell[5].value== drawCh &&arrCell[8].value==drawCh)) 
    { 
     winner=true; 
    } 
    if((arrCell[0].value ==drawCh && arrCell[4].value== drawCh &&arrCell[8].value==drawCh)) 
    { 
     winner=true; 
    } 
    if((arrCell[2].value ==drawCh && arrCell[4].value== drawCh &&arrCell[6].value==drawCh)) 
    { 
     winner=true; 
    } 

    if(winner) 
    { 
     if(drawCh=='X') 
      lblMsg.setText("Player1 Won, Game Over"); // player 1 won 
     else if(drawCh=='O') 
      lblMsg.setText("Player2 Won, Game Over"); // player 2 won 
     else 
      winner=false;  // no one won, its a default values of all boxes 

    } 
    for(int i=0;i<9;i++) 
    { 
     if(arrCell[i].value=='A') 
     { 
      Draw=false; 
      break; 
     } 
     else 
      { 
      Draw=true; 
      } 
    } 
if(Draw) 
{ 
    lblMsg.setText("Game Over : Result Draw"); 
} 
    System.out.println("Draw="+Draw); 
    return winner; 

} // end of isGameOver() 
public static class Cell extends JPanel 
{ 
    char value='A'; 
    public Cell() 
    { 
     this.setBorder(new LineBorder(Color.black, 1)); // Set cell's border 
     this.setPreferredSize(new Dimension(100,100)); 
     addMouseListener(new MouseListener()); 
    } 

    private class MouseListener extends MouseAdapter { 
/** Handle mouse click on a cell */ 
public void mouseClicked(MouseEvent e){ 

if(!gameOver && value=='A')  
{ 
     if(drawCh=='X')  // set the message for the next player and current image to draw 
     { 
      lblMsg.setText("Player1's Turn: mark X"); 
      drawCh='O'; 
     } 

     else if(drawCh=='O') // set the message for the next player and current image to draw 
     { 
      lblMsg.setText("Player2's Turn: mark O"); 
      drawCh='X'; 
     } 
     else  // set the message for the next player and current image to draw at start of the game 
     { 
     drawCh='X'; 
     lblMsg.setText("Player2's Turn: mark O"); 
     }  
     repaint(); 
     value=drawCh; 
    } 

} // end of mouseClicked 

} // end of MouseListener 

     protected void paintComponent(Graphics g) 
     { 
     try { 
      //super.paintComponent(g); 
      System.out.println("gameOver = "+gameOver); 
      System.out.println("Drawing"); 
      if (drawCh == 'X')  // draw X 
       { 
       fn = new File("D:\\Java Projects\\HW9\\images\\tictactoeX.png"); 
       bg = ImageIO.read(fn); 
       g.drawImage(bg, 0, 0, this); 
       //g.drawLine(10, 10, getWidth() - 10, getHeight() - 10); 
       // g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10); 
       } 
      else if (drawCh == 'O') // Draw O 
       { 
       fn = new File("D:\\Java Projects\\HW9\\images\\tic-tac-toe-O.png"); 
       bg = ImageIO.read(fn); 
       g.drawImage(bg, 0, 0, this); 
       //g.drawOval(10, 10, getWidth() - 20, getHeight() - 20); 
       } 

      gameOver=isGameOver(); 
     } // end of paint component 
     catch (IOException ex) { 
      Logger.getLogger(TicTacToe.class.getName()).log(Level.SEVERE, null, ex); 
     } 
} // end of paint component 



} // end of Cells 

} // end of tictactoe 

我寫了一個tic tac toe遊戲的代碼。現在我以某種方式編寫代碼,以便它可以像App和Applet一樣工作。 但問題是,它不顯示圖像,而我作爲小程序運行它。我沒有繪製圖像,而是嘗試了drawLine和drawOwal方法,它工作正常。Tic Tack Toe遊戲在applet中:圖像不能正常工作

但我的教授想要我必須使用圖像來運行它。請問任何人都可以給我任何解決方案。

在此先感謝。同時上傳代碼與這篇文章。並請讓我知道是否有人有解決辦法。我需要顯示分配的明天... :(

+0

@Mike注意[標籤:功課。標籤已經退役 –

+0

啊好的,謝謝:)。 – Mxyk

回答

0

小程序不能寫入或讀取本地文件。

+0

那我該如何顯示圖像? 我認爲這是您繪製圖像的方式。我也把圖片放在同一個文件夾中,我甚至用不同的方式嘗試過路徑,其中沒有一個能夠工作 – user1858245

+0

fn = new File(「D:\\ Java Projects \\ HW9 \\ images \\ tic-tac-toe-O .PNG「); bg = ImageIO.read(fn); g.drawImage(bg,0,0,this); – user1858245

+0

謝謝你,寶貴的時間。 – user1858245

0

小程序沙箱所以沒有權限訪問該applet代碼源自哪裏的外部資源。此外,不加載從paintComponent方法的圖像,而不是從applet的init方法,該方法被調用一次加載:

public void init() { 
    xImage = getImage(getDocumentBase(), "tictactoeX.png"); 
    oImage = getImage(getDocumentBase(), "tictactoeO.png"); 
} 
+0

謝謝,它工作,它顯示所有的圖像。但是現在遇到了另外一個問題。當我第一次開始遊戲時,它可以正常工作,但是當我重新開始遊戲時,applet屏幕會顯示全部'X'或全部'O'圖像。我認爲它在某些地方和開始緩衝emage,如果填充緩衝的圖像。 – user1858245

+0

當我點擊一個單元格時,我會建議使用他們自己的'X'或'O'圖片添加新的'JComponent'組件。這樣您可以將繪畫功能委託給這些組件。 – Reimeus