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
282 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Title # Comments Views Activity
ejb stateless example 2 43
jsp insert to database example 2 64
Java: The Public Class Main 4 45
maven disable workspace resolution 1 41
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…

726 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