我正在尝试编写一个sprite类,但是它不只是在一个类中,我创建了一个完整的工作类并且只声明了一些变量(这是我想要实现的。),但是相反......一个半完成的课程,有一个程序,我想"插入"那个程序,或者里面的东西。
这是半完成类的代码(已添加变量):
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);
}
//----------------------------------------------------------------------
}
}
现在这里是程序内部的内容:
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();
}
}