Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  Dynamic center and place strings on a JPanel  (Read 791 times)
0 Members and 1 Guest are viewing this topic.
Offline stef569

Junior Member





« Posted 2007-09-26 23:47:51 »

Hi,

I'm having trouble with Painting multiple strings nice underneath eatchother.
I already can draw a message excactly in the center
but when i start adding more there is no spacing between them...
so as a workaround i add more "\n" symbols but after adding 3 when trying to write game over in 35 points i had enough...
I alsoo tried to put the startRow higher but then all rows get placed higher so that's useless.
How would i get spacing between the strings.

Example function
drawMessages(Color.YELLOW, "Game Over!\nI win ^^", 35, g);

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
  private void drawMessages(Color color, String msg, int FontPointSize, Graphics2D g) {
    String[] msges;
    int startRow;
    int mesLen;

    // Put Message into Array
   msges = msg.split("\n");
    mesLen = msges.length;

    // Create Font information Object
   FontMetrics fm = g.getFontMetrics();

    // Get the Height of the Message
   int msgHeight = fm.getHeight();

    // Calculate the Row where we are going to start in PIXELS
   startRow = PANEL_HEIGHT/2 - mesLen/2*msgHeight;

    for(int i=0;i<mesLen;i++) {
      drawCenterMessage(color, msges[i], startRow+i*msgHeight, FontPointSize, g);
    }
  }
 


 
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  
 /**
   * Draw a Message in the Horizontal middle of the Panel
   * @param color The Text Color
   * @param msg The Message to be displayed
   * @param g graphical context
   */

  private void drawCenterMessage(Color color, String msg, int row, int FontPointSize, Graphics2D g) {
    final int VERTICAL_PADDING = 10;   //Unused
   double xP;      /* X pixel where we will be drawing the message */
    double yP;      /* Y pixel where we will be drawing the message */
    int msgWidth;  /* The Width of the message in the given Font in pixels */

    // Create Font and add it to g.
   Font font = new Font("Dialog", Font.BOLD, FontPointSize);
    g.setFont(font);

    // Create Font information Object
   FontMetrics fm = g.getFontMetrics();

    // Get the With and Height of the Message
   msgWidth  = fm.stringWidth(msg);

    // Set Color
   if (color == null)
      g.setColor(Color.WHITE);
    else
      g.setColor(color);

    // Col Calculation
   xP = (GAME_PANEL_WEIGHT/2) - (msgWidth/2);

    // Row Calculation
   if( row<=0 || row > PANEL_HEIGHT)
      throw new IllegalArgumentException("row: " + row + " is not within panel bounds!");
    else
      yP = row;

    g.drawString(msg, (float) xP, (float) yP);
  }


Functions are both in the same class. Wink
Offline broumbroum

Junior Member





« Reply #1 - Posted 2007-09-27 00:00:35 »

1  
    startRow = PANEL_HEIGHT/2 - mesLen/2*msgHeight;
these quotients must be on higher precision. change for 2f and (float) or (double) for better results. Usually it makes positoning more accurate.  Wink

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline stef569

Junior Member





« Reply #2 - Posted 2007-09-27 11:37:48 »

I posted too fast, i've solved it Shocked

    // Get the Height of the Message and add additional spacing.
    int msgHeight = fm.getHeight()+VERTICAL_PADDING;

is what i changed, working perfectly now Smiley

and indeed i changed everything to floats since the draw string wants to have floats anyway...
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.

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

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

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

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

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

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

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

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

BurntPizza (42 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
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!