Java Sprite自定义类不能正常工作,为什么?

Java Sprite自定义类不能正常工作,为什么?,java,logic,sprite,Java,Logic,Sprite,正如您在代码中所看到的,我试图创建一个自定义的sprite类,但是由于某种原因,变量的值“消失”,如果(正如您将看到的)isSprite为true,那么即使大型机也不会出现/不可见,它将在后台运行,直到您手动停止它 下面是该类的代码: package testowhat; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import ja

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

下面是该类的代码:

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();
    }
   }
}
包testowhat;
导入java.awt.Component;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.awt.Rectangle;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入java.util.Objects;
导入javax.imageio.imageio;
导入javax.swing.JPanel;
公共类Sprite扩展组件{
精灵(字符串图像1)抛出IOException、InterruptedException{
playerConstructor(图1);
}
整数imgPosX;整数imgPosY;
整数divideSize2;整数divideSize1;
整数高度;整数宽度;
SpriteCells整数、SpriteCells数、CellsInAline数、Cells高度、Cells宽度、countingNumber1、countingNumber2;
缓冲图像图片;
布尔isSprite;
字符串图像;
JPanel picturePanel=新的JPanel(){
公共空间涂料(图g){
g、 drawImage(picture.getScaledInstance(picture.getWidth()/divideSize1,picture.getHeight()/divideSize2,Image.SCALE\u默认值),imgPosX,imgPosY,null);
}     
};
私有void playerConstructor(字符串图像)引发IOException、InterruptedException{
试一试{
picture=ImageIO.read(新文件(图像));
}catch(IOException ex){System.out.println(“文件丢失”);}
//----------------------------------------------------------------------
高度=picture.getHeight()+10;
宽度=picture.getWidth()+10;
//----------------------------------------------------------------------
picturePanel.setLocation(this.getLocation());
picturePanel.setSize(this.getHeight(),this.getWidth());
//----------------------------------------------------------------------
picturePanel.setMinimumSize(新尺寸(高度、宽度));
picturePanel.setMaximumSize(新尺寸(高度、宽度));
//----------------------------------------------------------------------
if(this.isVisible()==true){
picturePanel.setVisible(真);
}
//----------------------------------------------------------------------
此.countingNumber1=0;
检查它们();
justDoIt();
}
//--------------------------------------------------------------------------
私人无效检查他们(){
if(isSprite==null){
isSprite=false;
}
if(imgPosX==null){
imgPosX=0;
imgPosY=0;
}
if(imgPosY==null){
imgPosX=0;
imgPosY=0;
}
if(numberOfSpriteCells==null){
SpriteCell数=6;
}
if(单元格宽度==null){
细胞宽度=103;
}
if(单元高度==null){
细胞高度=89;
}
if(numberOfSpriteLayers==null){
Spritelayers的数量=2;
}
}
//--------------------------------------------------------------------------
private void justDoIt()引发InterruptedException{
如果(isSprite==true){

对于(countingNumber2=1;countingNumber2仅调试有帮助我对编程非常陌生,你建议我怎么做?再次,调试值…使用NetBeansH附带的调试器,好的,谢谢!只有调试才有帮助我对编程非常陌生,你建议我怎么做?再次,用netbeansOh附带的调试器,好的,谢谢!