dayselection.java
来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 455 行 · 第 1/2 页
JAVA
455 行
@return -1 if coordinates don't point to a day in the selected month
*/
private int calcDay(int x,int y) {
int col = (x - HBORDER) / e.cellWidth;
int row = (y - VBORDER) / e.cellHeight;
int field = 0;
// System.out.println(row + ":" + col);
if ( row > 0 && col < COLUMNS && row < ROWS) {
field =(row-1) * COLUMNS + col;
}
if (belongsToMonth(field)) {
return calcDay(field);
} else {
return -1;
}
}
private void createDays(Locale locale) {
NumberFormat format = NumberFormat.getInstance(locale);
// Try to simluate a right align of the numbers with a space
for ( int i=0; i <10; i++)
days[i] = " " + format.format(i);
for ( int i=10; i < 40; i++)
days[i] = format.format(i);
}
/** This method is necessary to shorten the names down to 2 characters.
Some date-formats ignore the setting.
*/
private String small(String string) {
return string.substring(0,Math.min(string.length(),2));
}
private void createWeekdays(Locale locale ) {
Calendar calendar = Calendar.getInstance(locale);
SimpleDateFormat format = new SimpleDateFormat("EE",locale);
calendar.set(Calendar.DAY_OF_WEEK,calendar.getFirstDayOfWeek());
for (int i=0;i<7;i++) {
weekday2slot[calendar.get(Calendar.DAY_OF_WEEK)] = i;
weekdayNames[i] = small(format.format(calendar.getTime()));
calendar.add(Calendar.DAY_OF_WEEK,1);
}
}
// calculates the rectangle for a given field
private void calculateRectangle(int field) {
int row = (field / COLUMNS) + 1;
int col = (field % COLUMNS);
calculateRectangle(row,col);
}
// calculates the rectangle for a given row and col
private void calculateRectangle(int row,int col) {
Rectangle r = e.r;
r.x = e.cellWidth * col + HBORDER;
r.y = e.cellHeight * row + VBORDER;
r.width = e.cellWidth;
r.height = e.cellHeight;
}
private void calculateTextPos(int field) {
int row = (field / COLUMNS) + 1;
int col = (field % COLUMNS);
calculateTextPos(row,col);
}
// calculates the text-anchor for a giver field
private void calculateTextPos(int row,int col) {
Point p = e.p;
p.x = e.cellWidth * col + HBORDER + (e.cellWidth - e.fontWidth)/2;
p.y = e.cellHeight * row + VBORDER + e.fontHeight - 1;
}
private boolean belongsToMonth(int field) {
return (calcDay(field) > 0 && calcDay(field) <= focusModel.daysMonth());
}
private void selectField(int x,int y) {
int day = calcDay(x,y);
if ( day >=0 ) {
model.setDate(day, focusModel.getMonth(), focusModel.getYear());
} // end of if ()
}
private Color getBackgroundColor(int field) {
int day = calcDay(field);
if (belongsToMonth(field) && dateRenderer != null) {
Color color = dateRenderer.getBackgroundColor(focusModel.getWeekday(day), day, focusModel.getMonth(), focusModel.getYear());
if (color != null)
return color;
}
return DEFAULT_CELL_BACKGROUND;
}
// return the Daytext, that should be displayed in the specified field
private String getText(int field) {
int index = 0;
if ( calcDay(field) < 1)
index = focusModel.daysLastMonth() + calcDay(field) ;
if (belongsToMonth(field))
index = calcDay(field);
if ( calcDay(field) > focusModel.daysMonth() )
index = calcDay(field) - focusModel.daysMonth();
return days[index];
}
static char[] test = {'1'};
private void drawField(int field,boolean highlight) {
if (field <0)
return;
Graphics g = e.g;
Rectangle r = e.r;
Point p = e.p;
calculateRectangle(field);
if ( field == selectedField ) {
g.setColor(SELECTION_BACKGROUND);
g.fillRect(r.x +2 ,r.y +2 ,r.width-4,r.height-4);
g.setColor(SELECTION_BORDER);
//g.drawRect(r.x,r.y,r.width-1,r.height-1);
g.drawRect(r.x+1,r.y+1,r.width-3,r.height-3);
if ( field == focusedField )
g.setColor(FOCUSCOLOR);
else
g.setColor(getBackgroundColor(field));
g.drawRect(r.x,r.y,r.width-1,r.height-1);
g.setColor(SELECTION);
} else if ( field == focusedField ) {
g.setColor(getBackgroundColor(field));
g.fillRect(r.x,r.y,r.width -1,r.height -1);
g.setColor(FOCUSCOLOR);
g.drawRect(r.x,r.y,r.width-1,r.height-1);
if ( highlight) {
g.setColor(SELECTABLE);
} else {
g.setColor(UNSELECTABLE);
} // end of else
} else {
g.setColor(getBackgroundColor(field));
g.fillRect(r.x ,r.y ,r.width ,r.height);
if ( highlight) {
g.setColor(SELECTABLE);
} else {
g.setColor(UNSELECTABLE);
} // end of else
}
calculateTextPos(field);
g.drawString(getText(field)
, p.x , p.y);
}
private void drawHeader() {
Graphics g = e.g;
Point p = e.p;
g.setColor(HEADER_BACKGROUND);
g.fillRect(HBORDER,VBORDER, getSize().width - 2 * HBORDER, e.cellHeight);
g.setColor(HEADER);
for ( int i=0; i < COLUMNS; i++) {
calculateTextPos(0,i);
g.drawString(weekdayNames[i], p.x,p.y);
}
}
private void paintComplete() {
drawHeader();
int field = 0;
int start = weekday2slot[focusModel.firstWeekday()];
int end= start + focusModel.daysMonth() -1 ;
for ( int i=0; i< start; i++) {
drawField(field ++,false);
}
for ( int i= start; i <= end ; i++) {
drawField(field ++,true);
}
for ( int i= end + 1; i< FIELDS ; i++) {
drawField(field ++,false);
}
}
private void updateEnvironment(Graphics g) {
e.cellWidth = (getSize().width - HBORDER * 2) / COLUMNS;
e.g = g;
}
public void paint(Graphics g) {
super.paint(g);
if (e==null)
if (getFont() != null )
// We need the font-size to calculate the component size
calculateSizes();
else
return;
updateEnvironment(g);
paintComplete();
drawField(selectedField,true);
drawField(focusedField,true);
}
}
// Environment stores the values that would normaly be stored on the stack.
// This is to speedup performance, e.g. new Point and new Rectangle are only called once.
class PaintEnvironment {
Graphics g;
int cellWidth;
int cellHeight;
int fontWidth;
int fontHeight;
Point p = new Point();
Rectangle r = new Rectangle();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?