线程中的.setLocation没有在单独的对象中调用repaint(java)

时间:2018-02-21 19:46:52

标签: java swing awt

- 研究问题的一个版本 -

所以,我可以在网上找到那么多信息。

我和朋友一起制作游戏,当我们想要在线程中移动一些对象时,我们遇到了一个问题。我们有一个对象GroundBlocks,它正常工作以及它应该如何工作。   然后我们有另一个对象Obstacles,它使用相同的"方法"印刷和加工但没有出现。

我自己做了一些故障排除,我注意到paintComponent中的Obstacles阻止我们在线程中添加的.setLocation没有被调用。 正如我所提到的,我在搜索自己时并没有找到很多在线搜索,但通过测试,我知道这些数字是正确的,但它只是没有显示。

关于.setLocation无法解决问题的任何想法?

- 线程类的相关代码 -

// Creating objects
public static GroundBlocks[] ground = {new GroundBlocks(), new GroundBlocks(), new GroundBlocks()};
public static Obstacles[] obstacle = {new Obstacles(), new Obstacles(), new Obstacles(), new Obstacles(), new Obstacles()};

// Creating block dimensions
final int GROUND_HEIGHT = resY - GroundBlocks.getGroundHeight();
final int GROUND_WIDTH = ground[1].getGroundWidth();
int[] groundX = {0, GROUND_WIDTH, GROUND_WIDTH*2};

final int OBSTACLE_HEIGHT = obstacle[0].getHeight();
final int OBSTACLE_WIDTH = obstacle[0].getWidth();
int[] obstacleX = {newCoord(0), newCoord(0), newCoord(0), newCoord(0), newCoord(0)};
int[] obstacleY = {newCoord(1), newCoord(1), newCoord(1), newCoord(1), newCoord(1)};

// Setting game animation movement
final int MOVEMENT_SPEED = 7;

// Setting timer object with preferred FPS
Timer time = new Timer(Init.getFps(0), this);


public void run() {
    System.out.println("A new MainThread has been initiated");
    time.start();

    // Setting ground block size, starting location and image
    for(int i = 0; i < ground.length; i++) {
        ground[i].setLocation(groundX[i], GROUND_HEIGHT);
        ground[i].setSize(GROUND_WIDTH, GROUND_HEIGHT);
        ground[i].setGroundImage(pickRandomGroundImage());
    }

    // Setting background image for obstacles
    for (int i = 0; i < obstacle.length; i++) {
        obstacle[i].setSize(OBSTACLE_WIDTH, OBSTACLE_HEIGHT);
        obstacle[i].setLocation(obstacleX[i], obstacleY[i]);
        obstacle[i].setObstacleImage(img5);
//          System.out.println(obstacle[i].getLocation());
    }
}

@Override
public void actionPerformed(ActionEvent arg0) {
    for(int i = 0; i < ground.length; i++) {
        // Respawning ground block
        if(groundX[i] <= -resX) {
            groundX[i] = GROUND_WIDTH*2 - MOVEMENT_SPEED;
            ground[i].setGroundImage(pickRandomGroundImage());
        }

        // Animating ground block
        ground[i].setLocation(groundX[i] -= MOVEMENT_SPEED, GROUND_HEIGHT);
    }

    for (int i = 0; i < obstacle.length; i++) {
        // Resetting obstacle block
        if ((obstacleX[i] + OBSTACLE_WIDTH) <= -10) {
            obstacleX[i] = newCoord(0);
            obstacleY[i] = newCoord(1);
            obstacle[i].setObstacleImage(img5);
        }

        // Animating obstacle block
        obstacle[i].setLocation(obstacleX[i] -= MOVEMENT_SPEED, obstacleY[i]);
        if (i == 0) {
            System.out.println(obstacle[i].getLocation());
        }
    }
}

- GroundBlocks类 -

public class GroundBlocks extends JPanel{

// Declarations
static int resX = Main._init.getResX();
static int resY = Main._init.getResY();
static String font = Main._init.getOurFont();
private static int groundWidth;
private static int groundHeight;
private Image chosenImage;

public GroundBlocks() {
    System.out.println("GroundBlock created");
    setScaleIndex();
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    //Draw image background
    g.drawImage(chosenImage, 0, 0, this);
}

// Applying the scaleIndex to the ground objects X and Y dimensions.
// Size changes can be made manually in Init.
public void setScaleIndex() {
    groundWidth = (int) (Init.getGroundSize(0) * Init.getScaleIndex());
    groundHeight = (int) (Init.getGroundSize(1) * Init.getScaleIndex());
}

public int getGroundWidth() {
    return groundWidth;
}

public static int getGroundHeight() {
    return groundHeight;
}

public void setGroundImage(Image image) {
    chosenImage = image;
}
}

- 障碍代码 -

public class Obstacles extends JPanel{

    // Declarations
    static int resX = Main._init.getResX();
    static int resY = Main._init.getResY();
    static String font = Main._init.getOurFont();
    private static int obstacleHeight;
    private static int obstacleWidth;
    private Image chosenImage;

    public Obstacles() {
        System.out.println("New obstacle object created!");
        setScaleIndex();
        repaint();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.println("Printed obstacle");
        g.drawImage(chosenImage, 0, 0, this);
    }

    // Applying the scaleIndex to the ground objects X and Y dimensions.
    // Size changes can be made manually in Init.
    public void setScaleIndex() {
        obstacleWidth = (int) (Init.getObstacleSize(0) * Init.getScaleIndex());
        obstacleHeight = (int) (Init.getObstacleSize(1) * Init.getScaleIndex());
    }

    public int getObstacleHeight() {
        return obstacleHeight;
    }

    public int getObstacleWidth() {
        return obstacleWidth;
    }

    public void setObstacleImage(Image image) {
        chosenImage = image;
    }

}

0 个答案:

没有答案