Newer
Older
Algolike / src / main / java / views / CardButton.java
package views;

import javax.swing.*;
import javax.swing.border.LineBorder;

import java.awt.*;

import static views.Constants.*;
import static views.Constants.CARD_HEIGHT;

public class CardButton extends JButton {
    private Status status;
    final int UNSELECTED_THICKNESS=1;
    final int SELECTED_THICKNESS=4;
    public CardButton(String text) {
        this.setText(text);
        this.setBorder(new LineBorder(UNSELECTED_COLOR, UNSELECTED_THICKNESS, true));
        this.setPreferredSize(new Dimension(CARD_WIDTH, CARD_HEIGHT));
        this.setFont(new Font("MS ゴシック", Font.BOLD, 14));
    }
    //選択状態の切り替え
    public void setEnabledSelection(boolean enabledSelection){
       if(enabledSelection){
           this.setBorder(new LineBorder(SELECTED_COLOR, SELECTED_THICKNESS,true));
       }else {
           this.setBorder(new LineBorder(UNSELECTED_COLOR, UNSELECTED_THICKNESS, true));
       }
    }
    public void setStatus(Status status){
        switch (status){
            case OPEN -> {
                this.setBackground(OPEN_COLOR);
            }
            case CLOSED -> {
                this.setBackground(CLOSED_COLOR);
            }
            case MY_CLOSED -> {
                this.setContentAreaFilled(false);
                this.setFocusPainted(false); // used for demonstration
            }
        }
    }
    @Override
    protected void paintComponent(Graphics g) {
        final Graphics2D g2 = (Graphics2D) g.create();
        g2.setPaint(new GradientPaint(
                new Point(0, 0),
                OPEN_COLOR,
                new Point(0, getHeight()),
                CLOSED_COLOR));
        g2.fillRect(0, 0, getWidth(), getHeight());
        g2.dispose();

        super.paintComponent(g);
    }
    public enum Status{
        OPEN,       //開示されている
        CLOSED,     //開示されていない
        MY_CLOSED   //開示されていない(ただし自分専用)
    }
}