⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datefield.java

📁 一个 JAVA 写的 J2ME 开源模拟器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        int offset = (w - stringWidth) >>> 1;
        int dOff = offset;
        int del1Off = dOff + dayW;
        int mOff = del1Off + delimiterW;
        int del2Off = mOff + monthW;
        int yOff = del2Off + delimiterW;
        
        g.setColor(0);
        g.setFont(font);
        // draw the delimiter 
        g.drawString(delimiterStr, del1Off, y, Graphics.LEFT | Graphics.TOP);
        g.drawString(delimiterStr, del2Off, y, Graphics.LEFT | Graphics.TOP);
        
        // draw the rectangles 
        
        // colors  
        int colorR, colorT;

        if (selected == 0) {
            colorR = 0x000000;
            colorT = 0xffffff;
        } else {
            colorR = 0xffffff;
            colorT = 0x000000;
        }
        
        g.setColor(colorR);
        g.fillRect(dOff, y, dayW, font.getHeight());
        g.setColor(colorT);
        g.drawString(dayStr, dOff, y, Graphics.LEFT | Graphics.TOP);

        if (selected == 1) {
            colorR = 0x000000;
            colorT = 0xffffff;
        } else {
            colorR = 0xffffff;
            colorT = 0x000000;
        }

        g.setColor(colorR);
        g.fillRect(mOff, y, monthW, font.getHeight());
        g.setColor(colorT);
        g.drawString(monthStr, mOff, y, Graphics.LEFT | Graphics.TOP);

        if (selected == 2) {
            colorR = 0x000000;
            colorT = 0xffffff;
        } else {
            colorR = 0xffffff;
            colorT = 0x000000;
        }

        g.setColor(colorR);
        g.fillRect(yOff, y, yearW, font.getHeight());
        g.setColor(colorT);
        g.drawString(yearStr, yOff, y, Graphics.LEFT | Graphics.TOP);
	}

	public synchronized void keyPressed(int keycode) {
        int k = getGameAction(keycode);
        
        if (k == Canvas.LEFT && selected > 0) {
            selected--;
            repaint();
        } else if (k == Canvas.RIGHT && selected < 2) {
            selected++;
            repaint();
        } else if (k == Canvas.UP) {
            Calendar cal = Calendar.getInstance();

            switch (selected) {
                case 0:  // day
                	cal.set(Calendar.YEAR, year);
                	cal.set(Calendar.MONTH, month);
                	cal.set(Calendar.DAY_OF_MONTH, day);
                	cal.set(Calendar.HOUR_OF_DAY, 1);
                	
                	cal.setTime(cal.getTime());
                	cal.add(Calendar.DAY_OF_MONTH, 1);
                	if(cal.get(Calendar.MONTH) == month)
                		day++;
                	else 
                		day = 1;
                	break;
                case 1: // month
                	if (month == Calendar.DECEMBER)
                		month = Calendar.JANUARY;
                	else 
                		month++;

                	cal.set(Calendar.YEAR, year);
                	cal.set(Calendar.MONTH, month);
                	cal.set(Calendar.DAY_OF_MONTH, 28);
                	cal.set(Calendar.HOUR_OF_DAY, 1);
                	
                	cal.setTime(cal.getTime());
                	cal.add(Calendar.DAY_OF_MONTH, 4);
                	int daysInMonth = 28+(4-cal.get(Calendar.DAY_OF_MONTH));
                	
                	if (day > daysInMonth)
                		day = daysInMonth;
                	break;
                case 2: // year
                	// arbitrary limit
                	if (year < 5000) {
                		year++;
                		
                		// here i simply use the fact that there
                		// were nor will be two lenient years in
                		// a row
	                	if (day == 29 && month == Calendar.FEBRUARY)
	                		day = 28;
                	}
                	break;
            }
            repaint();
        } else if (k == Canvas.DOWN) {
            Calendar cal = Calendar.getInstance();

            switch (selected) {
                case 0:  // day
                	if(day > 1)
                		day--;
                	else {
                    	cal.set(Calendar.YEAR, year);
                    	cal.set(Calendar.MONTH, month);
                    	cal.set(Calendar.DAY_OF_MONTH, 28);
                    	cal.set(Calendar.HOUR_OF_DAY, 1);
                    	
                    	cal.setTime(cal.getTime());
                    	cal.add(Calendar.DAY_OF_MONTH, 4);
                    	int daysInMonth = 28+(4-cal.get(Calendar.DAY_OF_MONTH));
                    	day = daysInMonth;
                	}
                	break;
                case 1: // month
                	if (month == Calendar.JANUARY)
                		month = Calendar.DECEMBER;
                	else 
                		month--;

                	cal.set(Calendar.YEAR, year);
                	cal.set(Calendar.MONTH, month);
                	cal.set(Calendar.DAY_OF_MONTH, 28);
                	cal.set(Calendar.HOUR_OF_DAY, 1);
                	
                	cal.setTime(cal.getTime());
                	cal.add(Calendar.DAY_OF_MONTH, 1);
                	int daysInMonth = 28+(4-cal.get(Calendar.DAY_OF_MONTH));
                	
                	if (day > daysInMonth)
                		day = daysInMonth;
                	break;
                case 2: // year
                	// arbitrary limit
                	if (year > 1000) {
                		year--;
                		// here i simply use the fact that there
                		// were nor will be two lenient years in
                		// a row
	                	if (day == 29 && month == Calendar.FEBRUARY)
	                		day = 28;
                	}
                	break;
            }
            repaint();
        }
    }
}

class TimeCanvas extends Canvas {
	Calendar cal;
	private int minutes, hours;
	private int selected;
	
	public TimeCanvas() {
		cal = Calendar.getInstance();
	}

	public Date getTime() {
		this.cal.set(Calendar.HOUR_OF_DAY, hours);
		this.cal.set(Calendar.MINUTE, minutes);
		return cal.getTime();
	}

	public void setTime(Date time) {
		this.cal.setTime(time);
		this.hours = cal.get(Calendar.HOUR_OF_DAY);
		this.minutes = cal.get(Calendar.MINUTE);
		repaint();
	}

    public void paint(Graphics g) {
        int w = this.getWidth();
        int h = this.getHeight();

        g.setColor(0xffffff);
        g.fillRect(0, 0, w, h);
        
        Font font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, 
                Font.SIZE_MEDIUM);

        String hoursStr = Integer.toString(hours);
        if (hours < 10)
            hoursStr = "0" + hoursStr;
        String minutesStr = Integer.toString(minutes);
        if (minutes < 10)
            minutesStr = "0" + minutesStr;
        String delimiterStr = " : ";
        
        int y = (h - font.getHeight()) >>> 1;

        int hoursW = font.stringWidth(hoursStr);
        int minutesW = font.stringWidth(minutesStr);
        int delimiterW = font.stringWidth(delimiterStr);

        int stringWidth = hoursW + minutesW + delimiterW;
        int offset = (w - stringWidth) >>> 1;
        int hOff = offset;
        int dOff = offset + hoursW;
        int mOff = dOff + delimiterW;
        
        g.setColor(0);
        g.setFont(font);
        // draw the delimiter 
        g.drawString(delimiterStr, dOff, y, Graphics.LEFT | Graphics.TOP);
        
        // draw the rectangles 
        
        // colors  
        int colorR, colorT;

        if (selected == 0) {
            colorR = 0x000000;
            colorT = 0xffffff;
        } else {
            colorR = 0xffffff;
            colorT = 0x000000;
        }
        
        g.setColor(colorR);
        g.fillRect(hOff, y, hoursW, font.getHeight());
        g.setColor(colorT);
        g.drawString(hoursStr, hOff, y, Graphics.LEFT | Graphics.TOP);

        if (selected == 1) {
            colorR = 0x000000;
            colorT = 0xffffff;
        } else {
            colorR = 0xffffff;
            colorT = 0x000000;
        }

        g.setColor(colorR);
        g.fillRect(mOff, y, minutesW, font.getHeight());
        g.setColor(colorT);
        g.drawString(minutesStr, mOff, y, Graphics.LEFT | Graphics.TOP);
    }

    public synchronized void keyPressed(int keycode) {
        int k = getGameAction(keycode);
        
        if (k == Canvas.LEFT && selected > 0) {
            selected--;
            repaint();
        } else if (k == Canvas.RIGHT && selected < 1) {
            selected++;
            repaint();
        } else if (k == Canvas.UP) {
            switch (selected) {
                case 0:  // hours
                    hours++;
                    if (hours > 23)
                        hours = 0;
                    break;
                case 1: // minutes
                    minutes++;
                    if (minutes > 59)
                        minutes = 0;
                    break;
            }
            repaint();
        } else if (k == Canvas.DOWN) {
            switch (selected) {
                case 0:  // hours
                    hours--;
                    if (hours < 0)
                        hours = 23;
                    break;
                case 1: // minutes
                    minutes--;
                    if (minutes < 0)
                        minutes = 59;
                    break;
            }
            repaint();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -