字符串不会绘制

时间:2014-01-06 10:08:43

标签: java string graphics drawing slick2d

差不多2天了,我仍然没有想到这一点。我现在很确定我有失眠症,我只是想知道如何做到这一点。我想要绘制的字符串位于Square类的startGame()中。该方法在startNormalMode()中的NormalMode类中调用。晚上9点醒来,晚上11:30醒来,现在是凌晨3点05分,因为这个问题,我仍然无法入睡。尽可能多的信息,我只是想成为一个更好的程序员。没有比体验更好的了。

我的方形课程:

package data.src;

import java.util.Random;




import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class Square {

    private StateBasedGame game;
    boolean correct;
    boolean clickable;
    boolean clicked;
    boolean started;
    int squares;
    public Image squareIncorrect;
    public Image squareCorrect;
    public  Graphics g;



    //For drawing these images from other classes



    {
        try {
            squareIncorrect = new Image("res/squareIncorrect.png");
        } catch (SlickException e1) {

            e1.printStackTrace();
        }
        try {
            squareCorrect =  new Image("res/squareCorrect.png");
        } catch (SlickException e) {

            e.printStackTrace();
        }
    }

    public Square() {



    }

    public boolean checkCorrect(){
        return correct;

    }
    public boolean checkClickable(){
        return clickable;
    }

    public boolean checkClicked(){
        return clicked;

    }

    public int returnNumberOfSquares(){
        return squares;

    }
    //Draw appropriate square on screen depending on the creatRandom. CREATE THE RANDOM FIRST!
        public void squareDraw(int x ,int y, Graphics g)throws SlickException{

            if(correct == true){
                g.drawImage(squareCorrect, x, y);
                started = true;
            }
            else if(correct == false){
                g.drawImage(squareIncorrect, x, y);
            started = true;
            }
    }

    public void createRandom(){

        Random rand = new Random();
        int sRand = rand.nextInt(2);

            if(sRand == 0){
                correct = false;
                clickable = true;
                clicked = false;
                System.out.println("This square is a phony!");

            }
            else if(sRand == 1){
                correct = true;
                clickable = true;
                clicked = false;
                System.out.println("This is a true Square!");
                squares = squares+1;
            }
        }

    public void startGame(Graphics g, GameContainer container)throws SlickException{
        System.out.println("It works.");
        g.drawString("Remember the blue squares! The game will start in 5 seconds!", 200, 300);


    }




    public void youWin(int x, int y, Graphics g){
        if(squares == 0){
            g.drawString("You win!", x, y);
            try {
                wait(4000);
                game.enterState(0);
            } catch (InterruptedException e) {
                e.printStackTrace();

            }
        }
    }
}

我的NormalMode类:

package data.src;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class NormalMode extends BasicGameState {

    private Square[][] square = new Square[4][4];
    private Square squareChecker;
    Graphics g;


    public NormalMode() {


                }

    @Override
    public void init(GameContainer arg0, StateBasedGame Game)throws SlickException {
        squareChecker = new Square();
                    for(int i = 0; i < square.length; i++){
                        for(int j = 0; j < square.length; j++){
                            square[i][j] = new Square();
                            square[i][j].createRandom();

                        }
            }
                    startNormalMode(arg0);
    }                



    @Override
    public void render(GameContainer container, StateBasedGame game, Graphics g)
            throws SlickException{

                square[0][0].squareDraw(100, 300, g);
                square[0][1].squareDraw(150, 300, g);
                square[0][2].squareDraw(200, 300, g);
                square[0][3].squareDraw(250, 300, g);

                square[1][0].squareDraw(100, 400, g);
                square[1][1].squareDraw(150, 400, g);
                square[1][2].squareDraw(200, 400, g);
                square[1][3].squareDraw(250, 400, g);

                square[2][0].squareDraw(100, 500, g);
                square[2][1].squareDraw(150, 500, g);
                square[2][2].squareDraw(200, 500, g);
                square[2][3].squareDraw(250, 500, g);

                square[3][0].squareDraw(100, 600, g);
                square[3][1].squareDraw(150, 600, g);
                square[3][2].squareDraw(200, 600, g);
                square[3][3].squareDraw(250, 600, g);


                    }

    @Override
    public void update(GameContainer container, StateBasedGame game, int delta)
            throws SlickException {

    }

    @Override
    public int getID() {
        return 3;
    }


public void startNormalMode(GameContainer container)throws SlickException{

        Graphics g1 = container.getGraphics();


        squareChecker.startGame(g1, container);

    }
}

P.S。 :我使用的是slick2D库,没有错误,字符串不会出现在屏幕上。

1 个答案:

答案 0 :(得分:0)

根据Orginal Post发表的评论,只需检查游戏渲染图形的元素。第一个元素将被正在绘制的最后一个元素覆盖。

如果需要更多信息,请告诉我,我会尽我所能!