0

I am trying to write a sprite class, however it isn't in just one class, where I made a whole working class and just declared a few variables (this is what I am trying to achieve.), but rather... a half-done class, with a procedure, and I would like to "insert" that procedure, or what is inside.

Here is the code of the half-done class (with variables already added):

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 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 player;
String image;
JPanel playerPanel = new JPanel() {    
        public void paint(Graphics g) {
            g.drawImage(player.getScaledInstance(player.getWidth()/divideSize1, player.getHeight()/divideSize2, Image.SCALE_DEFAULT), imgPosX, imgPosY, null);
        }     
};

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

And now here is what is inside the procedure:

     for (number2=1; number2<7; number2++) {
      doChange();
      Thread.sleep(100);
      sprite.repaint();
      sprite.playerPanel.repaint();
      if (number2 <= 7) {
          number2 = 1;
      }
  }

  .....

  //--------------------------------------------------------------------------
private void doChange() {
    sprite.imgPosX = sprite.imgPosX - 103;
    number = number + 1;
    sprite.repaint();
    sprite.playerPanel.repaint();
    if (number==3) {
        sprite.imgPosY = sprite.imgPosY - 89;
        sprite.imgPosX = 0;
    }
    if (number==6) {
        number = 0;
        sprite.imgPosX = 0;
        sprite.imgPosY = 0;
        sprite.repaint();
        sprite.playerPanel.repaint();
    }
}
Diligent Key Presser
  • 4,183
  • 4
  • 26
  • 34
Coldus12
  • 27
  • 6
  • You have so much useless extra chitchat in your question that it's hard to understand your question. How about removing all that useless disclaimer and grammatical mistake blabber and we'll have another look? – Kayaman Dec 11 '15 at 19:39
  • There: Removed any useless text – Coldus12 Dec 11 '15 at 22:10
  • So what's the problem? Your `doChange()` is already a full blown method, so you can move it inside your `Sprite` class (just remove the `sprite.` prefix from every line since you'll work on `this`. Where's the method declaration for the first half of the code? You also shouldn't call `sprite.playerPanel.repaint();`. The `sprite.repaint();` should take care of any child component repaints. – Kayaman Dec 11 '15 at 22:17
  • I didn't know that about the .repaint(), thanks, and my problem is, that when i moved it inside, then even if i declared the imgPosX,imgPosY there was java.null.exception. It is probably a logical issue, but i don't know what exactly it is.. – Coldus12 Dec 11 '15 at 22:21
  • 1
    When you get an exception, you investigate the cause and solve it. That's how the programming process works. – Kayaman Dec 11 '15 at 22:22
  • Any suggestion how should i investigate it? – Coldus12 Dec 11 '15 at 22:24
  • By looking at the stacktrace and paying attention. It'll tell you which line the exception happened, and if you don't know what a `java.lang.NullPointerException` is, now's a great time to learn. Also don't expect this to be a quick fix or me (or someone else) to just give you the solution. – Kayaman Dec 11 '15 at 22:27
  • Don't get me wrong, i know i will have to investigate it, i know the line, i see the exception, i just have no actual clue why it is there, the oracle doc says that my line "is pointing to nowhere". I just hoped that you, or someone would explain how to avoid/repair these kind of errors. – Coldus12 Dec 11 '15 at 22:30
  • [What is a NullPointerException and how do I fix it](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Kayaman Dec 11 '15 at 22:32

0 Answers0