Java:使用绘制主画布的类之外的Graphics绘图

时间:2013-12-17 22:34:55

标签: java swing graphics tic-tac-toe

我正在尝试使用Graphics开发Java中的Tic Tac Toe游戏。 我的问题是:我不想在Grid.class中添加任何其他方法(绘制线条3x3),但我想从名为Game的类中绘制我的X或O.我的网格类如下所示:

import java.awt.Graphics;
import javax.swing.JPanel;

public class Grid extends JPanel{

    private final int ITEM_WIDTH = 30;
    private final int ITEM_HEIGHT = 30;
    private final int OUTER_WIDTH = 90;
private final int OUTER_HEIGHT = 90;

public void paintComponent(Graphics g){
    super.paintComponent(g);
    drawOuter(g);
    drawGrid(g);
}

public void drawOuter(Graphics g){
    g.drawRect(0, 0, OUTER_WIDTH, OUTER_HEIGHT);
}

public void drawGrid(Graphics g){
    //Vertikális
    for(int i = ITEM_WIDTH; i < OUTER_WIDTH; i += ITEM_WIDTH){
        g.drawLine(i, 0, i, OUTER_HEIGHT);
    }
    //Horizontális
    for(int i = ITEM_HEIGHT; i < OUTER_HEIGHT; i += ITEM_HEIGHT){
        g.drawLine(0, i, OUTER_WIDTH, i);
    }
}

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

我的建议是尽量保持简单。

  • 创建包含X和O图像的ImageIcons
  • 将JLabel的GridLayout放在上面的JPanel中。
  • 当我想要显示X,O或空白时,交换JLabel的ImageIcon。

概念证明代码。单击JLabel几次将显示图标。该代码没有Tic-Tac-Toe逻辑:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.swing.*;

@SuppressWarnings("serial")
public class XorOorBlank extends JPanel {
   private static final int IMG_WIDTH = 100;
   private static final Color BACKGROUND = Color.LIGHT_GRAY;
   private static final Color X_COLOR = Color.red;
   private static final Color O_COLOR = Color.blue;
   private static final Stroke X_STROKE = new BasicStroke(8f);
   private static final int GAP = 8;
   private static final int SIDE = 3;
   private Icon blankIcon = createBlankIcon();
   private Icon xIcon = createXIcon();
   private Icon oIcon = createOIcon();

   public XorOorBlank() {
      setBackground(BACKGROUND);
      setLayout(new GridLayout(SIDE, SIDE, GAP, GAP));

      MouseListener mouseListener = new MouseAdapter() {
         @Override
         public void mousePressed(MouseEvent e) {
            JLabel label = (JLabel) e.getSource();
            Icon icon = label.getIcon();
            if (icon == blankIcon) {
               icon = xIcon;
            } else if (icon == xIcon) {
               icon = oIcon;
            } else if (icon == oIcon) {
               icon = blankIcon;
            }
            label.setIcon(icon);
         }
      };
      for (int i = 0; i < SIDE; i++) {
         for (int j = 0; j < SIDE; j++) {
            JLabel label = new JLabel(blankIcon);
            label.addMouseListener(mouseListener);
            add(label);
         }
      }
   }

   private Icon createBlankIcon() {
      BufferedImage img = new BufferedImage(IMG_WIDTH, IMG_WIDTH,
            BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = img.createGraphics();
      g2.setColor(Color.white);
      g2.fillRect(0, 0, IMG_WIDTH, IMG_WIDTH);
      g2.dispose();
      ImageIcon icon = new ImageIcon(img);
      return icon;
   }

   private Icon createXIcon() {
      BufferedImage img = new BufferedImage(IMG_WIDTH, IMG_WIDTH,
            BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = img.createGraphics();
      g2.setColor(Color.white);
      g2.fillRect(0, 0, IMG_WIDTH, IMG_WIDTH);
      g2.setColor(X_COLOR);
      g2.setStroke(X_STROKE);
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      int x1 = GAP;
      int y1 = x1;
      int x2 = IMG_WIDTH - GAP;
      int y2 = x2;
      g2.drawLine(x1, y1, x2, y2);
      g2.drawLine(x2, y1, x1, y2);
      g2.dispose();
      ImageIcon icon = new ImageIcon(img);
      return icon;
   }

   private Icon createOIcon() {
      BufferedImage img = new BufferedImage(IMG_WIDTH, IMG_WIDTH,
            BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = img.createGraphics();
      g2.setColor(Color.white);
      g2.fillRect(0, 0, IMG_WIDTH, IMG_WIDTH);
      g2.setColor(O_COLOR);
      g2.setStroke(X_STROKE);
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      int x1 = GAP;
      int y1 = x1;
      int x2 = IMG_WIDTH - 2 * GAP;
      int y2 = x2;
      g2.drawOval(x1, y1, x2, y2);
      g2.dispose();
      ImageIcon icon = new ImageIcon(img);
      return icon;
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("X or O");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new XorOorBlank());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

注意:不是要找到的paintComponent方法。

显示:

enter image description here

答案 1 :(得分:1)

你有几个选择......

你可以

Grid延伸,覆盖它的paintComponent方法并在其中绘制X / O(调用super.paintComponent

你可以

创建一个interface,它提供了一个简单的“绘制”方法,在实施时,它会绘制X / O。然后,您可以简单地将此新实例添加到Grid类,并通过X方法中的某种循环绘制O / paintComponent

你可以

使用GridBagLayout甚至OverlayLayout之类的内容,将另一个面板放在Grid类的顶部,或者使用BorderLayout将面板直接添加到Grid X类,然后负责绘制O / {{1}}

你可以

做鳗鱼充满鳗鱼的建议...