JAVA   35
value
Guest on 13th March 2023 01:15:51 PM


  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class Value extends JPanel
  5. {
  6.    //Keeps track of the background (highlight) color.
  7.    private int col = 0;
  8.  
  9.    //The numeric value of the cell
  10.    private int val = 0;
  11.  
  12.    private JLabel lab;
  13.  
  14.    //Colors for highlight
  15.    public static final int WHITE = 0;
  16.    public static final int YELLOW = 1;
  17.    public static final int PINK = 2;
  18.    public static final int BLUE = 3;
  19.    public static final int GREEN = 4;
  20.    public static final int GRAY = 5;
  21.    public static final int ORANGE = 6;
  22.    public static final int MAGENTA = 7;
  23.    public static final int RED = 8;
  24.    public static final int DARKGRAY = 9;
  25.  
  26.  
  27.    public Value(int value, int color)
  28.    {
  29.       val = value;
  30.       col = color;
  31.       lab = new JLabel(String.valueOf(val), SwingConstants.CENTER);
  32.       this.setBorder(BorderFactory.createEtchedBorder());
  33.       paintMe();
  34.    }
  35.  
  36.    public Value(int value)
  37.    {
  38.       val = value;
  39.       col = 0;
  40.       this.setAlignmentX(CENTER_ALIGNMENT);
  41.       this.setAlignmentY(CENTER_ALIGNMENT);
  42.       lab = new JLabel(String.valueOf(val), SwingConstants.CENTER);
  43.       this.setBorder(BorderFactory.createEtchedBorder());
  44.       this.add(lab);
  45.       paintMe();
  46.    }
  47.  
  48.    public int getValue()
  49.    {
  50.       return val;
  51.    }
  52.  
  53.    public int getColor()
  54.    {
  55.       return col;
  56.    }
  57.  
  58.    public void setValue(int newVal)
  59.    {
  60.       val = newVal;
  61.       paintMe();
  62.    }
  63.  
  64.    public void setColor(int newCol)
  65.    {
  66.       col = newCol;
  67.       paintMe();
  68.    }
  69.  
  70.    public void setAll(int newVal, int newCol)
  71.    {
  72.       val = newVal;
  73.       col = newCol;
  74.       paintMe();
  75.    }
  76.  
  77.    public void paintMe()
  78.    {
  79.       this.setBackground(myColor());
  80.       lab.setText(String.valueOf(val));
  81.       this.repaint();
  82.    }
  83.  
  84.    public Color myColor()
  85.    {
  86.       if(col==WHITE)
  87.          return Color.white;
  88.       else if(col==YELLOW)
  89.          return Color.yellow;
  90.       else if(col==PINK)
  91.          return Color.pink;
  92.       else if(col==BLUE)
  93.          return Color.cyan;
  94.       else if(col==GREEN)
  95.          return Color.green;
  96.       else if(col==GRAY)
  97.          return Color.lightGray;
  98.       else if(col==ORANGE)
  99.          return Color.orange;
  100.       else if(col==MAGENTA)
  101.          return Color.magenta;
  102.       else if(col==RED)
  103.          return Color.red;
  104.       else if(col==DARKGRAY)
  105.          return Color.gray;
  106.       else
  107.          return Color.white;
  108.    }
  109. }

Raw Paste

Login or Register to edit or fork this paste. It's free.