Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify this following code so that each line of text is centered horizontally: /

ID: 3559417 • Letter: M

Question

Modify this following code so that each line of text is centered horizontally:

    /* Draws the different fonts, trying all the

     * styles, and using various point sizes. Draws

     * all text relative to the form's size.

     */

    public class Text : Form

    {

        private Font arialBold24 = new Font("Arial", 24, FontStyle.Bold);

        private Font timesItalic14 = new Font("Times New Roman", 14, FontStyle.Italic);

        private Font courierPlain18 = new Font("Courier New", 18, FontStyle.Strikeout);

        private Font genericSerifBI20 = new Font(FontFamily.GenericSerif, 20, FontStyle.Bold | FontStyle.Italic);

        private Font verdanaPlain18 = new Font("Verdana", 18, FontStyle.Regular | FontStyle.Underline);

        public Text()

        {

            Size = new Size(400, 200);

            Text = "Text";

            BackColor = Color.White;

        }

        protected override void OnPaint(PaintEventArgs e)

        {

            Graphics g = e.Graphics;

            int w = (int)g.MeasureString(arialBold24.Name, arialBold24).Width;

            int arialStart = (Width - w) / 2;

           int otherStart = Width / 4;

            int h = DisplayRectangle.Height;

            g.DrawString(arialBold24.Name, arialBold24, Brushes.Blue, arialStart, 0);

            g.DrawString(timesItalic14.Name, timesItalic14, Brushes.Blue, otherStart, h / 5);

          g.DrawString(courierPlain18.Name, courierPlain18, Brushes.Blue, otherStart, 2 * h / 5);

            g.DrawString(genericSerifBI20.Name, genericSerifBI20, Brushes.Blue, otherStart, 3 * h / 5);

            g.DrawString(verdanaPlain18.Name, verdanaPlain18, Brushes.Blue, otherStart, 4 * h / 5);

            base.OnPaint(e);

        }

        public static void Main()

        {

            Application.Run(new Text());

        }

    }

Explanation / Answer

The answer was provided in the arialStart variable. The equation to center the font horizontally was already done to the arialBold24 font. So you just had to simple use the same formula on the rest of the fonts. As so: (I left the arial font as is and only modified what I added)

public class Text : Form
{
private Font arialBold24 = new Font("Arial", 24, FontStyle.Bold);
private Font timesItalic14 = new Font("Times New Roman", 14, FontStyle.Italic);
private Font courierPlain18 = new Font("Courier New", 18, FontStyle.Strikeout);
private Font genericSerifBI20 = new Font(FontFamily.GenericSerif, 20, FontStyle.Bold | FontStyle.Italic);
private Font verdanaPlain18 = new Font("Verdana", 18, FontStyle.Regular | FontStyle.Underline);
public Text()
{
Size = new Size(400, 200);
Text = "Text";
BackColor = Color.White;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
int w = (int)g.MeasureString(arialBold24.Name, arialBold24).Width;
int timesItalic14Width = (int)g.MeasureString(timesItalic14.Name, timesItalic14).Width;
int courierPlain18Width = (int)g.MeasureString(courierPlain18.Name, courierPlain18).Width;
int genericSerifBI20Width = (int)g.MeasureString(genericSerifBI20.Name, genericSerifBI20).Width;
int verdanaPlain18Width = (int)g.MeasureString(verdanaPlain18.Name, verdanaPlain18).Width;

int arialStart = (Width - w) / 2;

//int otherStart = Width / 4;
int h = DisplayRectangle.Height;
g.DrawString(arialBold24.Name, arialBold24, Brushes.Blue, arialStart, 0);
g.DrawString(timesItalic14.Name, timesItalic14, Brushes.Blue, AlignHorizontally(timesItalic14Width), h / 5);
g.DrawString(courierPlain18.Name, courierPlain18, Brushes.Blue, AlignHorizontally(courierPlain18Width), 2 * h / 5);
g.DrawString(genericSerifBI20.Name, genericSerifBI20, Brushes.Blue, AlignHorizontally(genericSerifBI20Width), 3 * h / 5);
g.DrawString(verdanaPlain18.Name, verdanaPlain18, Brushes.Blue, AlignHorizontally(verdanaPlain18Width), 4 * h / 5);
base.OnPaint(e);
}
public static void Main()
{
Application.Run(new Text());
}

private void InitializeComponent()
{
this.SuspendLayout();
//
// Text
//
this.ClientSize = new System.Drawing.Size(284, 261);
this.Name = "Text";
this.ResumeLayout(false);

}

private int AlignHorizontally(int fontWidth)
{
return (Width - fontWidth) / 2;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote