Draw things after painting drawing with paint (JPanel) (java)

I am trying to make a very simple Tic Tac Toe game in Java. However, I cannot figure out how to draw something after I have drawn the board. Here is my structure for my game.

public class ThreeInARowMain extends JPanel {

    public static final int WIDTH = 600, HEIGHT = 640;

    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.setColor(Color.black);
        g.drawString("1.", 0,20);
        g.drawString("2.", 210,20);
        g.drawString("3.", 410,20);
        g.drawString("4.", 0,220);
        g.drawString("5.", 210,220);
        g.drawString("6.", 410,220);
        g.drawString("7.", 0,420);
        g.drawString("8.", 210,420);
        g.drawString("9.", 410,420);
        //Horizontal Lines
        g.drawLine(0, 200, WIDTH, 200);
        g.drawLine(0, 400, WIDTH, 400);
        //Vertical Lines
        g.drawLine(200, 0, 200, HEIGHT);
        g.drawLine(400, 0, 400, HEIGHT);

    }

    public static void main (String [] args) {
    boolean firstorsecond = true;
        JFrame frame = new JFrame("Tic Tac Toe");
        frame.setSize(WIDTH, HEIGHT);
        frame.getContentPane().add(new ThreeInARowMain());
        frame.setLocationRelativeTo(null);
        frame.setBackground(Color.black);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        boolean someonewins = false;
        int firstplayerpos, secondplayerpos;
        int [] [] board = new int [3] [3];
        Scanner in = new Scanner(System.in);

        while (!someonewins){
        System.out.println("P1, enter the number of the square you'd like to mark.");
        firstplayerpos = in.nextInt();
        DrawXorO(firstplayerpos,firstorsecond);


        System.out.println("P2, enter the number of the square you'd like to mark.");
        secondplayerpos = in.nextInt();
        DrawXorO(secondplayerpos,!firstorsecond);

        }
    }

    public static void DrawXorO (int position, boolean firstorsecond) {

    }

}

      

I know this is a poorly designed game at the moment; I will fix this later. However, what I want with DrawXorO is to draw an X or or O at the position the player has entered. How do I add graphics after using the paint method? Thank!

+3


source to share


3 answers




+4


source


I would revisit your GUI to use 9 JButtons

, 1 for each square. Then you can call JButton methods eg. JButton.setText("X")

to make a square X display, which is much easier. If you really want to override paintComponent, you can create variables to store what each square displays, update those variables, and redirect calls () when you need an update.



+1


source


Do you mean something like this?

Tic Tac Toe GUI

Here's one way to draw a tic tac toe diagram to the left of the GUI. I have a model class that stores the board position in a 2D int array and a mouse controller class that lets me know where the person clicked on the tic tac toe chart.

package com.ggl.tictactoe.view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import com.ggl.tictactoe.controller.MouseMoveController;
import com.ggl.tictactoe.model.GameStatus;
import com.ggl.tictactoe.model.TicTacToeModel;

public class TicTacToePanel extends JPanel {

    private static final long serialVersionUID = -9150412950439480699L;

    private MouseMoveController controller;

    private TicTacToeModel model;

    public TicTacToePanel(TicTacToeFrame frame, TicTacToeModel model) {
        this.model = model;
        this.controller = new MouseMoveController(frame, model);
        this.addMouseListener(controller);
        this.setPreferredSize(new Dimension(360, 360));
    }

