/**
*
* @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
我已经为井字游戏编写了代码。现在我以某种方式编写代码,使其既可以用作应用程序,也可以用作小程序。但问题是当我将它作为小程序运行时它不显示图像。我尝试了 drawLine 和 drawOwal 方法而不是绘制图像,并且效果很好。
但是我的教授希望我必须使用图像来运行它。任何人都可以给我任何解决方案吗?
提前致谢。还用这篇文章上传代码。如果有人有解决方案,请告诉我。我明天需要展示作业... :(