/****************************************/ /* */ /* 万年暦 ver.1.00 */ /* */ /* 1997/ 2/15 宍戸 輝光 */ /* */ /****************************************/ import java.awt.*; import java.util.Date; public class cl100 extends java.applet.Applet { String day[]={"Sun","Mon","Tue","Wed","Thu","fri","Sat"}; int n[]={31,28,31,30,31,30,31,31,30,31,30,31}; /*月の日数*/ int year,month,i,j,week; Date d; Label yearLabel,monthLabel; TextField input_year,input_month; Button go; Panel p1,p2; Font f; Image screen; Graphics g_screen; public void init() { resize(400,340); setBackground(Color.green); screen=createImage(360,256); g_screen=screen.getGraphics(); yearLabel=new Label(); monthLabel=new Label(); input_year=new TextField(6); input_month=new TextField(4); go=new Button(); f=new Font("TimesRoman",Font.PLAIN,12); input_year.setFont(f); input_month.setFont(f); input_year.setBackground(Color.white); input_month.setBackground(Color.white); yearLabel.setFont(f); monthLabel.setFont(f); go.setFont(f); f=new Font("TimesRoman",Font.BOLD,16); g_screen.setFont(f); yearLabel.setText("year"); monthLabel.setText("month"); go.setLabel("G o"); p1=new Panel(); p2=new Panel(); setLayout((LayoutManager)null); add(p1); add(p2); p1.reshape(0,0,400,32); p2.reshape(0,292,400,48); p1.setLayout(new FlowLayout(FlowLayout.CENTER,16,4)); p2.setLayout(new FlowLayout(FlowLayout.CENTER,16,4)); p1.add(yearLabel); p1.add(input_year); p1.add(monthLabel); p1.add(input_month); p2.add(go); d=new Date(); input_year.setText(String.valueOf(d.getYear()+1900));/*現在の年*/ input_month.setText(String.valueOf(d.getMonth()+1));/*現在の月*/ drawScreen(); repaint(); } public void drawScreen() { String s; int x,y,pn; FontMetrics fm; g_screen.setColor(Color.white); g_screen.fillRect(1,1,358,255); g_screen.setColor(Color.black); g_screen.drawRect(0,0,359,255); try { month=Integer.parseInt(input_month.getText()); year=Integer.parseInt(input_year.getText()); } catch (NumberFormatException e) { /*入力エラー*/ input_year.setText(String.valueOf(d.getYear()+1900)); input_month.setText(String.valueOf(d.getMonth()+1)); year=d.getYear()+1900; month=d.getMonth()+1; } if (year<1 ||year>9999 ||month<1 ||month>12) {/*無効な入力*/ input_year.setText(String.valueOf(d.getYear()+1900)); input_month.setText(String.valueOf(d.getMonth()+1)); year=d.getYear()+1900; month=d.getMonth()+1; } s=year+" / "+month; /*表示用年月文字列*/ for (i=1;i<8;i++) /*線を引く*/ g_screen.drawLine(0,i*32+16,360,i*32+16); g_screen.setFont(new Font("TimesRoman",Font.PLAIN,12)); for (i=0;i<7;i++) /*曜日名表示*/ g_screen.drawString(day[i],i*48+4,47); f=new Font("TimesRoman",Font.PLAIN,16); g_screen.setFont(f); fm=g_screen.getFontMetrics(f); /*年月表示のセンタリング処理*/ x=(int)((360-fm.stringWidth(s))/2); y=(int)((32-fm.getAscent())/2); g_screen.setColor(Color.black); g_screen.drawString(s,x,32-y); /*年月表示*/ if ((year % 4)==0 && (year % 100)!=0) /*閏年なら2月は29日まで*/ n[1]=29; else if ((year % 400)==0) n[1]=29; else n[1]=28; pn=0; for (i=0;i6) { i=0; week++; } } while(j<=n[month-1]); } public int first(int year) { /*指定した年の元旦の曜日を返す*/ int f; f=year+(year-1)/4-(year-1)/100+(year-1)/400; /*元旦の曜日のずれ*/ return (f % 7); /*ずれから曜日を求める*/ } public boolean action(Event evt,Object what) { if (evt.target==go) { /*Go ボタンの処理*/ drawScreen(); /*カレンダー作成*/ repaint(); /*画面表示*/ return true; } return false; } public void paint(Graphics g){ g.drawImage(screen,20,32,this); /*カレンダーのイメージを表示*/ } public void update(Graphics g){ /*updateをオーバーライド*/ paint(g); } }