Question

Hello all I have this problem that I can't seem to fix. I've been given some code and have to make a "tic tac toe" game. Fairly primitive. At the moment what it want's me to do is take user input (it just asks for what row / column you want to place the marker) and it is meant to draw an oval on the appropriate square of the board. My current code is as following the work has specified that I make a new class to deal with user input.

I am currently just mucking around with trying to get it to add new items to the JFrame but am having little success. I have a dummy call for input, it doesn't check to see what I've typed it just calls an oval that SHOULD sit in the first square in the top left corner. I can get the object to draw onto the JFrame (albeit it takes up the whole frame) but it is always BEHIND the actual board (ie: if I stretch the frame I can see the circle). I've tried adding JPanels and so forth so that they sit on top of the board but so far I am having little luck.

Here is the code for creating the Oval which I was given for the task. All I am doing is instantiating a new oval with position (0,0,10,10). When it draws however it takes up the WHOLE JFrame but it is also BEHIND the actual board...... any ideas?

package lab4;

import javax.swing.*;
import java.awt.*;

/** Oval Supplier Class 
 * Author: David D. Riley
 * Date: April, 2004
 */
public class Oval extends JComponent  {

    /** post:   getX() == x  and  getY() == y
     *          and  getWidth() == w  and getHeight() == h
     *          and  getBackground() == Color.black
     */
    public Oval(int x, int y, int w, int h)  {
        super();
        setBounds(x, y, w, h);
        setBackground(Color.black);
    }

    /** post:   this method draws a filled Oval
     *          and  the upper left corner of the bounding rectangle is (getX(), getY()) 
     *          and  the oval's dimensions are getWidth() and getHeight()
     *          and  the oval's color is getBackground()
     */
    public void paint(Graphics g)  {
        g.setColor( getBackground() );
        g.fillOval(0, 0, getWidth()-1, getHeight()-1);
        paintChildren(g);
   }

}

EDIT: THIS IS NOW THE CODE WE ARE LOOKING AT--

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lab4;

/**
 *
 * @author Scott
 */
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;

public class GameBoard {

    private JFrame win;
    private int count = 1;

    //Create new GUI layout
    GridLayout layout = new GridLayout(3, 3);
    JPanel panel = new JPanel(layout);

    //Create a new Array of Rectangles
    Rectangle[][] rect = new Rectangle[3][3];

    public GameBoard() {


        //Create new JFrame + Set Up Default Behaviour
        win = new JFrame("Tic Tac Toe");

        win.setBounds(0, 0, 195, 215);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setResizable(true);

        //Loop goes through each line of the array. It creates a new rectangle
        //determines it's colour based on modulus division
        //Add's the rectangle to the JPanel.
        for (int i = 0; i < rect.length; i++) {
            for (int j = 0; j < rect[i].length; j++) {
                rect[i][j] = new Rectangle(0, 0, 1, 1);
                if (count % 2 != 0) {
                    rect[i][j].setBackground(Color.black);
                } else {
                    rect[i][j].setBackground(Color.red);
                }
                panel.add(rect[i][j]);
                count++;
            }
        }


        //Sets the game to be visible.
        win.add(panel);
        win.setVisible(true);
        //userInput();
    }

    private void userInput() {
    Scanner scan = new Scanner(System.in);
    }
}
Was it helpful?

Solution 2

Answer ended up being that I need to stick with the flow layout, manually size the rectangles and use a JLayeredPane instead. Was not aware this existed, talked to my lecturer who said that my thought process was right and that was the way he intended for me to do it......what a pain but all done thanks to those that helped.

OTHER TIPS

Let's start with your oval class...

This is a VERY bad idea...

public void paint(Graphics g)  {
    // No super.paint(g) call, hello to a world of pain...
    // Paint some stuff
    g.setColor( getBackground() );
    g.fillOval(0, 0, getWidth()-1, getHeight()-1);
    // Then draw the children over it...???
    paintChildren(g);
}

This is a better approach.

protected void paintComponent(Graphics g)  {
    super.paintComponent(g);
    g.setColor( getBackground() ); // Might want to pick a better color...
    g.fillOval(0, 0, getWidth()-1, getHeight()-1);

}

At a guess, I suggest that your window is using a layout manager that is overriding your setBounds call

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top