Comment detail

年間カレンダー (Nested Flatten)

Java/Swingで組んでみました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

class MeApp {
    public static void main(String[] args) {
        final int year;
        if (args.length == 0) {
            year = Calendar.getInstance().get(Calendar.YEAR);
        } else {
            year = Integer.valueOf(args[0]);
        }
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                final MeFrame frame = new MeFrame(year);
                frame.setDefaultCloseOperation(MeFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

class MeFrame extends JFrame {
    public MeFrame(int year) {
        super(String.format("%d年 年間カレンダー", year));
        getContentPane().setLayout(new GridLayout(3, 4, 5, 5));
        for (int i = 0; i < 12; ++i) {
            getContentPane().add(new MeMonthPanel(year, i + 1));
        }
    }
}

class MeMonthPanel extends JPanel {
    public MeMonthPanel(int year, int month) {
        setLayout(new BorderLayout());
        add(new JLabel(String.format("%d月", month), JLabel.CENTER), BorderLayout.NORTH);
        add(new MeMonthCanvas(year, month), BorderLayout.CENTER);
    }
}

class MeMonthCanvas extends JComponent {
    private final String[][] _cells = new String[7][7];
    private final Color[][] _colors = new Color[7][7];
    public MeMonthCanvas(int year, int month) {
        final String[] weekdays = new DateFormatSymbols().getShortWeekdays();
        for (int col = 0; col < 7; ++col) {
            _cells[0][col] = weekdays[col + 1];
            _colors[0][col] = new Color(231, 255, 231);
        }
        _colors[0][0] = new Color(247, 202, 202); // Sunday
        _colors[0][6] = new Color(206, 255, 255); // Saturday

        final Calendar cal = Calendar.getInstance();
        for (int row = 1; row < 7; ++row) {
            for (int col = 0; col < 7; ++col) {
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, month - 1);     // starts from 0
                cal.set(Calendar.DAY_OF_WEEK, col + 1); // starts from 1
                cal.set(Calendar.WEEK_OF_MONTH, row);   // starts from 1
                if (cal.get(Calendar.DAY_OF_WEEK) == col + 1 && cal.get(Calendar.WEEK_OF_MONTH) == row) {
                    _cells[row][col] = String.valueOf(cal.get(Calendar.DATE));
                } else {
                    _cells[row][col] = "";
                }
                _colors[row][col] = Color.WHITE;
            }
        }
    }
    public Dimension getPreferredSize() {
        return new Dimension(25 * 7, 25 * 7);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        final FontMetrics fm = g.getFontMetrics();
        final int w = getWidth() / 7;
        final int h = getHeight() / 7;
        for (int row = 0; row < 7; ++row) {
            for (int col = 0; col < 7; ++col) {
                g.setColor(_colors[row][col]);
                g.fill3DRect(w * col, h * row, w, h, true);
                final String s = _cells[row][col];
                g.setColor(Color.BLACK);
                g.drawString(
                    s,
                    w * col + (w - fm.stringWidth(s)) / 2,
                    h * row + (h - fm.getHeight()) / 2 + fm.getAscent()
                );
            }
        }
    }
}

ついでにJTable版。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

class MeApp {
    public static void main(String[] args) {
        final int year;
        if (args.length == 0) {
            year = Calendar.getInstance().get(Calendar.YEAR);
        } else {
            year = Integer.valueOf(args[0]);
        }
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                final MeFrame frame = new MeFrame(year);
                frame.setDefaultCloseOperation(MeFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

class MeFrame extends JFrame {
    public MeFrame(int year) {
        super(String.valueOf(year));
        getContentPane().setLayout(new GridLayout(3, 4, 5, 5));
        for (int i = 0; i < 12; ++i) {
            getContentPane().add(new MeMonthPanel(year, i + 1));
        }
    }
}

class MeMonthPanel extends JPanel {
    public MeMonthPanel(int year, int month) {
        setLayout(new BorderLayout());
        add(new JLabel(new DateFormatSymbols().getMonths()[month - 1], JLabel.CENTER), BorderLayout.NORTH);
        add(new JScrollPane(new JTable(new MeMonthTableModel(year, month))), BorderLayout.CENTER);
    }
    public Dimension getPreferredSize() {
        return new Dimension(220, 180); // ???
    }
}

class MeMonthTableModel extends AbstractTableModel {
    private final int _year, _month;
    private final String[] _weekdays = new DateFormatSymbols().getShortWeekdays();
    private final Calendar _cal = Calendar.getInstance();

    public MeMonthTableModel(int year, int month) {
        _year = year; _month = month;
    }
    public int getColumnCount() {
        return 7;
    }
    public String getColumnName(int col) {
        return _weekdays[col + 1];
    }
    public int getRowCount() {
        return 6;
    }
    public Object getValueAt(int row, int col) {
        _cal.set(Calendar.YEAR, _year);
        _cal.set(Calendar.MONTH, _month - 1);      // starts from 0
        _cal.set(Calendar.DAY_OF_WEEK, col + 1);   // starts from 1
        _cal.set(Calendar.WEEK_OF_MONTH, row + 1); // starts from 1
        if (_cal.get(Calendar.DAY_OF_WEEK) == col + 1 && _cal.get(Calendar.WEEK_OF_MONTH) == row + 1) {
            return String.valueOf(_cal.get(Calendar.DATE));
        } else {
            return "";
        }
    }
}

他言語化。(ウィンドウタイトルはそのままだけど、まあいいか)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
--- MeApp.java.old  Mon Dec 31 18:21:09 2007
+++ MeApp.java  Mon Dec 31 18:21:22 2007
@@ -35,7 +35,7 @@
 class MeMonthPanel extends JPanel {
     public MeMonthPanel(int year, int month) {
         setLayout(new BorderLayout());
-        add(new JLabel(String.format("%dŒŽ", month), JLabel.CENTER), BorderLayout.NORTH);
+        add(new JLabel(new DateFormatSymbols().getMonths()[month - 1], JLabel.CENTER), BorderLayout.NORTH);
         add(new MeMonthCanvas(year, month), BorderLayout.CENTER);
     }
 }

Index

Feed

Other

Link

Pathtraq

loading...