New topic: NilObjectException when using Graphics.TextSize
<http://forums.realsoftware.com/viewtopic.php?t=37957> Page 1 of 1 [ 9 posts ] Previous topic | Next topic Author Message alexvs Post subject: NilObjectException when using Graphics.TextSizePosted: Sat Mar 05, 2011 12:48 pm Joined: Fri Feb 12, 2010 1:32 pm Posts: 69 Location: Switzerland Hello, I get a NOE on the second line when running following code: Code: Dim grph as Graphics grph.TextSize = 0 grph.TextFont = "System" CaptionWidth = grph.StringWidth(pCaption) Have I missed something? Thanks, Alex Top Jym Post subject: Re: NilObjectException when using Graphics.TextSizePosted: Sat Mar 05, 2011 1:20 pm Joined: Sat Oct 01, 2005 5:19 pm Posts: 2276 look at your debugger and you will see grph is Nil then look up Graphics in the LR and you will see you can't create a meaningful Graphics object. Top alexvs Post subject: Re: NilObjectException when using Graphics.TextSizePosted: Sat Mar 05, 2011 1:31 pm Joined: Fri Feb 12, 2010 1:32 pm Posts: 69 Location: Switzerland Okay, so would the right way be to create a picture and use the picture's graphics class? Thanks, Alex Top npalardy Post subject: Re: NilObjectException when using Graphics.TextSizePosted: Sat Mar 05, 2011 1:35 pm Joined: Sat Dec 24, 2005 8:18 pm Posts: 6487 Location: Canada, Alberta, Near Red Deer alexvs wrote:Okay, so would the right way be to create a picture and use the picture's graphics class? Thanks, Alex Yes try Code: dim p as new PIcture(1,1,32) Dim grph as Graphics = p.graphics grph.TextSize = 0 grph.TextFont = "System" CaptionWidth = grph.StringWidth(pCaption) _________________ My web site Great White Software RBLibrary.com REALbasic learning Top alexvs Post subject: Re: NilObjectException when using Graphics.TextSizePosted: Sat Mar 05, 2011 1:38 pm Joined: Fri Feb 12, 2010 1:32 pm Posts: 69 Location: Switzerland Cool, thanks. Did already do it, so quite similare code, see here: Code: Dim tmpPic as new picture(1, 1, 32) tmpPic.Graphics.TextSize = 0 tmpPic.Graphics.TextFont = "System" CaptionWidth = tmpPic.Graphics.StringWidth(pCaption) CaptionHeight = tmpPic.Graphics.StringHeight(pCaption, 9999) Pic.Graphics.DrawString pCaption, 10, (23-CaptionHeight)/2 But kind of a strange thing happens, with the line (23-CaptionHeight)/2 I do want to position it in the middle (23 is the hight, I need it to be hard coded). But somehow the StringHeight does deliver a far to high number, Is there a bug in my code? EDIT: Forget it, I was wrong. the y parameter is the baseline, didn't know. Sorry for that! Alex Top harriew Post subject: Re: NilObjectException when using Graphics.TextSizePosted: Sat Mar 05, 2011 2:38 pm Joined: Fri Dec 01, 2006 3:09 pm Posts: 502 Location: Tennessee, USA When you specify the y value for printing text you are specifying the line upon which all of the uppercase characters will sit. Sort of like when using lined paper when learning how to write. The descenders of lower case characters fall below that line and also below that line is the line gap (leading) value which gives what the font designer considers to be the proper blank space between lines of text. Because of this, attempting to exactly center a line of text vertically is not an easy task since none of the font metrics exposed in RB give you what you need. If your project will work with only one font you can experiment with different y values until you find what works best. If you allow the user to vary the font used the task becomes much harder and requires a lot more testing to get a calculation that works good in all cases. I speak from experience on this one. Top alexvs Post subject: Re: NilObjectException when using Graphics.TextSizePosted: Sat Mar 05, 2011 2:47 pm Joined: Fri Feb 12, 2010 1:32 pm Posts: 69 Location: Switzerland Okay. Another question regarding Graphics and so on If the user clicks on the canvas, I would like to shade it, meaning, making it darker. Here's what I have, but it seems to not be the right lines of code. The Picture "ReturnPic" which already contains the actual content, should just be darkened, but instead, the whole original content disappears. Nothing's left, even the shade has not been drawn. Code: Dim shade as new picture(ReturnPic.Width, ReturnPic.Height, 32) Shade.Graphics.ForeColor = &c000000 Shade.Graphics.DrawRect 0, 0, Shade.Width, Shade.Height Shade.Mask.Graphics.ForeColor = &cCCCCCC Shade.Mask.Graphics.DrawRect 0, 0, Shade.Width, Shade.Height Shade.Transparent=1 ReturnPic.Graphics.DrawPicture Shade, 0, 0, Shade.Width, Shade.Height Top jefftullin Post subject: Re: NilObjectException when using Graphics.TextSizePosted: Sat Mar 05, 2011 5:27 pm Joined: Wed Nov 15, 2006 3:50 pm Posts: 2171 Location: England You dont need a big image if you want to do the shading. Even a 1 x 1 pixel image will do the trick. Your drawing uses .DrawRect but should use .Fillrect I like to use Figureshapes myself. This example uses a red shading Code: dim fig as new figureShape fig.FillColor=&cff0000 'the color fig.Fill=50 'how transparent fig.addline (0,0, 100,0) fig.addline (100,0,100,100) fig.addline (100,100,0,100) fig.addline (0,0,0,100) thepicture.graphics.drawobject fig Top Thom McGrath Post subject: Re: NilObjectException when using Graphics.TextSizePosted: Sat Mar 05, 2011 5:57 pm Site Admin Joined: Tue May 06, 2008 1:07 pm Posts: 695 Location: Greater Hartford Area, CT Don't set the transparent property. _________________ Thom McGrath - @tekcor Web Framework Architect, REAL Software, Inc. Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 9 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
