问题描述
要做个小游戏背景打不出来,直接用staticvalue里的变量很容易实现,但是把图片新建一个background类就不行了用system.out.println输出图像,staticvalue有反应,但是在background里,bufferedimage没附上值。而且在background里没有staticvalue的值,很是诡异!!!大哥大姐帮帮忙看看吧!!!小弟甘做牛马!代码如下MyFrame.javapackagetest.ft;importjava.awt.Graphics;importjava.awt.Toolkit;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;importjava.util.Iterator;importjavax.imageio.ImageIO;importjavax.swing.JFrame;publicclassMyFrameextendsJFrame{privatebackgroundnowBG=newbackground();publicstaticvoidmain(Stringarg[]){newMyFrame();}publicMyFrame(){this.setTitle("Game");this.setSize(900,600);intwidth=Toolkit.getDefaultToolkit().getScreenSize().width;intheight=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();}publicvoidpaint(Graphicsg){//TODOAuto-generatedmethodstubBufferedImageimage=newBufferedImage(900,600,BufferedImage.TYPE_3BYTE_BGR);Graphicsg2=image.getGraphics();//BufferedImageimage2=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()){landl=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.javapackagetest.ft;importtest.ft.StaticValue.*;importjava.awt.image.BufferedImage;importjava.util.ArrayList;importjava.util.List;publicclassbackground{privateBufferedImagebackgroundImage=null;privateList<land>allLand=newArrayList<land>();/***@paramallLandtheallLandtoset*/publicvoidsetAllLand(List<land>allLand){this.allLand=allLand;}publicList<land>getAllLand(){returnallLand;}publicbackground(){backgroundImage=StaticValue.bgImage;System.out.println(StaticValue.bgImage);//System.out.println(backgroundImage);for(inti=0;i<15;i++){this.allLand.add(newland(i*60,540));}}/***@returnthebgImage*/publicBufferedImagegetBgImage(){returnbackgroundImage;}/***@parambgImagethebgImagetoset*/publicvoidsetBgImage(BufferedImagebgImage){this.backgroundImage=bgImage;}}StaticValue.javapackagetest.ft;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;importjavax.imageio.ImageIO;publicclassStaticValue{publicstaticBufferedImagebgImage=null;publicstaticBufferedImagelandImage=null;publicstaticStringimagePath=System.getProperty("user.dir")+"/bin/";publicstaticvoidinit(){try{bgImage=ImageIO.read(newFile(imagePath+"sky.gif"));//System.out.println(bgImage);landImage=ImageIO.read(newFile(imagePath+"land.gif"));}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}
解决方案
解决方案二:
统一规划将规划将缓解