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
273 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse compiler vs Installed JREs option 3 75
jar file executable 12 38
rhino JavaScript import, load 25 66
tomcat not starting 6 26
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now