eblly的gravatar头像
eblly 2014-07-30 21:14:38

java AWT入门教程之移動的小球实例代码下载

package com.zuidaima.awt;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
*@author www.zuidaima.com/
**/
public class Game extends JPanel {
	private int posx = 0;
	private int posy = 0;

	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.setColor(Color.RED);
		g2d.fillOval(posx, posy, 50, 50);
		posx += 1;
		posy += 1;
	}

	public static void main(String[] args) throws InterruptedException {
		JFrame jframe = new JFrame();
		jframe.setTitle("test");
		Game game = new Game();
		jframe.add(game);
		jframe.setSize(300, 300);
		jframe.setVisible(true);
		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		while (true) {
			game.repaint();
			Thread.sleep(10);
		}
	}
}

java AWT入门教程之移動的小球实例代码下载

然後設斷點,進行調試。

java AWT入门教程之移動的小球实例代码下载

paint(Graphics g)是由AWT線程自動運行的。可以看到右邊有Thread[AWT-EventQueue-0]這個線程。

第一個斷點super.paint(g);用戶重新刷新graphics。

第二個斷點game.repaint()是用來告訴AWT線程再次執行paint();

官方验证:

很简单的java awt的实例,大可以放心下载


最代码官方编辑于2014-7-30 22:19:05


打赏

已有1人打赏

cengxiangchao的gravatar头像

最代码最近下载分享源代码列表最近下载
最代码最近浏览分享源代码列表最近浏览
yangxb2  LV10 2023年7月11日
微信网友_6477647054655488  LV2 2023年5月23日
cjjjyy  LV1 2023年3月3日
我就是徊瑕哦 2022年12月20日
暂无贡献等级
xfx冲冲冲  LV4 2022年5月2日
admin528783  LV1 2021年5月31日
jrh947869206  LV4 2020年12月25日
10070116 2020年12月7日
暂无贡献等级
地上有草  LV12 2020年12月7日
what_whnt  LV1 2020年6月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友