📄 jxmonthview.java
字号:
if ((_dropShadowMask & WEEK_DROP_SHADOW) != 0) { g.setColor(shadowColor); g.drawString(_daysOfTheWeek[dayIndex], tmpX + 1, tmpY + 1); g.setColor(getDaysOfTheWeekForeground()); } g.drawString(_daysOfTheWeek[dayIndex], tmpX, tmpY); dayIndex++; if (dayIndex == DAYS_IN_WEEK) { dayIndex = 0; } } g.setFont(oldFont); fm = oldFM; } // Check if the month to paint falls in the clip. _bounds.x = _startX + (_ltr ? column * (_calendarWidth + CALENDAR_SPACING) : -(column * (_calendarWidth + CALENDAR_SPACING) + _calendarWidth)); _bounds.y = _startY + row * (_calendarHeight + CALENDAR_SPACING); _bounds.width = _calendarWidth; _bounds.height = _calendarHeight; // Paint the month if it intersects the clip. If we don't move // the calendar forward a month as it would have if paintMonth // was called. if (_bounds.intersects(clip)) { paintMonth(g, column, row); } else { _cal.add(Calendar.MONTH, 1); } x += _ltr ? _calendarWidth + CALENDAR_SPACING : -(_calendarWidth + CALENDAR_SPACING); } y += _calendarHeight + CALENDAR_SPACING; } // Restore the calendar. _cal.setTimeInMillis(_firstDisplayedDate); if (g2 != null && _antiAlias) { g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, oldAAValue); } } /** * Paints a month. It is assumed the calendar, _cal, is already set to the * first day of the month to be painted. * * @param col X (column) the calendar is displayed in. * @param row Y (row) the calendar is displayed in. * @param g Graphics object. */ private void paintMonth(Graphics g, int col, int row) { String numericDay; int days = _cal.getActualMaximum(Calendar.DAY_OF_MONTH); FontMetrics fm = g.getFontMetrics(); Rectangle clip = g.getClipBounds(); long nextFlaggedDate = -1; int flaggedDateIndex = 0; if (_flaggedDates != null && _flaggedDates.length > 0) { nextFlaggedDate = _flaggedDates[flaggedDateIndex]; } for (int i = 0; i < days; i++) { calculateBoundsForDay(_bounds); if (_bounds.intersects(clip)) { numericDay = _dayOfMonthFormatter.format(_cal.getTime()); // Paint bounding box around any date that falls within the // selection. if (isSelectedDate(_cal.getTimeInMillis())) { // Keep track of the rectangle for the currently // selected date so we don't have to recalculate it // later when it becomes unselected. This is only // useful for SINGLE_SELECTION mode. if (_selectionMode == SINGLE_SELECTION) { _dirtyRect.x = _bounds.x; _dirtyRect.y = _bounds.y; _dirtyRect.width = _bounds.width; _dirtyRect.height = _bounds.height; } paintSelectedDayBackground(g, _bounds.x, _bounds.y, _bounds.width, _bounds.height); g.setColor(getForeground()); } // Paint bounding box around today. if (_cal.getTimeInMillis() == _today) { paintTodayBackground(g, _bounds.x, _bounds.y, _bounds.width, _bounds.height); g.setColor(getForeground()); } // If the appointment date is less than the current // calendar date increment to the next appointment. while (nextFlaggedDate != -1 && nextFlaggedDate < _cal.getTimeInMillis()) { flaggedDateIndex++; if (flaggedDateIndex < _flaggedDates.length) { nextFlaggedDate = _flaggedDates[flaggedDateIndex]; } else { nextFlaggedDate = -1; } } // Paint numeric day of the month. g.setColor(getDayForeground(_cal.get(Calendar.DAY_OF_WEEK))); if (nextFlaggedDate != -1 && _cal.getTimeInMillis() == nextFlaggedDate) { Font oldFont = getFont(); FontMetrics oldFM = fm; g.setFont(_derivedFont); fm = getFontMetrics(_derivedFont); g.drawString(numericDay, _ltr ? _bounds.x + _boxPaddingX + _boxWidth - fm.stringWidth(numericDay): _bounds.x + _boxPaddingX + _boxWidth - fm.stringWidth(numericDay) - 1, _bounds.y + _boxPaddingY + fm.getAscent()); g.setFont(oldFont); fm = oldFM; } else { g.drawString(numericDay, _ltr ? _bounds.x + _boxPaddingX + _boxWidth - fm.stringWidth(numericDay): _bounds.x + _boxPaddingX + _boxWidth - fm.stringWidth(numericDay) - 1, _bounds.y + _boxPaddingY + fm.getAscent()); } } _cal.add(Calendar.DAY_OF_MONTH, 1); } } /** * Paints the background of the month string. The bounding box for this * background can be modified by setting its insets via * setMonthStringInsets. The color of the background can be set via * setMonthStringBackground. * * @see #setMonthStringBackground * @see #setMonthStringInsets * @param g Graphics object to paint to. * @param x x-coordinate of upper left corner. * @param y y-coordinate of upper left corner. * @param width width of the bounding box. * @param height height of the bounding box. */ protected void paintMonthStringBackground(Graphics g, int x, int y, int width, int height) { // Modify bounds by the month string insets. x = _ltr ? x + _monthStringInsets.left : x + _monthStringInsets.right; y = y + _monthStringInsets.top; width = width - _monthStringInsets.left - _monthStringInsets.right; height = height - _monthStringInsets.top - _monthStringInsets.bottom; Graphics2D g2 = (Graphics2D)g; GradientPaint gp = new GradientPaint(x, y + height, new Color(238, 238, 238), x, y, new Color(204, 204, 204)); //paint the border// g.setColor(_monthStringBackground); g2.setPaint(gp); g2.fillRect(x, y, width - 1, height - 1); g2.setPaint(new Color(153, 153, 153)); g2.drawRect(x, y, width - 1, height - 1); //TODO The right side of the rect is being clipped } /** * Paints the background for today. The default is a rectangle drawn in * using the color set by <code>setTodayBackground</code> * * @see #setTodayBackground * @param g Graphics object to paint to. * @param x x-coordinate of upper left corner. * @param y y-coordinate of upper left corner. * @param width width of bounding box for the day. * @param height height of bounding box for the day. */ protected void paintTodayBackground(Graphics g, int x, int y, int width, int height) {// g.setColor(_todayBackgroundColor);// g.drawRect(x, y, width - 1, height - 1); //paint the gradiented border GradientPaint gp = new GradientPaint(x, y, new Color(91, 123, 145), x, y + height, new Color(68, 86, 98)); Graphics2D g2 = (Graphics2D)g; g2.setPaint(gp); g2.drawRect(x, y, width - 1, height - 1); } /** * Paint the background for a selected day. The default is a filled * rectangle in the in the component's background color. * * @param g Graphics object to paint to. * @param x x-coordinate of upper left corner. * @param y y-coordinate of upper left corner. * @param width width of bounding box for the day. * @param height height of bounding box for the day. */ protected void paintSelectedDayBackground(Graphics g, int x, int y, int width, int height) { g.setColor(getSelectedBackground()); g.fillRect(x, y, width, height); } /** * Returns true if the specified time falls within the _startSelectedDate * and _endSelectedDate range. */ private boolean isSelectedDate(long time) { return time >= _startSelectedDate && time <= _endSelectedDate; } /** * Calculates the _numCalCols/_numCalRows that determine the number of * calendars that can be displayed. */ private void calculateNumDisplayedCals() { int oldNumCalCols = _numCalCols; int oldNumCalRows = _numCalRows; // Determine how many columns of calendars we want to paint. _numCalCols = 1; _numCalCols += (getWidth() - _calendarWidth) / (_calendarWidth + CALENDAR_SPACING); // Determine how many rows of calendars we want to paint. _numCalRows = 1; _numCalRows += (getHeight() - _calendarHeight) / (_calendarHeight + CALENDAR_SPACING); if (oldNumCalCols != _numCalCols || oldNumCalRows != _numCalRows) { calculateLastDisplayedDate(); } } /** * Calculates the _startX/_startY position for centering the calendars * within the available space. */ private void calculateStartPosition() { // Calculate offset in x-axis for centering calendars. _startX = (getWidth() - ((_calendarWidth * _numCalCols) + (CALENDAR_SPACING * (_numCalCols - 1)))) / 2; if (!_ltr) { _startX = getWidth() - _startX; } // Calculate offset in y-axis for centering calendars. _startY = (getHeight() - ((_calendarHeight * _numCalRows) + (CALENDAR_SPACING * (_numCalRows - 1 )))) / 2; } /** * Calculate the bounding box for drawing a date. It is assumed that the * calendar, _cal, is already set to the date you want to find the offset * for. * * @param bounds Bounds of the date to draw in. */ private void calculateBoundsForDay(Rectangle bounds) { int year = _cal.get(Calendar.YEAR); int month = _cal.get(Calendar.MONTH); int dayOfWeek = _cal.get(Calendar.DAY_OF_WEEK); int weekOfMonth = _cal.get(Calendar.WEEK_OF_MONTH); // Determine what row/column we are in. int diffMonths = month - _firstDisplayedMonth + ((year - _firstDisplayedYear) * MONTHS_IN_YEAR); int calRowIndex = diffMonths / _numCalCols; int calColIndex = diffMonths - (calRowIndex * _numCalCols); // Modify the index relative to the first day of the week. bounds.x = dayOfWeek - _firstDayOfWeek; if (bounds.x < 0) { bounds.x += DAYS_IN_WEEK; } // Offset for location of the day in the week. bounds.x = _ltr ? bounds.x * (_boxPaddingX + _boxWidth + _boxPaddingX) : (bounds.x + 1) * (_boxPaddingX + _boxWidth + _boxPaddingX); // Offset for the column the calendar is displayed in. bounds.x += calColIndex * (_calendarWidth + CALENDAR_SPACING); // Adjust by centering value. bounds.x = _ltr ? _startX + bounds.x : _startX - bounds.x; // Initial offset for Month and Days of the Week display. bounds.y = _boxPaddingY + _monthBoxHeight + _boxPaddingY + + _boxPaddingY + _boxHeight + _boxPaddingY; // Offset for centering and row the calendar is displayed in. bounds.y += _startY + calRowIndex * (_calendarHeight + CALENDAR_SPACING); // Offset for Week of the Month. bounds.y += (weekOfMonth - 1) * (_boxPaddingY + _boxHeight + _boxPaddingY); bounds.width = _boxPaddingX + _boxWidth + _boxPaddingX; bounds.height = _boxPaddingY + _boxHeight + _boxPaddingY; } /** * Return a long representing the date at the specified x/y position. * The date returned will have a valid day, month and year. Other fields * such as hour, minute, second and milli-second will be set to 0. * * @param x X position * @param y Y position * @return long The date, -1 if position does not contain a date. */ public long getDayAt(int x, int y) { if (_ltr ? (_startX > x) : (_startX < x) || _startY > y) { return -1; } // Determine which column of calendars we're in. int calCol = (_ltr ? (x - _startX) : (_startX - x)) / (_calendarWidth + CALENDAR_SPACING); // Determine which row of calendars we're in. int calRow = (y - _startY) / (_calendarHeight + CALENDAR_SPACING); if (calRow > _numCalRows - 1 || calCol > _numCalCols - 1) { return -1; } // Determine what row (week) in the selected month we're in. int row = 1; row += (((y - _startY) - (calRow * (_calendarHeight + CALENDAR_SPACING))) - (_boxPaddingY + _monthBoxHeight + _boxPaddingY)) / (_boxPaddingY + _boxHeight + _boxPaddingY); // The first two lines in the calendar are the month and the days // of the week. Ignore them. row -= 2; if (row < 0 || ro
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -