Advertisement
Vita_Harvey

Cell_C

Mar 3rd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package csc143.sudoku;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7.  
  8. /**
  9.  * @author Vita Wiebe
  10.  * @version PA8
  11.  * A helper class to make a cell inside a SudokuBoard object.
  12.  */
  13. class Cell extends JPanel {
  14.  
  15.     int row;
  16.     int column;
  17.     private boolean clicked;
  18.     private boolean hoveredOver;
  19.          
  20.      /** Our class constructor.
  21.       * Determines size and shape of Cell object, which has
  22.       * been standardized for the purposes of this assignment.
  23.       * @param int row, used to keep board position.
  24.       * @param int column, used to keep board position.
  25.       */
  26.      public Cell(int row, int column) {        
  27.          this.row = row;
  28.          this.column = column;
  29.          setPreferredSize(new Dimension(50, 50));    
  30.          add(new JLabel(row + ", " + column));
  31.          setBackground(Color.YELLOW);
  32.          clicked = false;
  33.          hoveredOver = false;            
  34.          setOpaque(false);
  35.          setEnabled(true);        
  36.      }
  37.      
  38.      public boolean isSelected() {      
  39.          return clicked;
  40.      }
  41.      
  42.      public boolean isHoveredOver() {
  43.          return hoveredOver;
  44.      }
  45.          
  46.      public void select() {
  47.    
  48.         setBackground(Color.YELLOW);
  49.         clicked = true;
  50.         setOpaque(true);              
  51.      }
  52.      
  53.      public void deSelect() {
  54.         clicked = false;
  55.         if(hoveredOver) {
  56.             setOpaque(true);
  57.             setBackground(Color.CYAN);
  58.         } else {
  59.             setOpaque(false);
  60.         }
  61.      }
  62.      
  63.      public void hoveredOver() {
  64.          hoveredOver = true;
  65.          if(!clicked) {
  66.             setBackground(Color.CYAN);
  67.          }
  68.          setOpaque(true);
  69.      }
  70.      
  71.      public void notHoveredOver() {
  72.          hoveredOver = false;
  73.          if(!clicked) {            
  74.             setOpaque(false);
  75.          }
  76.      }
  77.      
  78.     /**
  79.      * This method renders one 50x50 "cell" to add to cell array "cells".
  80.      * @param g The Graphics object use to render
  81.      */
  82.     @Override
  83.     public void paintComponent(java.awt.Graphics g) {
  84.        
  85.         // paint the underlying component
  86.         super.paintComponent(g);
  87.                              
  88.         // set the color of the outline        
  89.         g.setColor(Color.BLACK);
  90.                
  91.         g.drawRect(0, 0, 47, 47);
  92.                        
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement