2012年6月13日 星期三

Java Game Development(32) - Mouselooks draw Method


import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;

public class Look extends Core implements MouseMotionListener, KeyListener {

public static void main(String[] args) {
new Look().run();
}

private Image bg;
private Robot robot;
private Point mouse; 鼠標的位置
private Point center; 屏幕中心的位置
private Point image; 圖像的位置
private boolean centering;

//init
public void init() {
super.init();
mouse = new Point();
center = new Point();
image = new Point();
centering = false;

try {
robot = new Robot();
recenterMouse();
mouse.x = center.x;
mouse.y = center.y;
} catch (Exception ex) {
System.out.println("Exception 1");
}

Window w = s.getFullScreenWindow();
w.addMouseMotionListener(this);
w.addKeyListener(this);
bg = new ImageIcon("C://baby.png").getImage();
}

//繪製方法
public synchronized void draw(Graphics2D g) {
int w = s.getWidth();
int h = s.getHeight();

image.x %= w;
image.y %= h;

if (image.x < 0) {
image.x += w;
}
if (image.y < 0) {
image.y += h;
}
int x = image.x;
int y = image.y;
g.drawImage(bg, x, y, null);
g.drawImage(bg, x-w, y, null);
g.drawImage(bg, x, y-h, null);
g.drawImage(bg, x-w, y-h, null);
}

}

沒有留言:

張貼留言