首先是主类:
import javax.swing.JFrame;
public class OneStarDropJFrame extends JFrame {//jframe窗体
//一颗星落下来
public OneStarDropJFrame(){
this.setTitle("他妈是个傻子吧");//标题
this.setBounds(Factory.jframeX, Factory.jframeY, Factory.jfwidth, Factory.jfheight);//边界
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认的关闭操作,参数”在关闭动作时退出
this.setVisible(true);//可以开始画图了
this.add(new OneStarDropJPramel());//jpamel图形用户界面
this.setVisible(true);
}
public static void main(String[] args) {
new OneStarDropJFrame();
}
}
然后是工厂类(设置属性):
import java.awt.Toolkit;
public class Factory {
//窗体的高宽
public static int jfwidth=600;
public static int jfheight=900;
//屏幕的高宽
public static int width=Toolkit.getDefaultToolkit().getScreenSize().width;
public static int height=Toolkit.getDefaultToolkit().getScreenSize().height;
//水平宽高
public static int jframeX=width/2-jfheight/2;
public static int jframeY=height/2-jfwidth/2;
}
最后是星星落下的,类:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;
public class OneStarDropJPramel extends JPanel implements Runnable {
int y;//坐标
Thread t;
public OneStarDropJPramel() {
this.setBackground(Color.red);
t = new Thread(this);//线程开始
t.start();
}
@Override
public void run() {
while (true) {
y++;
if (y >= Factory.jfheight ) {
y = 0;
}
repaint();//重新执行paint
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.setColor(Color.yellow);
g.setFont(new Font("宋体",Font.BOLD,45));
g.drawString("*", 100, y);
}
}
效果图: