Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-18-2008, 06:46 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
can you help me with mouse pressed method please?
hello I'm trying to make this game but the player label it doesn't appear in the applet and i cant draw string in the location like X or O can you take a look at my code please it is in Board class.
Thanks

Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; class Player{ private int xCoordinate; private int yCoordinate; private String playerText; public Player(int x, int y, String t) { playerText = t; xCoordinate = x; yCoordinate = y; } public void draw(Graphics g) { g.drawString(playerText, xCoordinate, yCoordinate); } public void SetText(String t) { playerText = t; } }//end player class Square { private int x; private int y; private int occupied; public Square(int x1, int y1) { x = x1; y= y1; occupied = 0; } public void setX(int x1) { x = x1; } public int getX() { return x; } public void setY(int y1) { y = y1; } public int getY() { return y; } public void setOccupied(int o) { occupied = o; } public int getOccupied() { return occupied; } } public class TicTac extends JApplet implements ActionListener { private JPanel panel; private JButton newgame; private Board B; public void init() { B = new Board(); add( B, BorderLayout.CENTER ); panel = new JPanel(); newgame = new JButton( "New Game" ); panel.add( newgame); add( panel, BorderLayout.SOUTH ); newgame.addActionListener(this); } public void actionPerformed( ActionEvent ev) { Object source = ev.getSource(); if (source == newgame) {remove(B); repaint(); B = new Board(); add( B, BorderLayout.CENTER ); B.setTurn(1); repaint(); validate(); } } }//end class TicTac class Board extends JPanel implements MouseListener, MouseMotionListener { private JLabel playerlabel; private boolean play; private boolean start; private int turn; private Square squares[][]; private Player myplayer[][]; public Board() { setBackground( Color.WHITE ); playerlabel = new JLabel( "X player first" ); //playerlabel.setLayout(null); // playerlabel.setLocation(500,500); add( playerlabel ); play = true; turn = 1; squares = new Square[3][3]; myplayer = new Player[3][3]; int x = 40; int y = 40; for (int i=0; i<3; i++){ x = 40; for (int j=0; j<3; j++){ Square s = new Square( x,y ); squares[i][j] = s; x +=50; } y +=50; } this.addMouseListener(this); this.addMouseMotionListener(this); } public void mouseClicked( MouseEvent event ){} public void mouseMoved( MouseEvent event ){} public void mouseEntered( MouseEvent event ){} public void mouseExited( MouseEvent event ){} public void mousePressed( MouseEvent event ) {int xPos = event.getX(); int yPos = event.getY(); if(play && turn ==1){ for (int i=0; i<3; i++){ for (int j=0; j<3; j++){ int x = squares[i][j].getX(); int y = squares[i][j].getY(); if(xPos> x && xPos<x+40&& yPos> y && yPos<y+40){ if( squares[i][j].getOccupied() ==0 ){ String ptext; ptext = "X"; Player p = new Player(x, y,ptext); myplayer[i][j] = p; squares[i][j].setOccupied(1); playerlabel.setText( "O Player Turn" ); turn = 2; repaint(); Win(); } else{ turn = 2; playerlabel.setText( "O Player Turn" ); repaint(); } } } } } if ( play && turn ==2 ) { for (int i=0; i<3; i++){ for (int j=0; j<3; j++){ int x = squares[i][j].getX(); int y = squares[i][j].getY(); if(xPos> x && xPos<x+40&& yPos> y && yPos<y+40){ if( squares[i][j].getOccupied() ==0 ){ String ptext; ptext = "O"; Player p = new Player(x, y,ptext); myplayer[i][j] = p; squares[i][j].setOccupied(2); playerlabel.setText( "X Player Turn" ); turn = 1; repaint(); Win(); } else{ turn = 1; playerlabel.setText( "X Player Turn" ); repaint(); } } } } } } public void mouseReleased( MouseEvent event ) { } public void mouseDragged( MouseEvent event ) { } public void Win(){ if(squares[0][0].getOccupied() == squares[0][1].getOccupied() &&squares[0][1].getOccupied()==squares[0][2].getOccupied() &&squares[0][2].getOccupied() == 1) {playerlabel.setText("Player 1 X WON THE GAME!"); play =false; } else if (squares[1][0].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[1][2].getOccupied() &&squares[1][2].getOccupied() == 1) {playerlabel.setText("Player 1 X WON THE GAME!"); play =false; } else if(squares[2][0].getOccupied() == squares[2][1].getOccupied() &&squares[2][1].getOccupied()==squares[2][2].getOccupied() &&squares[2][2].getOccupied() == 1) {playerlabel.setText("Player 1 X WON THE GAME!"); play =false; } if(squares[0][0].getOccupied() == squares[0][1].getOccupied() &&squares[0][1].getOccupied()==squares[0][2].getOccupied() &&squares[0][2].getOccupied() == 2) {playerlabel.setText("Player 2 O WON THE GAME!"); play =false; } else if (squares[1][0].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[1][2].getOccupied() &&squares[1][2].getOccupied() == 2) {playerlabel.setText("Player 2 O WON THE GAME!"); play =false; } else if(squares[2][0].getOccupied() == squares[2][1].getOccupied() &&squares[2][1].getOccupied()==squares[2][2].getOccupied() &&squares[2][2].getOccupied() == 2) {playerlabel.setText("Player 2 O WON THE GAME!"); play =false; } //Vertically win if (squares[0][0].getOccupied() == squares[1][0].getOccupied() &&squares[1][0].getOccupied()==squares[2][0].getOccupied() &&squares[2][0].getOccupied() == 1) {playerlabel.setText("Player 1 X WON THE GAME!"); play =false; } else if (squares[0][1].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[2][1].getOccupied() &&squares[2][1].getOccupied() == 1) {playerlabel.setText("Player 1 X WON THE GAME!"); play =false; } else if(squares[0][2].getOccupied() == squares[1][2].getOccupied() &&squares[1][2].getOccupied()==squares[2][2].getOccupied() &&squares[2][2].getOccupied() == 1) {playerlabel.setText("Player 1 X WON THE GAME!"); play =false; } if (squares[0][0].getOccupied() == squares[1][0].getOccupied() &&squares[1][0].getOccupied()==squares[2][0].getOccupied() &&squares[2][0].getOccupied() == 2) {playerlabel.setText("Player 2 O WON THE GAME!"); play =false; } else if (squares[0][1].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[2][1].getOccupied() &&squares[2][1].getOccupied() == 2) {playerlabel.setText("Player 2 O WON THE GAME!"); play =false; } else if(squares[0][2].getOccupied() == squares[1][2].getOccupied() &&squares[1][2].getOccupied()==squares[2][2].getOccupied() &&squares[2][2].getOccupied() == 2) {playerlabel.setText("Player 2 O WON THE GAME!"); play =false; } //Diagonal win if (squares[0][0].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[2][2].getOccupied() &&squares[2][2].getOccupied() == 1) {playerlabel.setText("Player 1 X WON THE GAME!"); play =false; } else if (squares[0][2].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[2][0].getOccupied() &&squares[2][0].getOccupied() == 1) {playerlabel.setText("Player 1 X WON THE GAME!"); play =false; } if (squares[0][0].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[2][2].getOccupied() &&squares[2][2].getOccupied() == 2) {playerlabel.setText("Player 2 O WON THE GAME!"); play =false; } else if (squares[0][2].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[2][0].getOccupied() &&squares[2][0].getOccupied() == 2) {playerlabel.setText("Player 2 O WON THE GAME!"); play =false; } }//end win public void setTurn(int t) { turn = t; playerlabel.setText( "X Player Plays First" ); } public void paintComponent( Graphics g ) { super.paintComponent( g ); this.setBackground( Color.WHITE ); for (int i=0; i<3; i++){ for (int j=0; j<3; j++){ g.setColor( Color.BLACK ); g.drawRect( squares[i][j].getX(), squares[i][j].getY(), 50, 50 ); } } for (int i=0; i<3; i++){ for (int j=0; j<3; j++){ myplayer[i][j].draw(g); } } } //End of paintComponent }

Last edited by java_fun2007 : 05-18-2008 at 06:54 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-19-2008, 02:31 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
C:\jexp>javac TicTacApplet.java C:\jexp>appletviewer TicTacApplet.java Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at Board.paintComponent(TicTacApplet.java:330) ...
You must instantiate the elements of the myplayer array in the Board class constructor:
Code:
Square s = new Square( x,y ); squares[i][j] = s; myplayer[i][j] = new Player(x, y, String.valueOf(i*3+j+1)); x +=50;
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-20-2008, 09:34 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
Thanks Hardwired for your help, I don't know why my compiler doesn't detect this error also I can't see the applet when I use appletviewer or the html page to show the applet does it work for you?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-21-2008, 12:31 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
I don't know why my compiler doesn't detect this error
There may be a difference in the code you're compiling and what you posted here.
I can't see the applet when I use appletviewer or the html page to show the applet does it work for you?
Yes. Make sure you have an applet tag in a comment in your source file:
Code:
// <applet code="TicTacApplet" width="400" height="400"></applet>
Using an html file to launch an applet is difficult during development. Browsers and the java plug–in both cache applet class files and run the cached version instead of an/any updated versions. To avoid this you have to change the class name every time you compile it. Or you can use appletviewer for your applet developmment. If this is the trouble you're having you will need to change the class name to get started with appletviewer development.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-23-2008, 12:23 AM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
Thanks a lot hardwired now the problem has been solved I can't imagine how does a java program get effected by a comment like this APPLET TAG, it worked immediately after I wrote the comment.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to display a sum of all previously pressed numbers in JTextField? all eyes New To Java 2 03-30-2008 10:38 PM
when muse pressed the background change pcman Java Applets 1 03-18-2008 01:51 AM
key pressed event kavithas New To Java 7 12-10-2007 04:01 PM
mouse over on JButton gradon Java Applets 1 08-04-2007 07:50 AM
Use the mouse position susan Java Applets 1 07-29-2007 01:10 AM


All times are GMT +3. The time now is 06:09 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org