精灵自定义类无法正常工作,为什么?

时间:2015-12-18 16:47:59

标签: java logic sprite

正如您将在代码中看到的那样,我尝试创建一个自定义精灵类,但由于某种原因,变量的值“消失”,如果(如您所见)isSprite是是的,那么即使大型机也不会出现/不可见,它只会在后台运行,直到你手动停止它为止。

以下是该类的代码:

package testowhat;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Sprite extends Component {

Sprite(String image1) throws IOException, InterruptedException{
 playerConstructor(image1); 

}

Integer imgPosX;        Integer imgPosY;
Integer divideSize2;    Integer divideSize1;
Integer Height;         Integer Width;
Integer numberOfSpriteCells, numberOfSpriteLayers, numberOfCellsInALine, heightOfCells, widthOfCells, countingNumber1, countingNumber2;
BufferedImage picture;
Boolean isSprite;
String image;
JPanel picturePanel = new JPanel() {    
        public void paint(Graphics g) {
            g.drawImage(picture.getScaledInstance(picture.getWidth()/divideSize1, picture.getHeight()/divideSize2, Image.SCALE_DEFAULT), imgPosX, imgPosY, null);
        }     
};

private void playerConstructor(String image) throws IOException, InterruptedException {
    try {
        picture = ImageIO.read(new File(image));
    } catch (IOException ex) {System.out.println("File is missing.");}
    //----------------------------------------------------------------------
    Height = picture.getHeight()+10;
    Width = picture.getWidth()+10;
    //----------------------------------------------------------------------
     picturePanel.setLocation(this.getLocation());
     picturePanel.setSize(this.getHeight(), this.getWidth());
    //----------------------------------------------------------------------
    picturePanel.setMinimumSize(new Dimension(Height, Width));
    picturePanel.setMaximumSize(new Dimension(Height, Width));
    //----------------------------------------------------------------------
    if (this.isVisible() == true) {
      picturePanel.setVisible(true);  
    }
    //----------------------------------------------------------------------
    this.countingNumber1 = 0;
    checkThem();
    justDoIt();

}
//--------------------------------------------------------------------------
private void checkThem() {
    if (isSprite == null) {
        isSprite = false;
    }
    if (imgPosX == null) {
      imgPosX = 0;
      imgPosY = 0;
    }
    if (imgPosY == null) {
      imgPosX = 0;
      imgPosY = 0;
    }
    if (numberOfSpriteCells == null) {
        numberOfSpriteCells = 6;
    }
    if (widthOfCells == null) {
        widthOfCells = 103;
    }
    if (heightOfCells == null) {
        heightOfCells = 89;
    }
    if (numberOfSpriteLayers == null) {
        numberOfSpriteLayers = 2;
    }
}
//--------------------------------------------------------------------------
private void justDoIt() throws InterruptedException {
    if (isSprite == true) {
    for (countingNumber2=1; countingNumber2<7; countingNumber2++) {
      doChange();
      Thread.sleep(100);
      this.repaint();
      this.picturePanel.repaint();
      if (countingNumber2 <= 7) {
          countingNumber2 = 1;
      }
    } 
 }
}

//--------------------------------------------------------------------------

private void doChange() {
    imgPosX = imgPosX - widthOfCells;
    countingNumber1 = countingNumber1++;
    repaint();
    picturePanel.repaint();
    if (countingNumber1==numberOfSpriteCells/numberOfSpriteLayers) {
        imgPosY = imgPosY - heightOfCells;
        imgPosX = 0;
    }
    if (countingNumber1 == numberOfSpriteCells) {
        countingNumber1 = 0;
        imgPosX = 0;
        imgPosY = 0;
        repaint();
        picturePanel.repaint();
    }
   }
}

作为旁注:

  

予。在doChange()过程所在的代码的第一个“版本”中   另一堂课。

     

II。图片的setboundspicturepanel被声明   另一个班级,但已连接。

     

III。出于某种原因,大多数设定变量似乎都失去了它们   值

     

IV。我已经遇到了nullPointerException插入问题的问题   doChange()程序,所以我猜这可能就是那个   导致这些问题。

运行程序的结果:

如果isSprite设置为false,则:enter image description here

它运行平稳并显示我们想要的图片部分。

如果设置为true:enter image description here

没有任何事情发生,它会运行,但什么都不会出现,它只是无法正常工作。

0 个答案:

没有答案