3
\$\begingroup\$

I am Trying to show Image as background, but it doesn't fit to the screen. When I use this class as the start screen it fit the screen properly but when I call this class after winning the level, the image becomes bigger than the device screen and only part of it is shown but when I divide the width and height on 2 the image is becomes smaller than the screen :game.batch.draw(background, 0, 0, Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2); this is the code :

public class Celebration  implements Screen {
    private Fruits game;
    private OrthographicCamera gamecam;
    private Viewport gameport;

    private Stage stage;
    private World world;
    private static float temp;
    private float level;
    private Texture background;
    private Texture start;
    private Texture loading;
    private float counter;

    public Celebration(Fruits game, float level)
    {
        this.game=game;
        this.level=level;
        gamecam=new OrthographicCamera();
        gameport=new StretchViewport(820/Fruits.PPM,580/Fruits.PPM,gamecam);
        stage=new Stage(gameport,((Fruits) game).batch);
        background=new Texture("Wining.jpg");

        gamecam.position.set(gameport.getWorldWidth() , gameport.getWorldHeight() , 0);
        temp=0;

        counter=100;
        world=new World(new Vector2(0,-10),true);
    }
    @Override
    public void show() {

    }
    public void handleinput(float dt)
    {
        if (counter<0)
        {
            game.setScreen(new PlayScreen(game,level));
        }
    }
    public void update(float dt)
    {
        handleinput(dt);
        world.step(1 / 60f, 6, 2);

        gamecam.update();
    }
    @Override
    public void render(float delta) {
        float level;
        update(delta);
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        counter--;
        game.batch.begin();
        game.batch.draw(background, 0, 0, Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);

        game.batch.end();

    }

    @Override
    public void resize(int width, int height) {
        gameport.update(width, height);

    }

}
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$
     texture = new Texture(
            Gdx.files.internal("Menubckgrnd/MenuScreen.png"));
    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

// here 2048 and 1238 is the actul width and height of the image . so replace these values with your images height and width

    TextureRegion region = new TextureRegion(texture, 0, 0, 2048, 1238);

    sprite = new Sprite(region);
    sprite.setSize(1f, 1.12f * sprite.getHeight() / sprite.getWidth());
    sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
    sprite.setPosition(-sprite.getWidth() / 2, -sprite.getHeight() / 2);

Try this code . it will defenitly works

\$\endgroup\$
2
\$\begingroup\$

Try this. In create method

texture = new Texture(Gdx.files.internal("myBackground.png"));
mainBackground = new TextureRegion(texture, 0, 0, 2826, 1590);
batch = new SpriteBatch();

2826 and 1590 is the image width and height respectively.

In render method

batch.begin();
batch.draw(mainBackground, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
//...
batch.end()

Don't forget to declare it first.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .