Advertisement
loloof64

ChessWeb first errors corrections.

Nov 3rd, 2012
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. /*
  2.  * This file is part of ChessWeb.
  3.  
  4. ChessWeb is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. ChessWeb is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with ChessWeb.  If not, see <http://www.gnu.org/licenses/>
  16.  */
  17. package fr.loloof64.java_applet.chess_web.views;
  18.  
  19. import java.awt.Color;
  20. import java.awt.Container;
  21. import java.awt.Dimension;
  22. import java.awt.Graphics;
  23. import java.awt.LayoutManager;
  24. import java.awt.Panel;
  25.  
  26. public class BoardCanvas extends Panel {
  27.  
  28.     /**
  29.      * Simple constructor accepting a parent Container (java.awt)
  30.      * @param parent - {@link Container} - the parent Container.
  31.      */
  32.     public BoardCanvas(Container parent) {
  33.         adjustSize(new Dimension(DEFAULT_CELLS_SIZE_PX, DEFAULT_CELLS_SIZE_PX));
  34.     }
  35.  
  36.     /**
  37.      * Adjust size with the least value (between width and eight).
  38.      * @param parent - {@link Container} - the parent Container.
  39.      * @param layout - {@link LayoutManager} - the layoutmanager.
  40.      */
  41.     public BoardCanvas(Container parent, LayoutManager layout) {
  42.         super(layout);
  43.         Dimension requestedDimensions = layout.preferredLayoutSize(parent);
  44.         adjustSize(new Dimension(requestedDimensions.width, requestedDimensions.height));
  45.     }
  46.  
  47.     @Override
  48.     public void paint(Graphics graphics) {
  49.         for (int rankFromTopIndex = 0; rankFromTopIndex < 8; rankFromTopIndex++){
  50.             for (int fileFromLeftIndex = 0; fileFromLeftIndex < 8; fileFromLeftIndex++){
  51.                 Color cellColor = ((rankFromTopIndex+fileFromLeftIndex) % 2) == 0 ?
  52.                     WHITE_CELL_COLOR : BLACK_CELL_COLOR;
  53.                
  54.                 int xAbsCoordinate = currentCellsSizePx * fileFromLeftIndex;
  55.                 int yAbsCoordinate = currentCellsSizePx * rankFromTopIndex;
  56.                
  57.                 /*
  58.                  * Finally paints the current (loop) cell.
  59.                  */
  60.                 graphics.setColor(cellColor);
  61.                 graphics.fillRect(xAbsCoordinate, yAbsCoordinate, currentCellsSizePx, currentCellsSizePx);
  62.             }
  63.         }
  64.     }
  65.  
  66.     private void adjustSize(Dimension requestedDimensions){
  67.         int requestedWidth = requestedDimensions.width;
  68.         int requestedHeight = requestedDimensions.height;
  69.         currentCellsSizePx = requestedWidth > requestedHeight ? requestedWidth : requestedHeight;
  70.         setPreferredSize(new Dimension(currentCellsSizePx * 8, currentCellsSizePx * 8));
  71.     }
  72.    
  73.     /**
  74.      * Current cells size (in pixels).
  75.      */
  76.     private int currentCellsSizePx;
  77.    
  78.     /**
  79.      * Default cells size (in pixels).
  80.      */
  81.     private static final int DEFAULT_CELLS_SIZE_PX = 42;
  82.    
  83.     /**
  84.      * White cells colors.
  85.      */
  86.     private static final Color WHITE_CELL_COLOR = new Color(0xE9E441);
  87.    
  88.     /**
  89.      * Black cells colors.
  90.      */
  91.     private static final Color BLACK_CELL_COLOR = new Color(0xDF7401);
  92.    
  93.     /**
  94.      * Multi-threading security code.
  95.      */
  96.     private static final long serialVersionUID = 7321516321051309085L;
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement