简体   繁体   中英

Trying to make checkerboard

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

Well as far as I could see unless I missed something you never actually called a repaint or paint or draw method. This code is set up differently than most other codes that I have seen trying to accomplish similar tasks and I don't really feel like figuring it all out but you have to actually call the method to draw the image. However, make sure that you pay close attention to where you call this method because it is rather important to place it correctly otherwise it may not serve its function correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM