试图制作棋盘格 [英] Trying to make checkerboard

查看:169
本文介绍了试图制作棋盘格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我正在使用的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;


平方[] [] sq;

public void paint(Graphics page)
{

setBackground(Color.white);
fillBoard(page); //绘制绘制棋子的方法
setSize(APP_WIDTH,APP_HEIGHT);


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

int x,y;
颜色rb;
$ b $ for(int row = 0; row< MAXSIZE; row ++)
for(int col = 0; col< MAXSIZE; col ++)
{
x = row *(APP_WIDTH / MAXSIZE);
y = col *(APP_HEIGHT / MAXSIZE);如果((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;
私有颜色c;
私有布尔占用;
私有颜色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(布尔占用)
{
占用= this.occupied;
}

public boolean getOc​​cupy()
{
return occupied;
}

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

public Color getCheckerColor()
{
return checkerColor;


public String toString()
{
return(X coordinate:+ x +\\\
Y coordinate:+ y +\ nSquare颜色:+ c);
}


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


解决方案

$ c $> Square#draw 。



话虽如此,我会对 fillBoard 每次调用 paint 方法时,实际上我会阻止你在第一个中重写 paint 我可以做的是检查 sq 是否是 null 。

code> in fillBoard ,然后只生成数组。回到paint方法中,我会简单地使用一个复合循环,并在每个方格中使用 draw



而不是覆盖 paint > JApplet ,你应该从 JPanel 重写它的 paintComponent 方法,确保你调用 super.paintComponent



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



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



我会将所有绘画逻辑移动到它。查看执行自定义绘画以获取更多详细信息


I am trying to make a checkerboard given a template from a CS class I am taking. However, when I run it, nothing comes up on the screen. I am guessing I am missing some code to actually draw the squares onto the screen but I have tried a lot of things and still nothing.

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

解决方案

You never call Square#draw.

Having said that, I would be wary about calling fillBoard every time the paint method is called, in fact I'd discourage you from overriding paint in the first place.

What I might do is check to see if sq is null in fillBoard and only generate the array then. Back in the paint method, I would simply use a compound loop and draw each square.

Instead of overriding paint of JApplet, you should start with something like JPanel and override it's paintComponent method, make sure you call super.paintComponent!

There are a number of reasons you should do this, but the main one here is JApplet is not double buffered, meaning you will get "flashes" as the drawing is updated. JPanel is double buffered by default, saving you a lot of work and time having to implement your own solution...

Once you've done this, take custom panel and add it to the applet.

I would move all the painting logic to it. Take a look at Performing Custom Painting for more details

这篇关于试图制作棋盘格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