⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 background.java

📁 一个java的免费游戏引擎,里面有文档,和例子
💻 JAVA
字号:
package com.golden.gamedev.object;

import java.awt.*;
import java.io.Serializable;

// Referenced classes of package com.golden.gamedev.object:
//            Sprite
/**
 * Background 把backgroud做为整个游戏世界,所以的sprite都在上面活动;
 * 用setClip(x,y,w,h)可以设置在background上的view区域的大小和位置,如果background大于view,
 * setLocation(double d1, double d2)和move(double d1, double d2)可以移动view的位置
 * 
 */
public class Background implements Serializable {

	// 默认的background大小 screen 640x480;
	public static Dimension screen = new Dimension(640, 480);

	// background----------x坐标
	protected double x;

	// background----------y坐标
	protected double y;

	// background----------宽度
	private int width;

	private int height;

	private final Rectangle rect;

	private static Background background;

	public static Background getDefaultBackground() {
		if (background == null)
			background = new Background();
		return background;
	}

	public Background(int w, int h) {
		x = y = 0.0D;
		width = w;
		height = h;
		rect = new Rectangle(0, 0, screen.width, screen.height);
	}

	public Background() {
		this(screen.width, screen.height);
	}

	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public void setSize(int w, int h) {
		width = w;
		height = h;
		setLocation(x, y);
	}

	public void setLocation(double d1, double d2) {
		if (d1 > (double) (width - rect.width))
			d1 = width - rect.width;
		if (d2 > (double) (height - rect.height))
			d2 = height - rect.height;
		if (d1 < 0.0D)
			d1 = 0.0D;
		if (d2 < 0.0D)
			d2 = 0.0D;
		x = d1;
		y = d2;
	}

	public void move(double d1, double d2) {
		setLocation(x + d1, y + d2);
	}

	public void setToCenter(int x, int y, int w, int h) {
		setLocation((x + w / 2) - rect.width / 2, (y + h / 2) - rect.height / 2);
	}

	public void setToCenter(Sprite sprite) {
		setToCenter((int) sprite.getX(), (int) sprite.getY(),
				sprite.getWidth(), sprite.getHeight());
	}

	public void setClip(int x, int y, int w, int h) {
		rect.setBounds(x, y, w, h);
	}

	public void setClip(Rectangle rectangle) {
		rect.setBounds(rectangle);
	}

	public Rectangle getClip() {
		return rect;
	}

	public void update(long l) {
	}

	public void render(Graphics2D graphics2d) {
		render(graphics2d, (int) x, (int) y, rect.x, rect.y,
				width >= rect.width ? rect.width : width,
				height >= rect.height ? rect.height : height);
	}

	/**				background
	 *		|-------------------------------|
	 *		|	. (xbg,ybg)					|
	 *		|  			(x,y)				|
	 *		|			|---------|			|
	 * 		|			|		  |h		|
	 *		|			|_________|			|
	 *		|_______________w_______________|	
	 * @param g
	 * @param xbg	background的 x---第xbg象素
	 * @param ybg	background的 y---第ybg象素
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	public void render(Graphics2D g, int xbg, int ybg, int x, int y, int w,
			int h) {
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -