【问题标题】:Draw things after board is drawn with paint (JPanel) (java)用油漆(JPanel)(java)绘制板后绘制东西
【发布时间】:2015-06-14 10:47:55
【问题描述】:

我正在尝试用 Java 制作一个非常简单的井字游戏。但是,在我画完电路板后,我无法弄清楚如何画一些东西。这是我的游戏结构。

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) {

    }

}

我知道目前这是一款设计糟糕的游戏;我稍后会更正。但是,我想要 DrawXorO 是在玩家输入的位置绘制一个 X 或和 O。使用方法 paint 后如何添加图形?谢谢!

【问题讨论】:

标签: java jframe jpanel paint


【解决方案1】:

【讨论】:

  • 但是我不能将 Scanner 与 UI 混合使用?所以以我想要的方式构建游戏根本行不通?我想做的是让用户在控制台中输入他们想要标记的位置。
  • 您可以使用按钮、鼠标点击、文本字段或类似的东西
【解决方案2】:

你的意思是这样的吗?

这是在 GUI 左侧绘制井字游戏图的一种方法。我有一个模型类,它将棋盘位置保持在二维 int 数组中,还有一个鼠标控制器类,它让我知道人们在井字游戏图上单击的位置。

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);
    }

}

这是鼠标控制器代码。

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;
    }

}

【讨论】:

  • 谢谢。比我的水平高一点。但是,非常有用且值得一看。一个问题;是导入 com.ggl.tictactoe.model.GameStatus;进口com.ggl.tictactoe.model.TicTacToeModel;进口com.ggl.tictactoe.view.TicTacToeFrame;
  • 上面写的是外部包吗?
  • @Filip:我使用标准 Swing 类编写了井字游戏代码。我发布了我为游戏编写的 9 个类中的 2 个。 com.ggl 导入是我自己的 Java 包。我将模型代码、视图代码和控制器代码保存在单独的包中。
  • 我刚刚在Github上发布了所有Java代码。
【解决方案3】:

我会重新设计您的 GUI 以使用 9 个JButtons,每个正方形 1 个。然后您可以调用 JButton 方法,例如JButton.setText("X") 制作一个方形显示 X 会更简单。如果你真的想重写paintComponent,你可以创建变量来存储每个方块当前显示的内容,更新这些变量并在需要更新时调用repaint()。

【讨论】:

  • 谢谢,我确实做到了,并且完全用 JButtons 制作了游戏。这不是最好看的游戏,但是,它确实有效。谢谢你的提示,给了我很好的训练。
  • 没问题,实际上有很多 JButton 方法,例如设置背景图像/颜色等,您可以使用这些方法来使您的 gui 看起来更好。
猜你喜欢
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
相关资源
最近更新 更多