package myGame;
/**
* 测试物体按轨迹移动
*/
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.Thread;
public class Gamefarme2 extends Frame {
Image img = gameUtil.getImage("imges/game.png"); //加载图片
public void launchFrame ()
{
setSize(600,600);
setLocation(100,100);
setVisible(true);
new PainThread().start();//启动重画线程
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private double x,y=100;
private boolean left;
@Override
public void paint(Graphics g) { //加载窗口
super.paint(g);
g.drawImage(img,(int)x,(int) y, null);
if(left){
x-=100;
}
else {
x+=100;
}
if (x>500-30) {
left=true;
}
if(x<0){
left=false;
}
}
class PainThread extends Thread //重画线程
{
public void run() {
while(true){
repaint();
try
{
Thread.sleep(50);
break;
}
catch (Exception e) {
}
}
}
}
public static void main(String[] args) { // 主函数
Gamefarme2 gf= new Gamefarme2();
gf.launchFrame();
}
}
/**
* 测试物体按轨迹移动
*/
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.Thread;
public class Gamefarme2 extends Frame {
Image img = gameUtil.getImage("imges/game.png"); //加载图片
public void launchFrame ()
{
setSize(600,600);
setLocation(100,100);
setVisible(true);
new PainThread().start();//启动重画线程
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private double x,y=100;
private boolean left;
@Override
public void paint(Graphics g) { //加载窗口
super.paint(g);
g.drawImage(img,(int)x,(int) y, null);
if(left){
x-=100;
}
else {
x+=100;
}
if (x>500-30) {
left=true;
}
if(x<0){
left=false;
}
}
class PainThread extends Thread //重画线程
{
public void run() {
while(true){
repaint();
try
{
Thread.sleep(50);
break;
}
catch (Exception e) {
}
}
}
}
public static void main(String[] args) { // 主函数
Gamefarme2 gf= new Gamefarme2();
gf.launchFrame();
}
}