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
271 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 86
What is JNDI datasource in spring 1 38
groovy example issue 10 67
github account with ecipse 1 17
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

758 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

21 Experts available now in Live!

Get 1:1 Help Now