今天星期天,参加培训的第十七天,早上有点不想起床,一直到七点才起来,挺纠结的,很累,可能是游戏来去玩多了,其余的也没有什么,嗯早上也没有吃饭,反正就是很懒就对了,然后早上来了,看了一会书,补了一下昨天的日记,然后继续看书,有一个居中显示文字的代码,还有一个显示时钟的代码,感觉还好吧,其它的也没有什么,反正是看的不多,一上午就过去了,也不知道到底做了些什么,唉,很纠结。
中午,吃了饭,回来小睡了一会,然后,老师带着串了一下学过的知识,然后就下课了,到现在,我在写今天的日记,不知道该做什么了,脑袋有点晕晕的,什么也不想做,也好,那就什么也不做好了,要不然呢,对吧,好的,不说什么有的没的了,就先这样吧,毕竟实在是提不起干劲来。
贴一下今天的代码吧,凑一下今天的字数,行,就这样。
居中文字代码:
package graphics;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MessagePanel extends JPanel {
private String message = "welcome to java";
private int xCoordinate = 20;
private int yCoordinate = 20;
private boolean centered=true;
private int interval = 10;
public MessagePanel() {
}
public MessagePanel(String message) {
super();
this.message = message;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(centered) {
FontMetrics fm = g.getFontMetrics();
int stringWidth = fm.stringWidth(message);
int stringAscent = fm.getAscent();
xCoordinate = getWidth()/2-stringWidth/2;
yCoordinate = getHeight()/2+stringAscent/4;
}
g.drawString(message, xCoordinate, yCoordinate);
}
public void moveLeft() {
xCoordinate-=interval;
repaint();
}
public void moveRight() {
xCoordinate+=interval;
repaint();
}
public void moveUp() {
yCoordinate-=interval;
repaint();
}
public void moveDown() {
yCoordinate-=interval;
repaint();
}
public Dimension gePreferredSize() {
return new Dimension(200, 30);
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
repaint();
}
public int getxCoordinate() {
return xCoordinate;
}
public void setxCoordinate(int xCoordinate) {
this.xCoordinate = xCoordinate;
repaint();
}
public int getyCoordinate() {
return yCoordinate;
}
public void setyCoordinate(int yCoordinate) {
this.yCoordinate = yCoordinate;
repaint();
}
public boolean isCentered() {
return centered;
}
public void setCentered(boolean centered) {
this.centered = centered;
repaint();
}
public int getInterval() {
return interval;
}
public void setInterval(int interval) {
this.interval = interval;
repaint();
}
}
时钟代码:
package graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JPanel;
public class StillClock extends JPanel {
private int hour;
private int minute;
private int second;
public StillClock() {
super();
setCurrentTime();
}
public StillClock(int hour, int minute, int second) {
super();
this.hour = hour;
this.minute = minute;
this.second = second;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int clockRadius = (int)(Math.min(getWidth(), getHeight()*0.6*0.5));
int xCenter = getWidth()/2;
int yCenter = getHeight()/2;
g.setColor(Color.BLACK);
g.drawOval(xCenter-clockRadius, yCenter-clockRadius, clockRadius*2, clockRadius*2);
g.drawString("12", xCenter-5, yCenter-clockRadius+12);
g.drawString("9", xCenter-clockRadius+3, yCenter+5);
g.drawString("3",xCenter+clockRadius-10,yCenter+3);
g.drawString("6",xCenter-3 , yCenter+clockRadius-3);
int sLength = (int) (clockRadius*0.8);
int xSecond = (int)(xCenter+sLength*Math.sin(second*(2*Math.PI/60)));
int ySecond = (int)(yCenter-sLength*Math.cos(second*(2*Math.PI/60)));
g.setColor(Color.RED);
g.drawLine(yCenter, yCenter, xSecond, ySecond);
int mLength = (int) (clockRadius*0.65);
int xMinute = (int)(xCenter+mLength*Math.sin(minute*(2*Math.PI/60)));
int yMinute = (int)(yCenter-mLength*Math.cos(minute*(2*Math.PI/60)));
g.setColor(Color.blue);
g.drawLine(yCenter, yCenter, xMinute, yMinute);
int hLength = (int) (clockRadius*0.5);
int xHour = (int)(xCenter+hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12)));
int yHour = (int)(yCenter-hLength*Math.cos((hour%12+minute/60.0)*(2*Math.PI/12)));
g.setColor(Color.green);
g.drawLine(yCenter, yCenter, xHour, yHour);
}
public void setCurrentTime() {
Calendar calendar = new GregorianCalendar();
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
this.second = calendar.get(Calendar.SECOND);
}
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
repaint();
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
repaint();
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
repaint();
}
}
好了,别的也没有什么了,要不然呢,哈哈,没有什么意思,结束了,好,结束。
不对,还有一件好玩的事情没有说,是这样的,今天,有人评论了我的日记,感觉惊呆了,还对我表示感谢呢,有什么值的借鉴的吗,说出来,我也借鉴一下,哈哈,挺好的,还蛮高兴的,毕竟有人感谢了我,虽然我也不知道哪里有值得借鉴的地方,算了,别的没有什么了,好了,结束 。