Newer
Older
Algolike / src / main / java / views / CardButton.java
  1. package views;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.border.LineBorder;
  5.  
  6. import java.awt.*;
  7.  
  8. import static views.Constants.*;
  9. import static views.Constants.CARD_HEIGHT;
  10.  
  11. public class CardButton extends JButton {
  12. private Status status;
  13. final int UNSELECTED_THICKNESS=1;
  14. final int SELECTED_THICKNESS=4;
  15. public CardButton(String text) {
  16. this.setText(text);
  17. this.setBorder(new LineBorder(UNSELECTED_COLOR, UNSELECTED_THICKNESS, true));
  18. this.setPreferredSize(new Dimension(CARD_WIDTH, CARD_HEIGHT));
  19. this.setFont(new Font("MS ゴシック", Font.BOLD, 14));
  20. }
  21. //選択状態の切り替え
  22. public void setEnabledSelection(boolean enabledSelection){
  23. if(enabledSelection){
  24. this.setBorder(new LineBorder(SELECTED_COLOR, SELECTED_THICKNESS,true));
  25. }else {
  26. this.setBorder(new LineBorder(UNSELECTED_COLOR, UNSELECTED_THICKNESS, true));
  27. }
  28. }
  29. public void setStatus(Status status){
  30. switch (status){
  31. case OPEN -> {
  32. this.setBackground(OPEN_COLOR);
  33. this.setForeground(CLOSED_COLOR);
  34. }
  35. case CLOSED -> {
  36. this.setBackground(CLOSED_COLOR);
  37. this.setForeground(OPEN_COLOR);
  38. }
  39. case MY_CLOSED -> {
  40. this.setContentAreaFilled(false);
  41. this.setFocusPainted(false); // used for demonstration
  42. this.setForeground(OPEN_COLOR);
  43. }
  44. }
  45. }
  46. @Override
  47. protected void paintComponent(Graphics g) {
  48. final Graphics2D g2 = (Graphics2D) g.create();
  49. g2.setPaint(new GradientPaint(
  50. new Point(0, 0),
  51. OPEN_COLOR,
  52. new Point(0, getHeight()),
  53. CLOSED_COLOR));
  54. g2.fillRect(0, 0, getWidth(), getHeight());
  55. g2.dispose();
  56.  
  57. super.paintComponent(g);
  58. }
  59. public enum Status{
  60. OPEN, //開示されている
  61. CLOSED, //開示されていない
  62. MY_CLOSED //開示されていない(ただし自分専用)
  63. }
  64. }