四个类
设计思路:
1、星期(1-7)的绘制
2、5行7列(日期)的绘制
3、当前日期彩色框的绘制
4、日期的查询
5、两个面板周边彩色框的绘制
************************************************************************************************************************************
1、MyPanel(日历面板)
package my;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Calendar;
import javax.swing.JPanel;
public class MyPanel extends JPanel
{
Calendar cal=Calendar.getInstance();
int theMonth=cal.get(Calendar.MONTH); //当前是几月
int theDay=cal.get(Calendar.DAY_OF_MONTH);//当前是几号
boolean flag=false; //用于标记是否开始绘制DAY_OF_MONTH;
public MyPanel()
{
}
@Override
protected void paintComponent(Graphics g)
{
int width=this.getWidth();
int height=this.getHeight();
Graphics2D g2d=(Graphics2D)g;
//平滑绘制
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.white);
g2d.fillRect(0, 0, width, height);
int x=0,y=0;
int x_size=40; //单元格宽度
int y_size=40; //单元格高度
// 第一行:一、二、三、四、五、六、日
g2d.setPaint(Color.GRAY);
g2d.drawLine(x+100, y, x_size*7+100, y);
g2d.drawLine(x+100, y_size, x+x_size*7+100, y+y_size);
String[] cc = {"一", "二", "三", "四", "五", "六", "日" };
g2d.setPaint(Color.GRAY);
g2d.setFont(new Font("宋体",0,20));
//drawText(g2d," ",new Rectangle(0,0,40,40));
x=x+100;
y=y;
for(int i=0;i<7;i++)
{
Rectangle rect=new Rectangle(x+i*x_size,y,x_size,y_size);
drawText(g2d,cc[i],rect);
}
//计算本月第一天是星期几
cal.set(Calendar.DAY_OF_MONTH, 1); //向前推到当月第一天
int weekday=cal.get(Calendar.DAY_OF_WEEK);//计算本月第一天周几
//往前推N天
// Calendar里规定Sunday=1(周日), Monday=2, ..., Saturday=7(周六)
// 它是周一,则前推0天,周二,推1天,周六,推5天,周日,推6天
int start=weekday-Calendar.MONDAY;
if(start <0) start = 6;
cal.add(Calendar.DAY_OF_MONTH, 0 - start); // 前推,从上月开始显示
//绘制5行
//每月最多31天,可分5行显示
x=0;
y=y+y_size;
for(int i=0; i<5; i++) // 5行
{
drawText(g2d," ",new Rectangle(0,40,100,40));
for(int j=0; j<7; j++) // 7列
{
flag=true;//开始绘制月号
// 判断月份是否相同
if(cal.get(Calendar.MONTH) == theMonth)
{
g2d.setPaint(Color.BLACK);
}
else
{
g2d.setPaint(Color.GRAY);
}
int day = cal.get(Calendar.DAY_OF_MONTH);
Rectangle rect = new Rectangle(x+x_size*j+100, y+y_size*i, x_size, y_size);
drawText(g2d, String.valueOf(day), rect);
// 日期 +1
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
//彩色边框
if(true)
{
int w=this.getWidth();
int h=this.getHeight();
Graphics2D g2=(Graphics2D)g;
//设置线型
Stroke stroke=new BasicStroke(10);
g2.setStroke(stroke);
Color c1=Color.GREEN;
Color c2=Color.RED;
//drawLine(g2,0,0,w,0,c1,c2);
drawLine(g2,0,0,0,h,c1,c2);
drawLine(g2,w,h,w,0,c1,c2);
drawLine(g2,w,h,0,h,c1,c2);
}
}
// 将str绘制在矩形rect中,居中显示
private void drawText(Graphics2D g2d, String str, Rectangle rect)
{
FontMetrics fm = g2d.getFontMetrics(g2d.getFont());
int fontSize = fm.getHeight(); // 字高
int textWidth = fm.stringWidth(str);
int leading = fm.getLeading();
int ascent = fm.getAscent(); // top -> baseline 的高度
int descent = fm.getDescent(); // bottom->baseline 的高度
// System.out.println(fontSize + "," + leading + "," + ascent + "," + descent);
//判断日期是否相同
if(cal.get(Calendar.DAY_OF_MONTH)==theDay&&cal.get(Calendar.MONTH) == theMonth&&flag)
{
//设置线型
Stroke stroke=new BasicStroke(3);
g2d.setStroke(stroke);
Color c1=Color.GREEN;
Color c2=Color.RED;
//左上角
int x=rect.x;
int y=rect.y;
//右上角
int x1=rect.x+rect.width;
int y1=rect.y;
//右下角
int x2=rect.x+rect.width;
int y2=rect.y+rect.height;
//左下角
int x3=rect.x;
int y3=rect.y+rect.height;
drawLine(g2d,x,y,x1,y1,c1,c2);
drawLine(g2d,x1,y1,x2,y2,c2,c1);
drawLine(g2d,x2,y2,x3,y3,c1,c2);
drawLine(g2d,x3,y3,x,y,c2,c1);
}
int x = 0, y = 0;
x = rect.x + (rect.width - textWidth)/2; // 水平居中
y = rect.y + rect.height /2 + (fontSize-leading)/2 - descent; // 竖直居中
g2d.drawString(str, x, y);
}
//绘制
private void drawLine(Graphics2D g2d,
int x1,int y1,int x2,int y2,
Color c1,Color c2)
{
//Point2D
Point2D start=new Point2D.Double(x1, y1); //起点
Point2D end=new Point2D.Double(x2, y2); //终点(渐变的方向)
float[] dist= {0.0f,1.0f}; //插入关键点
Color[] colors= {c1,c2}; //关键点的颜色值
Paint paint=new LinearGradientPaint(start,end,dist,colors);
//设置Paint
g2d.setPaint(paint);
//构造一个shape
Shape shape=new Line2D.Double(start, end);
g2d.draw(shape);
}
public String showTime()
{
Calendar c=Calendar.getInstance();
String time=c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+theDay+"日";
//System.out.println(time);
return time;
}
}
2、RowPanle(查询面板)
package my;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JPanel;
public class RowPanel extends JPanel
{
@Override
protected void paintComponent(Graphics g) {
int w=this.getWidth();
int h=this.getHeight();
Graphics2D g2=(Graphics2D)g;
//设置线型
Stroke stroke=new BasicStroke(10);
g2.setStroke(stroke);
Color c1=Color.GREEN;
Color c2=Color.RED;
drawLine(g2,0,0,w,0,c2,c1);
drawLine(g2,0,0,0,h,c2,c1);
drawLine(g2,w,h,w,0,c2,c1);
//drawLine(g2,w,h,0,h,c1,c2);
}
private void drawLine(Graphics2D g2d,
int x1,int y1,int x2,int y2,
Color c1,Color c2)
{
//Point2D
Point2D start=new Point2D.Double(x1, y1); //起点
Point2D end=new Point2D.Double(x2, y2); //终点(渐变的方向)
float[] dist= {0.0f,1.0f}; //插入关键点
Color[] colors= {c1,c2}; //关键点的颜色值
Paint paint=new LinearGradientPaint(start,end,dist,colors);
//设置Paint
g2d.setPaint(paint);
//构造一个shape
Shape shape=new Line2D.Double(start, end);
g2d.draw(shape);
}
}
3、主面板
package my;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyFrame extends JFrame
{
JButton button=new JButton("当前查询");
JTextField textField=new JTextField(15);
//ContentPane
//JPanel root=new JPanel();
MyPanel panel=new MyPanel();
JPanel row=new RowPanel();
JPanel root=new JPanel();
public MyFrame(String title)
{
super(title);
this.setContentPane(root);
root.setLayout(new BorderLayout());
row.add(button);
row.add(textField);
row.setBackground(Color.WHITE);
root.add(row, BorderLayout.PAGE_START);
root.add(panel, BorderLayout.CENTER);
//按钮事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
showTime();
}
});
}
private void showTime()
{
textField.setFont(new Font("宋体",Font.PLAIN,20));
textField.setText(panel.showTime());
}
}
4、MyDemo(主类)
package my;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MyDemo
{
private static void createGUI()
{
// JFrame指一个窗口,构造方法的参数为窗口标题
JFrame frame = new MyFrame("Js日历");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口的其他参数,如窗口大小
frame.setSize(500, 350);
// 显示窗口
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
createGUI();
}
});
}
}