Solved

trying to draw an x on a tic tac toe board after a mouse press in the mousepressed(MouseEvent e) can't except graphics g so how else can i draw?

Posted on 2004-10-20
8
283 Views
Last Modified: 2010-03-31
I have created a tic tac toe board by creating 9 rectangles and then drawing lines between them. as folows.

============draws the squares=================

 g2.setColor(Color.black);

         int j=0;
         int k=50;
        for(int row=0; row < 3; row++)
        {
          for(int col=0; col < 3; col++)
          {

                    r[row][col] = new Rectangle(100 + j,k,130,147);
                    g2.fill(r[row][col]);
                    j=j+133;
              }

              k=k+150;
              j=0;
          }
===============================================

So once i have these squares created i know exactly where they where created so know i create a mouse press() which will read a press in a square and now what i want to do is the square i press in draw an X. However thje problem is when you are inside mousepress(MouseEvent e) you can't call graphics g. here is the code i have thus far. when i click a square it sends an 0 and x to system.out.println. i'd like to draw on. I thiough about calling a method from within the mouse pressed but the method will still have a graphics g in the parameter so when you call it in the mouse pressed () it be drawX(g); the g would give you an error.Please any ideas would be great.



========================




 */
   private class MouseStuff extends MouseAdapter
      {

         /**
            This method is called by the framework each time the mouse is
            pressed.
            @param e  a <code>MouseEvent</code> object generated by the framework.
            This object <code>e</code> contains all sort of information about the
            situation at the moment the mouse was pressed. We will need only the coordiates
            of the point where the mouse was pressed.
         */
         public void mousePressed(MouseEvent e)
         {
                     Point p = e.getPoint();
                 Rectangle rect;
                     int j=0;
                           int k=50;
                          for(int row=0; row < 3; row++)
                          {
                            for(int col=0; col < 3; col++)
                            {
                                if( r[row][col].contains(p))
                                {
                             if(table[row][col] == white)
                             {
                                                table[row][col] = drawn; // current box is now marked drawn so you can't draw here anymore
                                System.out.println("o");
                                // drawX(x,y);
                                     for(int rows=0; rows < 3; rows++)
                                                        {
                                                          for(int cols=0; cols < 3; cols++)
                                                          {

                                                 if(table[rows][cols] == white)
                                                 {
                                                 System.out.println("x");
                                                 return;
                                                                       }
                                                              }

                                         }

                                         }


                              j=j+133;
                                          return;

                                    }
                                }

                                k=k+150;
                                j=0;
                            }

         }
      }








===============================
0
Comment
Question by:tyweed420
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:aviadbd
ID: 12362635
Hey,

Well, first you can use "Graphics.DrawString()" method to draw an X.

Otherwise, just draw two lines using "Graphics.DrawLine()" methods.

I didnt understand the problem you had with the graphics?

AviadBD.
0
 

Author Comment

by:tyweed420
ID: 12362714
problem is not how to draw an o or x. it's that i need to do it somehow in the mouse pressed() method because thats where i know the square coordinates so i can use that to draw the x or the o in the right location. However i can't add Graphics g to the mousepress method .
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12362732
Hi tyweed420,

You have to keep track of the current state of the game in variables. Here you need and array of int (for example) where each element indicate what to draw : 0 (empty),  1 ( X ),  2 ( O ).

    int[] game=new int[3][3];

Then in your paint method, you can draw your component/window according to the current state of the game.
And this array is also usefull to test if there is a winner.
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 13

Expert Comment

by:Webstorm
ID: 12362748
Then, call the repaint() method in the mousePressed method in order to refresh your component/window
0
 
LVL 3

Expert Comment

by:aviadbd
ID: 12362769

Oh I get your problem now.

What you should do instead - You have a data structure of the board. In it, you keep whats in the square - If its empty, if its X or if its Y. Then, when there's a click, you mark it with X or O, accordingly, and then call "repaint()" of the component (or window).

Then, in the Container.paint(Graphics) method, you have Graphics and you can test the data structure to know what to paint, X or O.

AviadBD.
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 125 total points
ID: 12362789
aviadbd, please, don't repeat what i write
0
 
LVL 3

Expert Comment

by:aviadbd
ID: 12362817

Webstorm, I did not.

Just took me longer to write.

Webstorm, please dont presume you're the only one with the answer. ;-)

AviadBD.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12398453
:-)
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question