要做个小游戏背景打不出来,直接用staticvalue里的变量很容易实现,但是把图片新建一个background类就不行了用system.out.println输出图像,staticvalue有反应,但是在background里,bufferedimage没附上值。而且在background里没有staticvalue的值,很是诡异!!!大哥大姐帮帮忙看看吧!!!小弟甘做牛马!代码如下MyFrame.javapackage test.ft;import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;import javax.imageio.ImageIO;
import javax.swing.JFrame;public class MyFrame extends JFrame{
private background nowBG=new background();
public static void main(String arg[]){
new MyFrame();
}
public MyFrame(){
this.setTitle("Game");
this.setSize(900,600);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation((width-900)/2,(height-600)/2);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
StaticValue.init();
}
public void paint(Graphics g) {
// TODO Auto-generated method stub
BufferedImage image=new BufferedImage(900,600,BufferedImage.TYPE_3BYTE_BGR);
Graphics g2=image.getGraphics();
//BufferedImage image2=this.nowBG.getBgImage();
//g2.drawImage(StaticValue.bgImage,0,0,this);
//g2.drawImage(this.nowBG.getBgImage(),0,0,this);
//land
//System.out.println(this.nowBG.bgImage);
//System.out.println(nowBG.getBgImage());
/*
Iterator<land> iter=this.nowBG.getAllLand().iterator();
while(iter.hasNext()){
land l=iter.next();
g2.drawImage(l.getShowImage(), l.getX(), l.getY(), this);
}
*/
g.drawImage(image,0,0,this);
//g.drawImage(StaticValue.startImage,0,0,this);
}
}background.javapackage test.ft;
import test.ft.StaticValue.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;public class background {
private BufferedImage backgroundImage=null;
private List<land> allLand =new ArrayList<land>();
/**
* @param allLand the allLand to set
*/
public void setAllLand(List<land> allLand) {
this.allLand = allLand;
}
public List<land> getAllLand(){
return allLand;
}
public background(){
backgroundImage=StaticValue.bgImage;
System.out.println(StaticValue.bgImage);
//System.out.println(backgroundImage);
for(int i=0;i<15;i++){
this.allLand.add(new land(i*60,540));
}
}
/**
* @return the bgImage
*/
public BufferedImage getBgImage() {
return backgroundImage;
} /**
* @param bgImage the bgImage to set
*/
public void setBgImage(BufferedImage bgImage) {
this.backgroundImage = bgImage;
}
}StaticValue.javapackage test.ft;import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;public class StaticValue {
public static BufferedImage bgImage = null;
public static BufferedImage landImage=null;
public static String imagePath=System.getProperty("user.dir")+"/bin/";
public static void init(){
try {
bgImage=ImageIO.read(new File(imagePath+"sky.gif"));
//System.out.println(bgImage);
landImage=ImageIO.read(new File(imagePath+"land.gif"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;import javax.imageio.ImageIO;
import javax.swing.JFrame;public class MyFrame extends JFrame{
private background nowBG=new background();
public static void main(String arg[]){
new MyFrame();
}
public MyFrame(){
this.setTitle("Game");
this.setSize(900,600);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation((width-900)/2,(height-600)/2);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
StaticValue.init();
}
public void paint(Graphics g) {
// TODO Auto-generated method stub
BufferedImage image=new BufferedImage(900,600,BufferedImage.TYPE_3BYTE_BGR);
Graphics g2=image.getGraphics();
//BufferedImage image2=this.nowBG.getBgImage();
//g2.drawImage(StaticValue.bgImage,0,0,this);
//g2.drawImage(this.nowBG.getBgImage(),0,0,this);
//land
//System.out.println(this.nowBG.bgImage);
//System.out.println(nowBG.getBgImage());
/*
Iterator<land> iter=this.nowBG.getAllLand().iterator();
while(iter.hasNext()){
land l=iter.next();
g2.drawImage(l.getShowImage(), l.getX(), l.getY(), this);
}
*/
g.drawImage(image,0,0,this);
//g.drawImage(StaticValue.startImage,0,0,this);
}
}background.javapackage test.ft;
import test.ft.StaticValue.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;public class background {
private BufferedImage backgroundImage=null;
private List<land> allLand =new ArrayList<land>();
/**
* @param allLand the allLand to set
*/
public void setAllLand(List<land> allLand) {
this.allLand = allLand;
}
public List<land> getAllLand(){
return allLand;
}
public background(){
backgroundImage=StaticValue.bgImage;
System.out.println(StaticValue.bgImage);
//System.out.println(backgroundImage);
for(int i=0;i<15;i++){
this.allLand.add(new land(i*60,540));
}
}
/**
* @return the bgImage
*/
public BufferedImage getBgImage() {
return backgroundImage;
} /**
* @param bgImage the bgImage to set
*/
public void setBgImage(BufferedImage bgImage) {
this.backgroundImage = bgImage;
}
}StaticValue.javapackage test.ft;import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;public class StaticValue {
public static BufferedImage bgImage = null;
public static BufferedImage landImage=null;
public static String imagePath=System.getProperty("user.dir")+"/bin/";
public static void init(){
try {
bgImage=ImageIO.read(new File(imagePath+"sky.gif"));
//System.out.println(bgImage);
landImage=ImageIO.read(new File(imagePath+"land.gif"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货