I'm new to Libgdx and am having some problems filling an image to the entire screen. If I import a 1900x1200 image, it fills the screen, but if instead the image is 1024x512, the image is not stretched to the screen. The following is the code I've used. I thought that ''background.setSize(stageWidth, stageHeight)'' would scale the image to the screen but this doesn't happen. Could you please inform me of what I'm doing wrong? I've tried toggling with the ''setFillParent'' (as mentioned in another post) but it still doesn't work. Here's also included a screenshot of what currently appears (both on the Desktop implementation and Android): http://tinypic.com/r/b7j20z/8
Thank You for your help!
package com.mygdx.game;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class MyGdxGame implements ApplicationListener{
private Stage stage;
#Override
public void create() {
stage = new Stage();
float stageHeight = Gdx.graphics.getHeight();
float stageWidth = Gdx.graphics.getWidth();
// Background
Texture board = new Texture(Gdx.files.internal("data/frame.png"));
Image background = new Image(board);
background.setOrigin(0, 0);
background.setSize(stageWidth, stageHeight);
background.rotateBy(0);
background.setPosition(0, 0);
stage.addActor(background);
}
#Override
public void dispose() {
stage.dispose();
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
After thinking about it for a bit, the most likely cause of your problem is that you're adding your image directly to the root of the stage. The root of the stage is a Group that doesn't handle any kind of layout managing, which is probably why your image isn't sized correctly.
I'd recommend instead that you create a Table, and then use setFillParent() so it takes up the entire screen. You can then simply use the Table's built in function, table.background(drawable), to set a background. However, you have to keep in mind that a Texture doesn't count as drawable for the table, so you have to use the class TextureRegionDrawable to get a valid drawable for the background. Here's an example:
table.background(new TextureRegionDrawable(new TextureRegion(board)));
Related
I have three objects using the same functions and variables: just different images. So I decided to move them into a parent class called Entity. They still can render the same images and stuff but suddenly, my Update() method which moves the objects using keypresses no longer works. I'm not very sure why that's the case because to me, the idea of parent class is to store things the child class would use.
my parent class
package com.mygdx.game;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
public class Entity{
public Vector2 position;
public Sprite sprite;
public float speed;
public Entity(Texture img){
sprite = new Sprite(img);
position = new Vector2();
}
public void Update(float deltaTime){
if(Gdx.input.isKeyPressed(Input.Keys.A)) position.x-=deltaTime*speed;
if(Gdx.input.isKeyPressed(Input.Keys.D)) position.x+=deltaTime*speed;
if(Gdx.input.isKeyPressed(Input.Keys.S)) position.y-=deltaTime*speed;
if(Gdx.input.isKeyPressed(Input.Keys.W)) position.y+=deltaTime*speed;
}
public void Draw(SpriteBatch batch){
Update(Gdx.graphics.getDeltaTime());
sprite.setPosition(position.x, position.y);
sprite.draw(batch);
}
}
my child class
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
public class Object1 extends Entity{
public Object1(Texture img){
super(img);
super.position = new Vector2(Gdx.graphics.getWidth()/10,0);
}
}
I found out that the key presses work, but it can't seem to update the position of the image. I thought functions in parent classes are essentially functions in child classes so I don't know why it can't be updated.
my apologies if this is an easy thing for you, but my mind boggles. After several years of not programming at all, I am working on a pet project (2d tile based game engine) where I would like to use Java FX headless in order to make use of the graphics capabilities.
I have understood from here and here
that you need to a Java FX Application in order to have the graphics system initialized.
So I basically took the ImageViewer example and implemented Runnable:
package net.ck.game.test;
import java.io.BufferedReader;
import java.util.ArrayList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ImageTest extends Application implements Runnable {
protected static final Logger logger = (Logger) LogManager.getLogger(ImageTest.class);
BufferedReader x;
#Override public void start(#SuppressWarnings("exports") Stage stage) {
logger.error(Thread.currentThread().getName() + ", executing run() method!");
Image standardImage = new Image("file:graphics/image1.png");
logger.error("image height image1: "+ standardImage.getHeight());
logger.error("image width image1:" + standardImage.getWidth());
Image movingImage = new Image("file:graphics/image2.png");
ArrayList<Image> images = new ArrayList<Image>();
images.add(movingImage);
images.add(standardImage);
ImageView iv1 = new ImageView();
iv1.setImage(standardImage);
ImageView iv2 = new ImageView();
iv2.setImage(movingImage);
Group root = new Group();
Scene scene = new Scene(root);
scene.setFill(Color.BLACK);
HBox box = new HBox();
box.getChildren().add(iv1);
box.getChildren().add(iv2);
root.getChildren().add(box);
stage.setTitle("ImageView");
stage.setWidth(415);
stage.setHeight(200);
stage.setScene(scene);
stage.sizeToScene();
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void run()
{
Application.launch(ImageTest.class);
}
}
When I run this as its own application, this works fine and displays the two images I want it to display.
When I run it like this in the "game" constructor:
public class Game {
private boolean animated;
public boolean isAnimated() {
return animated;
}
public void setAnimated(boolean animated) {
this.animated = animated;
}
public Game() {
setAnimated(true);
if (isAnimated() == true)
{
ImageTest imageTest = new ImageTest();
new Thread(imageTest).start();
}
}
There are no errors, ImageTest runs in its own thread, the application window opens, but it is empty.
I do not understand this at all, why is that?
Can someone pleaese shed some light on this?
UPDATE:
I had different working contexts by accident. Fixing this fixed the problem.
UPDATE: I had different working contexts by accident. Fixing this fixed the problem.
package com.test.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class Splash implements Screen {
private Sprite splash;
private SpriteBatch batch;
private float height = 1920;
private float width = 1080;
private float aspectratio = width/height;
#Override
public void render(float delta){
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
splash.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height){
}
#Override
public void show(){
batch = new SpriteBatch();
Texture splashTexture = new Texture("zeuswallpaperphone1.jpg");
splash = new Sprite(splashTexture);
//splash.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
splash.rotate(90);
splash.setPosition(Gdx.graphics.getWidth()/2 - splashTexture.getWidth()/2, Gdx.graphics.getHeight()/2 - splashTexture.getHeight()/2);
}
#Override
public void hide(){
}
#Override
public void pause(){
}
#Override
public void resume(){
}
#Override
public void dispose(){
}
}
This is the splash screen of my game. I am trying to get the splash screen image to adjust for different screen sizes. Is there a way that I set the screen to one size? I want to set the screen to a size of 1280 x 720. And also will this keep the touch inputs the same? I do not understand why this is so hard in android. I have searched the internet vigorously trying to find a solution. I am also sorry if this question was already answered in another post. It seems that most answers to this problem are really old. I have not been able to find an answer the the problem that I have. I am looking for any help so please someone help me out. I have tried various methods and nothing seems to be working. Thank you in advance for your help. I hate that stack overflow makes you type a certain amount. There are a few libgdx tutorials out there. So if someone could watch one and get back to me that would be great. I have tried playing with the Viewport and Orthographic camera but when I run the game it will not adjust. On iOS you just set the screen size and you are good to go on all devices. They need that in android studio. Nothing seems to be working.
This is the code I have. Thanks for your help. This is crazy they still want me to type more.
I think you need to use a camera and viewport:
OrthographicCamera hudCamera = new OrthographicCamera();
Viewport hudViewport = new ScreenViewport(hudCamera);
public void create {
// Your code
hudViewport.apply(true);
}
public void render(float delta){
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(hudCamera.combined);
batch.begin();
splash.draw(batch);
batch.end();
}
public void resize(int width, int height) {
hudViewport.update(width, height);
hudViewport.apply(true);
}
Make sure you are setting the correct size in your main method.
My question is I'm creating a game where I have to get an image moving from one side of the screen to the other, and then coming back from the same side of the screen. For example I have an image of a jellyfish, it starts moving from the right side of the screen, to the left side, and then I want it to come back from the right side. I'm stuck and don't know what to do. I'm using LIBGDX and JAVA.
My code so far is :
package gdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import java.awt.Graphics;
public class Main extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
Sprite sprite;
float fGrav, fVelo, fX, fY;
#Override
public void create() {
batch = new SpriteBatch();
img = new Texture("JellyFish.png");
sprite = new Sprite(img);
sprite.setScale(0.3f);
fX=0;
fY=0;
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
batch.end();
}
#Override
public void dispose() {
batch.dispose();
img.dispose();
}
}
If you want to continuously bounce an image from left to right, create an infinite loop that increases x until it is >= your display width, and then decrease it until it is <= 0, and then repeat.
I've never worked with LIBGDX, but if you wanted to use the Graphics class it would be as follows:
Example:
boolean f = true;
int x = 0;
while (f) {
while (x <= SCREEN_WIDTH) {
x++;
}
while (x >= 0) {
x--;
}
}
This should loop indefinitely.
Here is my code:
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import acm.graphics.GLabel;
import acm.program.GraphicsProgram;
import javax.imageio.ImageIO;
import acm.util.MediaTools;
public class FlappyBird extends GraphicsProgram {
public Background background; //background image
public UpTube uptube; //one of the pipes
public DownTube downtube; //other pipe
public Bird bird;
//image for the bird
public static final int APPLICATION_WIDTH = 882;
public static final int APPLICATION_HEIGHT = 772;
public void run(){
addKeyListeners();
background = new Background();
add(background);
uptube = new UpTube();
add(uptube);
downtube = new DownTube();
add(downtube);
bird = new Bird();
add(bird);
public void jump(){
for(int i =0;i<5;i++){
bird.move(3,-7);
pause(100);
}
for(int i =0;i<15;i++){
bird.move(5, -4);
pause(100);
}
for(int i =0;i<15;i++){
bird.move(7,0);
pause(100);
}
for(int i =0;i<15;i++){
bird.move(5,7);
pause(100);
}
for(int i =0;i<15;i++){
bird.move(3,-7);
pause(100);
}
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_SPACE){
jump();
However, when I run this and I press the Space Bar, it doesn't show the bird's individual movements, it just teleports the bird to the end location after the pause(100) is over for each for statement. How do I make it so that it updates the bird's location each time I move it?
I dont know about the API you are using but I have made many graphics utilities, games and programs in java and there are some basic principles you must know; the one you seem to be having an 'issue' with is you assume the rendering is done one another thread (happening while this code is running) or will completely redraw whenever the bird moves - this is not the case in most graphics renderers, instead they simply redraw after all processing of every frame.
So, what you will need to do is either get rendering on another thread so the for loop can run while the rendering is happening at a different rate, or, implement more state-machine like code where it knows what it should do each frame (e.g move the bird every 100ms, 15 times).