你这个x,y是在方法内部定义的一个变量 没有赋值就使用当然编译不通过咯
站在用户的角度思考问题,与客户深入沟通,找到富县网站设计与富县网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:做网站、网站制作、企业官网、英文网站、手机端网站、网站推广、域名注册、网站空间、企业邮箱。业务覆盖富县地区。
要想使用必须要先实例化。、
代码具体要怎么写 要看你的业务逻辑!
代码主要为以下:
package com.lovo.game.frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import javax.swing.JFrame;
import com.lovo.game.role.Fire;
import com.lovo.game.role.GameMap;
import com.lovo.game.role.ZhaoYun;
import com.lovo.game.util.TrackerInit;
public class GameStartFrame extends JFrame implements Runnable{
private Image memoryImage;
private Graphics memoryGraphics;
public static boolean isRun = true;
private static GameMap gameMap = new GameMap();
private ZhaoYun zy = new ZhaoYun();
private Fire fire = new Fire();
public GameStartFrame(){
this.setSize(900,700);
this.setVisible(true);
this.setDefaultCloseOperation(3);
//设置居中
this.setLocationRelativeTo(null);
//初始化双缓冲画布、画笔
this.memoryImage = this.createImage(900,700);
this.memoryGraphics = this.memoryImage.getGraphics();
//媒体追踪器
MediaTracker tracker = new MediaTracker(this);
TrackerInit.initImage(tracker);
//启动线程
Thread th = new Thread(this);
th.start();
}
public static void main(String[] args) {
GameStartFrame game = new GameStartFrame();
}
public void run() {
while(isRun){
this.flushFrame();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void flushFrame(){
gameMap.drawMap(memoryGraphics);
zy.drawImage(memoryGraphics);
fire.drawImage(memoryGraphics);
//将双缓冲画布中的图片显示在窗体
this.getGraphics().drawImage(this.memoryImage,0,0,this);
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.lovo.game.role;
import java.awt.Graphics;
import java.awt.Image;
public class GameMap {
public static Image mapImg;
public static int mapx;
public void drawMap(Graphics memoryGraphics){
mapx -=5;
if(mapx =-17100){
mapx = -17100;
}
memoryGraphics.drawImage(mapImg,mapx,0,null);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.lovo.game.role;
import java.awt.Graphics;
import java.awt.Image;
import com.lovo.game.util.MoveImageChange;
public class Fire {
public static Image[]fireImage;
private int x =100;
private int y =300;
private int width = 200;
private int height = 130;
private MoveImageChange moveChange = new MoveImageChange(3);
public void drawImage(Graphics memoryGraphics){
Image img = moveChange.imageChange(fireImage);
memoryGraphics.drawImage(img,this.x,this.y,this.width,this.height,null);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.lovo.game.util;
import java.awt.MediaTracker;
import com.lovo.game.role.Fire;
import com.lovo.game.role.GameMap;
import com.lovo.game.role.ZhaoYun;
public class TrackerInit {
public static void initImage(MediaTracker tracker){
//初始化地图图片
GameMap.mapImg = CutImage.getSingleImage("image/map.jpg", tracker);
//赵云
ZhaoYun.zyImage = CutImage.cutOneImage("image/boss/playSpear.png",18, 236, 134,tracker);
//火
Fire.fireImage = CutImage.cutOneImage("image/fireImg.png", 6, 283, 161,tracker);
try {
//0组的图片全部等待加载完毕后,在显示
tracker.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.lovo.game.util;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class CutImage {
public static Image[][] cutManyImage(String filePath, int row, int col,
int imageWidth, int imageHight,MediaTracker tracker) {
Image[][] img = new Image[row][col];
ImageIcon imIcon = new ImageIcon(filePath);// 创建图像数组对象
Image imgTemp = imIcon.getImage();// 创建源图像
// 为源 图象获取ImageProducer源
ImageProducer sourceProducer = imgTemp.getSource();
for (int i = 0; i row; i++) {
for (int j = 0; j col; j++) {
// 创建图片分割图像对象,第一、二个参数为分割图像起始坐标。后两个参数为图像大小
CropImageFilter cropImg = new CropImageFilter(j * imageWidth, i * imageHight,imageWidth, imageHight);
ImageProducer imgProducer = new FilteredImageSource(sourceProducer, cropImg);
img[i][j] = new JFrame().createImage(imgProducer);
tracker.addImage(img[i][j], 0);
}
}
return img;
}
public static Image[] cutOneImage(String filePath,int col,
int imageWidth, int imageHight,MediaTracker tracker) {
Image[] img = new Image[col];
ImageIcon imIcon = new ImageIcon(filePath);// 创建图像数组对象
Image imgTemp = imIcon.getImage();// 创建源图像
// 为源 图象获取ImageProducer源
ImageProducer sourceProducer = imgTemp.getSource();
for (int j = 0; j col; j++) {
// 创建图片分割图像对象,第一、二个参数为分割图像起始坐标。后两个参数为图像大小
CropImageFilter cropImg = new CropImageFilter(j * imageWidth, 0,imageWidth, imageHight);
ImageProducer imgProducer = new FilteredImageSource(sourceProducer, cropImg);
img[j] = new JFrame().createImage(imgProducer);
tracker.addImage(img[j], 0);
}
return img;
}
public static Image getSingleImage(String imgPath,MediaTracker tracker){
Image img = new ImageIcon(imgPath).getImage();
tracker.addImage(img, 0);
return img;
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.lovo.game.util;
import java.awt.Image;
public class MoveImageChange {
private int count;
private Image img;
private int frequency;
private int loopCount;
public MoveImageChange(int frequency){
this.frequency=frequency;
}
public MoveImageChange(int frequency,int count){
this.frequency=frequency;
this.count = count;
}
public Image imageChange(Image[] images){
if(img==null){//初始图片为第一张图片
img=images[0];
}
count++;
if(countfrequency){//当记数器大于频率时
count=0;
loopCount++;
if(loopCount = images.length){//图片下标越界时
loopCount=0;
}
img=images[loopCount];
}
return img;
}
public void setLoopCount(int loopCount){
this.loopCount = loopCount;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.lovo.game.role;
import java.awt.Graphics;
import java.awt.Image;
import com.lovo.game.util.MoveImageChange;
public class ZhaoYun {
public static Image[] zyImage;
private int x =600;
private int y =300;
private int width =200;
private int height =130;
private MoveImageChange moveChange = new MoveImageChange(3);
public void drawImage(Graphics memoryGraphics){
Image img = moveChange.imageChange(zyImage);
memoryGraphics.drawImage(img,this.x,this.y,this.width,this.height,null);
写两个程序分别模拟红绿灯和汽车:
1)红绿灯程序以报文形式通知汽车程序;
2)汽车程序需要用多线程来实现。
3)红绿灯程序用个循环,每隔n分钟红绿灯转换,同时通知汽车程序;