试图制作棋盘格

时间:2014-01-23 06:29:36

标签: java object graphics

我正在尝试制作一个棋盘,给出了我正在参加的CS课程的模板。但是,当我运行它时,屏幕上没有任何内容。我猜我错过了一些实际在屏幕上绘制方块的代码,但我已经尝试了很多东西但仍然没有。

   import java.applet.Applet;
   import java.awt.*;
   import java.util.Random;
   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import javax.swing.event.*;


   public class Checkers extends JApplet
   {
   private final int MAX_SIZE = 8; 
   private final int APP_WIDTH = 400;
   private final int APP_HEIGHT = 400;
   private final int MAXSIZE = 8;


   Square[][] sq;

   public void paint(Graphics page)
    {

    setBackground(Color.white);
    fillBoard(page); // draws the method that will draw the checkers
    setSize (APP_WIDTH,APP_HEIGHT);

    }

    public void fillBoard(Graphics page)
    {  
       sq = new Square[8][8];

       int x,y;
       Color rb;

       for (int row = 0; row < MAXSIZE; row++)
         for (int col = 0; col < MAXSIZE; col++)
         {
            x = row * (APP_WIDTH/MAXSIZE);
            y = col * (APP_HEIGHT/MAXSIZE);
            if ( (row % 2) == (col % 2) )
               rb = Color.red;
            else
               rb = Color.blue;
            sq[row][col] = new Square (x, y, rb);  
         }
   }

   class Square 
   {


    private int x, y = 0;  
    private Color c;
    private boolean occupied;
    private Color checkerColor;


    public Square (int x, int y, Color c)
    {
      this.x = x;
      this.y = y;
      this.c = c;
    }

    public void setX (int x)
    {
      x = this.x;
    }

    public int getX ()
    {
      return x;
    }

    public void setY (int y)
    {
      y= this.y;
    }

    public int getY ()
    {
      return y;
    }

    public void setColor (Color c)
    {
      c = this.c;
    }

    public Color getColor ()
    {
      return c;
    }

    public void setOccupy (boolean occupied)
    {
      occupied = this.occupied;
    }

    public boolean getOccupy ()
    {
      return occupied;
    }

    public void setCheckerColor (Color c)
    {
      checkerColor = this.checkerColor;
    }

    public Color getCheckerColor ()
    {
      return checkerColor;
    }

    public String toString()
    {
      return ("X coordinate: " + x + "\nY coordinate:" + y + "\nSquare color: " + c);
    }


   public void draw (Graphics page)
    {
         page.setColor(c);
         page.fillRect(x, y, 50, 50);
    }

2 个答案:

答案 0 :(得分:2)

您永远不会致电Square#draw

话虽如此,每次调用fillBoard方法时我都会警惕调用paint,事实上我不鼓励你首先覆盖paint。< / p>

我可能会做的是检查sq中的nullfillBoard,然后只生成数组。回到paint方法中,我只需使用复合循环并在每个方格中使用draw

不应覆盖paint的{​​{1}},而应该从JApplet开始,并覆盖JPanel方法,确保拨打paintComponent!< / p>

您应该这样做有很多原因,但这里的主要原因是super.paintComponent不是双缓冲的,这意味着当绘图更新时,您将获得“闪烁”。 JApplet默认为双缓冲,为您节省了大量的工作和时间来实现自己的解决方案......

完成此操作后,请使用自定义面板并将其添加到小程序中。

我会把所有的绘画逻辑都移到它身上。请查看Performing Custom Painting了解更多详情

答案 1 :(得分:0)

就我所见,除非我错过了一些你从未真正称之为重画或绘画或绘画方法的东西。这段代码的设置方式与我尝试完成类似任务的大多数其他代码的设置不同,我真的不想把它全部搞清楚,但你必须实际调用方法来绘制图像。但是,请确保密切关注您调用此方法的位置,因为正确放置它非常重要,否则可能无法正常运行。

相关问题