IGFrame Ball Test 示例

各位转载请注明出处,谢谢合作~作者:ioozhuangzi

先扔给小示例~小球+连线,话说俺还试过5000个球,效果嘛。。。

package org.ioo.igframe.test;

import java.awt.Color;
import java.awt.Graphics;
/**
 * @(#):Ball.java 
 * @author: ioozhuangzi  2013-2-20 
 * @Copyright:
 */
public class Ball {
	private int size = 5; //小球大小
	private int x, y; //小球左上角坐标
	private int vx, vy; //向量
	private int cx, cy; //小球中心点坐标
	private int ballType = 0;//0为实心球,1为空心球

	public Ball(int x, int y, int vx, int vy) {
		this.x = x;
		this.y = y;
		this.vx = vx;
		this.vy = vy;
		this.cx = (size / 2 + x);
		this.cy = (size / 2 + y);
	}
	
	public Ball(int x, int y, int vx, int vy, int ballType) {
		this.x = x;
		this.y = y;
		this.vx = vx;
		this.vy = vy;
		this.cx = (size / 2 + x);
		this.cy = (size / 2 + y);
		this.ballType = ballType;
	}
	
	public Ball(int x, int y, int vx, int vy, int ballType, int size) {
		this.x = x;
		this.y = y;
		this.vx = vx;
		this.vy = vy;
		this.size = size;
		this.cx = (size / 2 + x);
		this.cy = (size / 2 + y);
		this.ballType = ballType;
	}

	public void move() {
		x += vx;
		y += vy;
		this.cx = (size / 2 + x);
		this.cy = (size / 2 + y);
		if (x < 0 || x > BallTest.WIDTH - size) {
			vx = -vx;
		}
		if (y < 0 || y > BallTest.HEIGHT - size) {
			vy = -vy;
		}
	}

	public void draw(Graphics g) {
		g.setColor(Color.ORANGE);
		if(ballType ==0) {
			g.fillOval(x, y, size, size);
		}
		else if(ballType == 1) {
			g.drawOval(x, y, size, size);
		}
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getVx() {
		return vx;
	}

	public void setVx(int vx) {
		this.vx = vx;
	}

	public int getVy() {
		return vy;
	}

	public void setVy(int vy) {
		this.vy = vy;
	}

	public int getCx() {
		return cx;
	}

	public void setCx(int cx) {
		this.cx = cx;
	}

	public int getCy() {
		return cy;
	}

	public void setCy(int cy) {
		this.cy = cy;
	}

	public int getBallType() {
		return ballType;
	}

	public void setBallType(int ballType) {
		this.ballType = ballType;
	}

}

//基于IGFrame创建窗口的示例

package org.ioo.igframe.test;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Random;

import org.ioo.igframe.core.GFrame;
import org.ioo.igframe.core.GModel;
/**
 * @(#):BallTest.java 
 * @author: ioozhuangzi  2013-2-20 
 * @Copyright:
 */
public class BallTest extends GModel {
	//窗口宽高
	public static final int WIDTH = 480;
	public static final int HEIGHT = 320;
	//小球数目
	private static final int NUM_BALLS = 50;
	//0实心或1空心球
	private int ballType = 0;
	//0无  1连线 2连线并填充
	private int lineFillType = 1;
	//小球大小
	private int size = 5;
	//随机数生成器
	private Random rand;
	//小球数组
	private Ball[] ball;
	//当前小球坐标集合
	int[] x = null;
	int[] y = null;
	
	public void onLoad() {
		rand = new Random(System.currentTimeMillis());//以当前时间(毫秒)作为随机数种子
		ball = new Ball[NUM_BALLS];
		// 初始化小球
		for (int i = 0; i < NUM_BALLS; i++) {
			int x = rand.nextInt(WIDTH);
			int y = rand.nextInt(HEIGHT);
			int vx = rand.nextInt(3)+1;
			int vy = rand.nextInt(3)+1;
			ball[i] = new Ball(x, y, vx, vy, ballType, size);
		}
		x = new int[NUM_BALLS];
		y = new int[NUM_BALLS];
	}

	public void onExit() {
		
	}
	
	public void onUpdate() {
		//更新小球位置
		for (int i = 0; i < NUM_BALLS; i++) {
			ball[i].move();
		}
	}

	public void onRender(Graphics2D g) {
		if(lineFillType == 1 || lineFillType == 2) {
			g.setColor(Color.GREEN);
			//连线
			for (int i = 0; i < NUM_BALLS; i++) {
				if(i == 0) {
					g.drawLine(ball[NUM_BALLS - 1].getCx(), ball[NUM_BALLS - 1].getCy()
							, ball[i].getCx(), ball[i].getCy());
				}
				else {
					g.drawLine(ball[i - 1].getCx(), ball[i - 1].getCy()
							, ball[i].getCx(), ball[i].getCy());
				}
				//获取每个小球的中心点坐标
				x[i] = ball[i].getCx();
				y[i] = ball[i].getCy();
			}
		}
		if(lineFillType == 2) {
			//根据小球坐标集合填充多边形
			g.setColor(Color.GRAY);
			g.fillPolygon(x, y, NUM_BALLS);
		}
		
		// 分别绘制相应球体
		for (int i = 0; i < NUM_BALLS; i++) {
			ball[i].draw(g);
		}
	}

	public static void main(String[] args) {
		GFrame gFrame = new GFrame("IGFrame Ball Test",
				new BallTest(),50, WIDTH, HEIGHT);
		gFrame.setShowFPS(true);
		gFrame.setAlias(true);
		gFrame.setRenderQuality(true);
		gFrame.showFrame();
	}
}

IGFrame的代码还在编写和整理当中,暂不发布~~有爱好的可加QQ群交流~

对Java Sound,Java Video,Java游戏开发等有爱好的各位可以进来交流一下~

想要研究Java(包括Android)文字冒险游戏(AVG/ADV)的人有木有?欢迎交流~

Q群:210816248

验证:IGFrame

猜你喜欢

转载自blog.csdn.net/ioozhuangzi/article/details/8978858
今日推荐