Java question

iSkylla

Supreme [H]ardness
Joined
Jul 22, 2005
Messages
4,240
Hey guys, I have this problem where we have to draw a couple tic tac toe boards and the xs and os. Anyways, I'm drawing the borders to the rectangles and I can't seem to figure out how to do it. Obviously, my method doesn't work, but I was wondering if any of you can help. I would appreciate it, thanks. Here is part of my main method and my draw method.

Code:
    public static void main(String[] args) 
    {
        DrawingPanel panel = new DrawingPanel (400, 325);  //Creates graphics panel
        Graphics g = panel.getGraphics();
        Color ltBlue = new Color (180, 180, 255);  //Creates custom color, Light Blue
        panel.setBackground(ltBlue);  // Sets background color
        
        //Method call drawBoard
        
        drawBoard(g, 60, 3, 10, 10);
        drawBoard(g, 24, 5, 50, 200);
        drawBoard(g, 48, 4, 200, 75);
    }

    public static void drawBoard(Graphics g, int sizeBoard, int dimBoard, int x, int y)
    {
        //Nested loop for drawing each line of boxes
        //The first loop controls the amount of lines drawn
        for (int drawTimes = 0; drawTimes < dimBoard; drawTimes++)
        {
            for (int i = 0; i < dimBoard; i++)
            {
                g.setColor(Color.BLACK);
                g.drawLine(x, y, sizeBoard, sizeBoard); 
                g.drawRect(x, y, sizeBoard, sizeBoard);
                g.setColor(Color.YELLOW);
                g.fillRect(x, y, sizeBoard, sizeBoard);
                x = x + sizeBoard;
            }
            
            y = y + sizeBoard;
            x = x - (dimBoard * sizeBoard);
        }
    }

The part that is giving me trouble is the g.drawLine. I'm just not sure what to use for its parameters. For reference, here is what the end result should look like. Another note, we are using a "Drawing Panel" to avoid some of the complexities of the built in java class until we learn more.

full_window.png


Thanks a lot!
 
Well, I haven't parsed through exactly what you are doing in the nested for loops, but i don't think you want to be drawing the lines in the nested for loops. (to do a full border for each square, you'd be drawing 4 lines each time through the inner loop, lots of extra work).

You also don't want to be modifying the variables you pass in (x,y).. i would create local copies of these to modify.

The first thing i would do is draw the background (yellow).
the coordinates for this should be upper left = x,y and x+width & y+height = lower right.This you only do once.

Next you draw the lines, horizontal & vertical. I would do these in a for loop, 1 vert & 1 horiz line at a time. Count how many of each you need, its not the same as dimBoard. each time you loop through you would increment one of your coordinates by the height/width divided by how many spaces in your grid.

The x's and o's you may want to do in nested for loops. It would be a good idea to put this loop inside your loop that draws the lines. Your coordinate calculations from the outer loop would be helpful here.

If things have to be in precise regions, you may have to nudge lines by a pixel, etc.
 
I figured it out myself, it was just something simple. Thanks though.
 
Back
Top