27-04-2011, 01:28
|
|
|
חבר מתאריך: 02.12.09
הודעות: 19
|
|
עזרה JAVA
אני מנסה לבנות את המשחק איקס עיגול
הבעיה היא שאני רוצה להוסיף גרפיקה במקום הX או O. איך אני יכול להוסיף את זה ואיך אני יכול לדעת איזה היה לחיצת עכבר
תודה לעוזרים
קוד:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;
public class tic_tac_toe extends JApplet
{
String empty[]={"e", "e","e", "e","e", "e","e", "e","e"}, lineOut=" Hello"; //// e= empty, p1 or p2
JPanel panel[] =new JPanel[9];
JPanel inner = new JPanel();
private JLabel line= new JLabel (lineOut);
boolean TwoPlayer= true, startWithRed= true; /// start with red mean the user start to play with red else(false) yellow
////////////////////////////////////////
JMenuBar bar;
JMenu Game;
JMenu color;
JMenuItem p1Item;
JMenuItem p2Item;
JMenuItem redItem;
JMenuItem yellowItem;
/////////////////////////////////////////
public tic_tac_toe()
{
/////////////////////////////// menu
Game = new JMenu("Game");
color = new JMenu("Color");
p1Item = new JMenuItem("One player");
Game.add(p1Item);
redItem = new JMenuItem("play with red");
color.add(redItem);
yellowItem = new JMenuItem("play with yellow");
color.add(yellowItem);
p2Item = new JMenuItem("Two player");
Game.add(p2Item);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(Game);
bar.add(color);
color.setEnabled(false);
action(); ////// add the action listener to menu
////////////////////
//// end of menu
setLayout(new BorderLayout());
inner.setLayout(new GridLayout(3,3,10,10));
for (int l=0; l<9; l++)
{
panel[l]= new JPanel();
inner.add(panel[l]);
panel[l].setBackground(Color.black);
panel[l].addMouseListener(new MouseListener());
}
add(inner, BorderLayout.CENTER);
add(line,BorderLayout.SOUTH);
//////////////////////////////////////////////////////
}
private void action()
{
p1Item.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
TwoPlayer= false;
color.setEnabled(true);
System.out.println("p1 is pressed");
}
}
);
p2Item.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
TwoPlayer= true;
color.setEnabled(true);
System.out.println("p2 is pressed");
}
}
);
redItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
startWithRed= true;
System.out.println("red is pressed");
}
}
);
yellowItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
startWithRed= false;
System.out.println("yellow is pressed");
}
}
);
}
public class MouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
System.out.print("panel");
}
}
}
|