一个星星落下

首先是主类:

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);
		
		
	}
	
}

效果图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值