Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to make text/numbers add to the left instead of right? :(  (Read 681 times)
0 Members and 1 Guest are viewing this topic.
Offline Delta517

Senior Member


Medals: 2
Projects: 1



« Posted 2013-07-20 19:24:36 »

Hi Smiley So I'm pretty sure you're all confused over the title, so I'll try to be more clear Tongue

Im currently adding score to my game, but my problem is that I want to display the Score in the top-right corner, but when I do that, it adds the extra numbers to the right so they go offscreen Sad Is there any way to make it put the extra numbers to the left? :/

Here's a pic to explain what I mean Smiley



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
package com.mayogames.zombiecubes;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.mayogames.zombiecubes.entities.Player;
import com.mayogames.zombiecubes.screens.GameScreen;
import com.mayogames.zombiecubes.weapon.Weapon;

public class HUD {
   
   ZombieCubes zombieCubes;
   private GameScreen gameScreen;
   private Player player;
   private Weapon weapon;
   
   private float stateTime;
   private TextureRegion currentHeartFrame;
   
   public HUD(ZombieCubes zombieCubes, GameScreen gameScreen, Player player, Weapon weapon){
      this.zombieCubes = zombieCubes;
      this.gameScreen = gameScreen;
      this.player = player;
      this.weapon = weapon;
   }
   
   public void tick(){
     
   }
   
   public void render(SpriteBatch hudBatch){
      renderHearts(hudBatch);
      Assets.white.draw(hudBatch, getPointsAsString(gameScreen.getPoints()), 700, 483);
   }
   
   public void renderHearts(SpriteBatch hudBatch){
      stateTime += Gdx.graphics.getDeltaTime();
      currentHeartFrame = Assets.healthHUDAnim.getKeyFrame(stateTime, true);
     
      if(player.getHp() >= 3){
         hudBatch.draw(currentHeartFrame, 64, 448);
         hudBatch.draw(currentHeartFrame, 32, 448);
         hudBatch.draw(currentHeartFrame, 0, 448);
      }else if(player.getHp() == 2){
         hudBatch.draw(Assets.heartEmpty, 64, 448);
         hudBatch.draw(currentHeartFrame, 32, 448);
         hudBatch.draw(currentHeartFrame, 0, 448);
      }else if(player.getHp() == 1){
         hudBatch.draw(Assets.heartEmpty, 64, 448);
         hudBatch.draw(Assets.heartEmpty, 32, 448);
         hudBatch.draw(currentHeartFrame, 0, 448);
      }else if(player.getHp() <= 0){
         hudBatch.draw(Assets.heartEmpty, 64, 448);
         hudBatch.draw(Assets.heartEmpty, 32, 448);
         hudBatch.draw(Assets.heartEmpty, 0, 448);
      }
   }
   
   public String getPointsAsString(int convertToString){
      return String.format("%02d", convertToString);
     
   }
}
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #1 - Posted 2013-07-20 19:55:41 »

Find out how big one character is in pixels. We will call this:
1  
int charlengthinpixels = 4;


Then, you need the length of the number.
1  
int lengthofstring = String.valueOf(score).length;


Now, with every character(lengthofstring) subtract charlengthinpixels from the x value of the score! If the length varies, just add to length in pixels until it is the max!
1  
scorex -= lengthinpixels*lengthofstring;


And thats all!

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #2 - Posted 2013-07-20 20:07:34 »

Easy way: put the score count on the left Wink
Other way: just experiment with how many pixels to offset. This should have been a problem you could solve on your own.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Troncoso

JGO Coder


Medals: 20



« Reply #3 - Posted 2013-07-20 20:13:19 »

Just set the offset to account for the highest score you can have.
Offline ReBirth
« Reply #4 - Posted 2013-07-21 02:53:07 »

Just set the offset to account for the highest score you can have.
That's ugly. No other way, the recalculation is needed.

Offline Sethir

Senior Newbie


Exp: 3 years



« Reply #5 - Posted 2013-07-22 12:04:10 »

wouldn't this work too?
1  
2  
3  
4  
5  
Graphics g = new Graphics();
FontMetrics fm = g.getFontMetrics( g.getFont() );
int scoreWidth = fm.stringWidth(score);

hudBatch.draw(score, 0, 448-scoreWidth);

      
Offline ReBirth
« Reply #6 - Posted 2013-07-22 12:29:07 »

He uses libgdx, so instead Java's FontMetrics better use libgdx's font class (don't remeber the name, but has methods for calculate width/height).

Offline HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #7 - Posted 2013-07-23 01:21:54 »

Draw it backwards on the left and flip it horizontally? Grin

Or you need to calculate the width of the string and draw it offset from the right edge.

Offline philfrei
« Reply #8 - Posted 2013-07-23 09:27:27 »

Is Swing out of bounds for this question?

A JLabel be set up so that the background color is invisible/transparent. And the text can be right-justified. So, you can set it up for the largest possible score and should not have to worry about it further.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline 666DemonThe

Senior Newbie





« Reply #9 - Posted 2013-07-24 14:28:07 »

make a offset !!!!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #10 - Posted 2013-07-24 19:08:12 »

make a offset !!!!
I don't feel like adressing that this has been said, so I'll quote:
Just set the offset to account for the highest score you can have.
That's ugly. No other way, the recalculation is needed.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!