/** check Define festival */ private boolean checkDefineFestival(String normalDate) { if (normalDate != null) { if (CodeConstants.DEFINE_FESTIVAL.get(normalDate) != null) { return true; } } return false; }
/** The key method of <code>Cell</code>: how to draw date number on the cell. */ @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (isHeader()) return; // Do not render the header if (day > 0) { AttributedString dayAttrStr; Font font = MonthCalendarImpl.this.getFont(); try { calendar.set(Calendar.DATE, day); String dayStr = Integer.toString(day); dayAttrStr = new AttributedString(dayStr); dayAttrStr.addAttribute( TextAttribute.FONT, isToday(calendar) ? font.deriveFont(Font.BOLD, font.getSize() + SHRINK + SHRINK) : font.deriveFont(Font.PLAIN, font.getSize() + SHRINK + 3)); Graphics2D g2 = (Graphics2D) g; TextLayout textLayout = new TextLayout(dayAttrStr.getIterator(), g2.getFontRenderContext()); int width = textLayout.getBounds().getBounds().width; Dimension size = getSize(); int x = size.width > width ? (size.width - width) / 2 : 0; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Draw in the center g2.drawString(dayAttrStr.getIterator(), x, (int) (textLayout.getAscent())); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); String normalDate = (month < 10 ? "0" + month : month) + "" + (day < 10 ? "0" + day : day); String lunarInfo = ""; if (checkNormalFestival(normalDate)) { lunarInfo = CodeConstants.NORMAL_FESTIVAL.get(normalDate); setForeground(Color.blue); } else if (checkDefineFestival(normalDate)) { lunarInfo = CodeConstants.DEFINE_FESTIVAL.get(normalDate); setForeground(Color.green); } else if (checkChineseYear(month, day)) { lunarInfo = CodeConstants.CHINESE_FESTIVAL.get("1299"); setForeground(Color.red); } else { // lunar date Lunar lunar = new Lunar(calendar); String lunarDate = lunar.getNumericMD(); if (checkChineseFestival(lunarDate)) { lunarInfo = CodeConstants.CHINESE_FESTIVAL.get(lunarDate); setForeground(Color.MAGENTA); } else { if (checkUnWorkFestival(normalDate + "W") || checkUnWorkFestival(lunarDate + "Y")) { setForeground(Color.red); } lunarInfo = lunar.toString(); } } AttributedString lunarStr = new AttributedString(lunarInfo); lunarStr.addAttribute( TextAttribute.FONT, isToday(calendar) ? font.deriveFont(Font.PLAIN, font.getSize()) : font); TextLayout textLayout2 = new TextLayout(lunarStr.getIterator(), g2.getFontRenderContext()); // Resume date calendar.set(Calendar.DATE, todayInMonth); int width2 = textLayout2.getBounds().getBounds().width; int x2 = size.width > width ? (size.width - width2) / 2 : 0; g2.drawString( lunarStr.getIterator(), x2, (int) (textLayout.getAscent() + textLayout.getDescent() + textLayout.getLeading() + textLayout2.getAscent())); } catch (ParseException e) { e.printStackTrace(); } } }