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
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
279 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

790 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