Canvas size for text output is too small.

Results 1 to 3 of 3
  1. #1
    OFWGK†∆ joellol is offline
    True MemberRank
    Apr 2008 Join Date
    HollandLocation
    508Posts

    Canvas size for text output is too small.


    RaGEZONE Recommends

    RaGEZONE Recommends

    /title

    Hey there peeps, the title says it all.

    My text size is too big for it's canvas. How can i fix this?

    Code snippet:

    Textboard base

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    
    using GXPEngine;
    
    public class TextBoard : Canvas
    {
        public TextBoard(int width, int height) : base(width, height)
    
        {
            SetText("");
        }
    
        public void SetText(string text)
        {
    
            //graphics.Clear(Color.Gray);
            Font drawFont = new Font("ArialBlack", 15);
            graphics.DrawString(text, drawFont, Brushes.Black, 0, 0);
    
        }
    
    }
    What i'm trying to put in (Ignore the sprite / just press 1 to...)

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    
    using GXPEngine;
    
    public class MenuDesign : Sprite
    {
        private TextBoard _Level1;
    
        public MenuDesign(int PosX, int PosY, MyGame gamemanager) : base("StartSc.png")
        {
            Sprite SaxionLogo = new Sprite("Saxion.png");
            AddChild(SaxionLogo);
            SaxionLogo.scale = 0.5f;
            SaxionLogo.x = ((MyGame.WIDTH / 4) * 3);
            SaxionLogo.y = ((MyGame.HEIGHT/ 4) * 3);
            this.x = PosX;
            this.y = PosY;
    
            _Level1 = new TextBoard(MyGame.GRID_WIDTH*2, MyGame.GRID_HEIGHT);
            _Level1.x += (MyGame.WIDTH / 2);
            _Level1.y += (MyGame.HEIGHT / 2);
            _Level1.x += PosX;
            _Level1.y += PosY;
            AddChild(_Level1);
            _Level1.SetText("Press 1 to start level one.");
        }
    
        public void Update()
        {    
        }
    }
    Image of what the actual result is:

    https://imgur.com/a/xRJB4

    Thanks in advance :)

    Joël

    Edit: The GXPEngine is provided by my school, for this project obviously.
    If i helped please click the Thanks button.


  2. #2
    Canvas size for text output is too small. Future is online now
    SupervisorRank
    Dec 2011 Join Date
    1,839Posts

    Re: Canvas size for text output is too small.

    A blatant answer would be: "Make it larger then"

    It really depends on how you plan to use that Canvas. Should it be statically sized? If that's the case, then make it wider so possible texts will fit.

    If you want the Canvas to size with the text, you have to calculate the boundaries, of the text to be drawn, in TextBoard.SetText and adjust the Canvas' size on every call. I don't know this engine your school provided, but with typical GDI+ stuff (in C#) you'd call a text length measuring method like Graphics.MeasureString. So your TextBoard.SetText should look something like this:

    Code:
    public void SetText(string text)
    {
    	Font drawFont = new Font("ArialBlack", 15);	// should not be instantiated here for performance reasons
    	
    	// Measure string boundaries
    	SizeF sSize = graphics.MeasureString(text, drawFont);
    	
    	// Adjust canvas width if necessary
    	if (sSize.Width > this.Width)
    		this.Width = sSize.Width;	// or alternative engine call
    	
    	graphics.DrawString(text, drawFont, Brushes.Black, 0, 0);
    }
    Note that the code might not work as you want it to, it's just to give you an idea of what you'd need to do. For a school project, this should do.




    Quote Originally Posted by A Wise Man
    P-Servers are NOT dead. Bugs need squishing. Quests need fixing. Unfortunately, majority of people don't know the difference between a computer and a toaster so...

  3. #3
    OFWGK†∆ joellol is offline
    True MemberRank
    Apr 2008 Join Date
    HollandLocation
    508Posts

    Re: Canvas size for text output is too small.

    Quote Originally Posted by Future View Post
    A blatant answer would be: "Make it larger then"

    It really depends on how you plan to use that Canvas. Should it be statically sized? If that's the case, then make it wider so possible texts will fit.

    If you want the Canvas to size with the text, you have to calculate the boundaries, of the text to be drawn, in TextBoard.SetText and adjust the Canvas' size on every call. I don't know this engine your school provided, but with typical GDI+ stuff (in C#) you'd call a text length measuring method like Graphics.MeasureString. So your TextBoard.SetText should look something like this:

    Code:
    public void SetText(string text)
    {
    	Font drawFont = new Font("ArialBlack", 15);	// should not be instantiated here for performance reasons
    	
    	// Measure string boundaries
    	SizeF sSize = graphics.MeasureString(text, drawFont);
    	
    	// Adjust canvas width if necessary
    	if (sSize.Width > this.Width)
    		this.Width = sSize.Width;	// or alternative engine call
    	
    	graphics.DrawString(text, drawFont, Brushes.Black, 0, 0);
    }
    Note that the code might not work as you want it to, it's just to give you an idea of what you'd need to do. For a school project, this should do.
    Thanks alot Future, i'll try this asap. :)
    If i helped please click the Thanks button.




Advertisement