/**
*
* @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 メソッドを試してみましたが、うまくいきました。
しかし、私の教授は、イメージを使用して実行する必要があることを望んでいます。これに対する解決策を教えてください。
前もって感謝します。また、この投稿でコードをアップロードします。誰かが解決策を持っているかどうか教えてください。明日課題を見せないといけない... :(