?? background.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 + -