VB.NET

Moderators: seancampbell
Number of threads: 4022
Number of posts: 10035

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Using stars to draw rectangle Posted by GODDY on 6 Jun 2007 at 3:59 AM

Hi everyone i'm trying to draw a rectagle using the code below. i want the border of the rectagle to use stars (***) something like this

say a user inputs 4 in the inputbox

****
* *
* *
****

How can i get this working?

thanks




Public Class frmRectagle
Inherits System.Windows.Forms.Form
Private number As Integer

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
number = (InputBox("Inter number between 3 and 50", "Rectangle"))
If number > 3 AndAlso number < 50 AndAlso number <> "" Then
Dim g As Graphics = Me.CreateGraphics()
Dim x As String = 40
Dim y As String = 40
Dim height As String = 120
Dim width As String = 120

Dim pt As New Point(number, number)

Dim sz As New Size(number, number)

Dim rect1 As New Rectangle(pt, sz)

Dim Pen As New Pen(Color.Blue, 3)
g.DrawRectangle(Pen, rect1)

Pen.Dispose()
g.Dispose()

Else
If number < 3 Then
MessageBox.Show(" Enter Number greater then 3")
ElseIf number > 50 Then
MessageBox.Show("Enter number les then 50")
ElseIf number = "" Then
MessageBox.Show("Enter Value", "Rectangle")
End If
End If

End Sub

End Class
Report
Re: Using stars to draw rectangle Posted by seancampbell on 6 Jun 2007 at 8:46 AM
Are you trying to draw a Rectangle and instead of having a rectangle draw, have stars draw instead? That would be a bit different then the code I emailed you.

Report
Re: Using stars to draw rectangle Posted by seancampbell on 7 Jun 2007 at 9:53 AM
This is the solution I provided Goddy (This solution is commented to help him understand the code). He was not asking me to draw stars inside of a rectangle but I am writing that code and will post it next for anyone interested in using it.

Oh yeah.... The form has 2 objects. One is a Panel named Panel1 and the other is a Button which has this code underneath it:

        'I get the feeling like I am doing somoene's project for them...

        'This is very easy to do, so pay attention

        'This line creates a new Bitmap object the same size as Panel1
        'The reason we do this is because we are going to draw into this
        'object and then set the Panel's background image to equal this object
        Dim BMP As New Bitmap(Panel1.Width, Panel1.Height)

        'This sets our graphics object to paint on the BMP
        'If we wanted to draw directly to the Panel, I beleive we could
        'say G = Graphics.FromImage(Panel1.BackgroundImage)
        Dim G As Graphics = Graphics.FromImage(BMP)

        'This function fills in the Bitmap with Color.White
        G.Clear(Color.White)

        'Guess what these are used for.
        Dim X As Integer = 5 'Set these to 5 so there is a bit of space
        Dim Y As Integer = 5 'from the left side of the panel

        'Ok this is the user entry part that asks the user to enter
        'a number and then sets an Integer object (number) = to that number

        Dim UserEnteredNumber As String = ""
        Do While Not IsNumeric(UserEnteredNumber)
            UserEnteredNumber = InputBox("Enter a number: ")
        Loop

        Dim Number As Integer = CInt(UserEnteredNumber)

        Dim i As Integer = 0 'This var is used for the For loops


        'This for loop draws Stars along the top of the Rectangle
        For i = 1 To Number - 1

            'So I am drawing a * at x, y
            'It is drawn in Sans Serif at 12 point font
            G.DrawString("*", New Font(Font.FontFamily.GenericSansSerif, 12, FontStyle.Regular, GraphicsUnit.Point), New SolidBrush(Color.Black), X, Y)

            'I increase X by 5 no so on the next pass of the For loop it
            'will draw the next * 5 pixels to the right of the first one
            X = X + 5

        Next

        'At this point there is 1 less * on the top then we need
        'So The next * we draw we want to be at the top.
        'Since X is sitting 5 pixels right of the last draw *
        'The next For loop (which is going to draw down the right side of the
        'square) is going to start by drawing its first * on the top
        For i = 1 To Number - 1

            G.DrawString("*", New Font(Font.FontFamily.GenericSansSerif, 12, FontStyle.Regular, GraphicsUnit.Point), New SolidBrush(Color.Black), X, Y)

            'This makes the next * draw 5 pixels down from the last one
            Y = Y + 5
        Next

        'At this point there is 1 less * on the right side then we need
        'The next for loop will draw across the bottom
        'starting at the right and moving to the left
        'The first * will be drawn where we are missing one on the right
        For i = 1 To Number - 1

            G.DrawString("*", New Font(Font.FontFamily.GenericSansSerif, 12, FontStyle.Regular, GraphicsUnit.Point), New SolidBrush(Color.Black), X, Y)

            'This makes the next * draw 5 pixels to the left of the last one
            X = X - 5
        Next

        'At this point there is 1 less * on the bottom then we need
        'Like previously we will start where we left of and draw *'s up
        'the left side
        'When this for loop is done, the square will be complete because the last
        'position that would need a * is 5, 5 and we drew our first one there
        For i = 1 To Number - 1

            G.DrawString("*", New Font(Font.FontFamily.GenericSansSerif, 12, FontStyle.Regular, GraphicsUnit.Point), New SolidBrush(Color.Black), X, Y)

            Y = Y - 5

        Next

        'Now we have made the square in memory
        'BMP is ready to be set as Panel1's background

        'This function puts BMP into the panel
        Panel1.BackgroundImage = BMP

        'Done
        'Good Luck!
Report
Re: Using stars to draw rectangle Posted by seancampbell on 7 Jun 2007 at 11:27 AM
Ok. This function returns a Bitmap which you can then do a GraphicsObj.DrawImage with:

    Function TextRect(ByVal CharToDraw As Char, ByVal CharSpace As Integer, ByVal Rect As Rectangle) As Bitmap
        'This function returns a bitmap image
        'of the rectangle. Since it is transparent there shouldn't
        'be a problem with pasting this on to of graphics
        Dim BMP As New Bitmap(Rect.Width + CharSpace, Rect.Height + CharSpace)
        Dim G As Graphics = Graphics.FromImage(BMP)

        Dim X As Integer
        Dim Y As Integer

        Dim xRatio As Double = Rect.Width / CharSpace
        Dim yRatio As Double = Rect.Height / CharSpace

        Dim i As Integer
        For i = 1 To CInt(xRatio) - 1
            G.DrawString(CharToDraw, New Font(Font.FontFamily.GenericSansSerif, 12, FontStyle.Regular, GraphicsUnit.Point), New SolidBrush(Color.Black), X, Y)
            X = X + CharSpace
        Next
        For i = 1 To CInt(yRatio) - 1
            G.DrawString(CharToDraw, New Font(Font.FontFamily.GenericSansSerif, 12, FontStyle.Regular, GraphicsUnit.Point), New SolidBrush(Color.Black), X, Y)
            Y = Y + CharSpace
        Next
        For i = 1 To CInt(xRatio) - 1
            G.DrawString(CharToDraw, New Font(Font.FontFamily.GenericSansSerif, 12, FontStyle.Regular, GraphicsUnit.Point), New SolidBrush(Color.Black), X, Y)
            X = X - CharSpace
        Next
        For i = 1 To CInt(yRatio) - 1
            G.DrawString(CharToDraw, New Font(Font.FontFamily.GenericSansSerif, 12, FontStyle.Regular, GraphicsUnit.Point), New SolidBrush(Color.Black), X, Y)
            Y = Y - CharSpace
        Next

        Return BMP

    End Function
Report
Re: Using stars to draw rectangle Posted by seancampbell on 7 Jun 2007 at 11:58 AM
Ok Goddy got back to me and informed me he was trying to "Print" that rectangle to a printer. Since we found code on About.com with a MyPrinter class object that would print a single string, I redid the code to create a square of characters in one single string. It uses vbNewLine to seperate each line so when it is passed to the printer it would work right.

Here is that code:
        'Temporary string we will use for input and for printing
        Dim Str As String = ""
        Dim Num As Integer

        Do While Not IsNumeric(Str)
            Str = InputBox("Input an integer value: ")
        Loop
        Num = CInt(Str)

        Str = "" 'Set Str back to nothing
        Dim i, h As Integer
        For i = 1 To Num
            If i = 1 Or i = Num Then
                'The first and last parts of the string
                'will be full of *'s (top and bottom of the box)
                For h = 1 To Num
                    Str &= "*"
                Next
            Else
                'The sides will be stars and the middle will be spaces
                Str &= "*"
                Str &= Space(Num - 2)
                Str &= "*"
            End If
            'vbNewLine is a carrage return (like hitting enter in a text editor)
            Str &= vbNewLine
        Next
        'Str now equals a printable rectangle

        'Add Printing code here




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.