    public void setComputerMove() {
        if (!model.isPlayerGoesFirst()) {
            controller.setComputerMove();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, getWidth(), getHeight());

        int[][] board = model.getBoard();
        int width = getWidth() / board.length;
        int height = getHeight() / board[0].length;

        int x = 0;
        int y = 0;

        Stroke stroke = new BasicStroke(7F);
        g2d.setStroke(stroke);
        g2d.setColor(Color.BLACK);

        x += width;
        for (int i = 0; i < (board.length - 1); i++) {
            g2d.drawLine(x, y, x, getHeight());
            x += width;
        }

        x = 0;
        y += width;
        for (int i = 0; i < (board.length - 1); i++) {
            g2d.drawLine(x, y, getWidth(), y);
            y += width;
        }

        float fontSize = (float) height * 72F / 96F;
        Font largeFont = getFont().deriveFont(fontSize);

        for (int i = 0; i < board.length; i++) {
            x = width * i;
            for (int j = 0; j < board[i].length; j++) {
                y = height * j;
                Rectangle r = new Rectangle(x, y, width, height);
                if (board[i][j] == 1) {
                    g2d.setColor(Color.BLUE);
                    centerString(g2d, r, "X", largeFont);
                } else if (board[i][j] == -1) {
                    g2d.setColor(Color.CYAN);
                    centerString(g2d, r, "O", largeFont);
                }
            }
        }

        if (model.getGameStatus() == GameStatus.TIE_GAME) {
            BufferedImage image = GameOverImage.createImage(getWidth(),
                    getHeight(), "Tie Game");
            g2d.drawImage(image, 0, 0, this);
        } else if (model.getGameStatus() == GameStatus.COMPUTER_WINS) {
            BufferedImage image = GameOverImage.createImage(getWidth(),
                    getHeight(), "Computer Wins");
            g2d.drawImage(image, 0, 0, this);
        } else if (model.getGameStatus() == GameStatus.PLAYER_WINS) {
            BufferedImage image = GameOverImage.createImage(getWidth(),
                    getHeight(), "Player Wins");
            g2d.drawImage(image, 0, 0, this);
        }
    }

    /**
     * This method centers a <code>String</code> in a bounding
     * <code>Rectangle</code>.
     * 
     * @param g2d
     *            - The <code>Graphics2D</code> instance.
     * @param r
     *            - The bounding <code>Rectangle</code>.
     * @param s
     *            - The <code>String</code> to center in the bounding rectangle.
     * @param font
     *            - The display font of the <code>String</code>
     * 
     * @see java.awt.Graphics
     * @see java.awt.Rectangle
     * @see java.lang.String
     */
    private void centerString(Graphics2D g2d, Rectangle r, String s, Font font) {
        FontRenderContext frc = new FontRenderContext(null, true, true);

        Rectangle2D r2D = font.getStringBounds(s, frc);
        int rWidth = (int) Math.round(r2D.getWidth());
        int rHeight = (int) Math.round(r2D.getHeight());
        int rX = (int) Math.round(r2D.getX());
        int rY = (int) Math.round(r2D.getY());

        int a = (r.width / 2) - (rWidth / 2) - rX;
        int b = (r.height / 2) - (rHeight / 2) - rY;

        g2d.setFont(font);
        g2d.drawString(s, r.x + a, r.y + b);
    }

}

      

And here is the mouse controller code.

package com.ggl.tictactoe.controller;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import com.ggl.tictactoe.model.GameStatus;
import com.ggl.tictactoe.model.TicTacToeModel;
import com.ggl.tictactoe.view.TicTacToeFrame;

public class MouseMoveController extends MouseAdapter {

    private ComputerMoveRunnable controller;

    private TicTacToeFrame frame;

    private TicTacToeModel model;

    public MouseMoveController(TicTacToeFrame frame, TicTacToeModel model) {
        this.frame = frame;
        this.model = model;
        this.controller = new ComputerMoveRunnable(frame, model);
    }

    @Override
    public void mousePressed(MouseEvent event) {
        if ((model.getGameStatus() == GameStatus.ACTIVE_GAME)
                && (event.getButton() == MouseEvent.BUTTON1)) {
            int width = frame.getTicTacToePanel().getWidth();
            int height = frame.getTicTacToePanel().getHeight();
            int positionWidth = TicTacToeModel.getPositionWidth();

            Point p = event.getPoint();

            int x = getPosition(p.x, width, positionWidth);
            int y = getPosition(p.y, height, positionWidth);

            int[][] board = model.getBoard();
            if (board[x][y] == 0) {
                model.setPlayerMove(x, y);
                setComputerMove();
            }
        }
    }

    public void setComputerMove() {
        controller.run();
        frame.repaintTicTacToePanel();
    }

    private int getPosition(int location, int dimension, int squareWidth) {
        for (int i = 0; i < squareWidth; i++) {
            if (location < (dimension * (i + 1) / squareWidth)) {
                return i;
            }
        }

        return squareWidth - 1;
    }

}

      

+1


source







All Articles