你這個(gè)x,y是在方法內(nèi)部定義的一個(gè)變量 沒有賦值就使用當(dāng)然編譯不通過咯
站在用戶的角度思考問題,與客戶深入溝通,找到富縣網(wǎng)站設(shè)計(jì)與富縣網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋富縣地區(qū)。
要想使用必須要先實(shí)例化。、
代碼具體要怎么寫 要看你的業(yè)務(wù)邏輯!
代碼主要為以下:
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);
//設(shè)置居中
this.setLocationRelativeTo(null);
//初始化雙緩沖畫布、畫筆
this.memoryImage = this.createImage(900,700);
this.memoryGraphics = this.memoryImage.getGraphics();
//媒體追蹤器
MediaTracker tracker = new MediaTracker(this);
TrackerInit.initImage(tracker);
//啟動(dòng)線程
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);// 創(chuàng)建圖像數(shù)組對象
Image imgTemp = imIcon.getImage();// 創(chuàng)建源圖像
// 為源 圖象獲取ImageProducer源
ImageProducer sourceProducer = imgTemp.getSource();
for (int i = 0; i row; i++) {
for (int j = 0; j col; j++) {
// 創(chuàng)建圖片分割圖像對象,第一、二個(gè)參數(shù)為分割圖像起始坐標(biāo)。后兩個(gè)參數(shù)為圖像大小
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);// 創(chuàng)建圖像數(shù)組對象
Image imgTemp = imIcon.getImage();// 創(chuàng)建源圖像
// 為源 圖象獲取ImageProducer源
ImageProducer sourceProducer = imgTemp.getSource();
for (int j = 0; j col; j++) {
// 創(chuàng)建圖片分割圖像對象,第一、二個(gè)參數(shù)為分割圖像起始坐標(biāo)。后兩個(gè)參數(shù)為圖像大小
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){//當(dāng)記數(shù)器大于頻率時(shí)
count=0;
loopCount++;
if(loopCount = images.length){//圖片下標(biāo)越界時(shí)
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);
寫兩個(gè)程序分別模擬紅綠燈和汽車:
1)紅綠燈程序以報(bào)文形式通知汽車程序;
2)汽車程序需要用多線程來實(shí)現(xiàn)。
3)紅綠燈程序用個(gè)循環(huán),每隔n分鐘紅綠燈轉(zhuǎn)換,同時(shí)通知汽車程序;