[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 768
  • Last Modified:

Visual Basic 2008 Graphics - Drawing multiple circumscribed circles Part 2

Circle within a circle within a circle...etc...etc...down to 1 point

Ellipses must have a constant radius on both x and y axis "Perfect Circles Only".

Please Include complete project (which includes form1) with screenshot.

CircleDemo2.jpg
0
NerdsOfTech
Asked:
NerdsOfTech
  • 3
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Same thing but going inwards using a diminishing radius value:
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DoubleBuffered = True
    End Sub
 
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Static R As New Random
 
        Dim C As Color
        Dim rc As Rectangle
 
        Dim center As New Point(Me.ClientRectangle.Width / 2, Me.ClientRectangle.Height / 2)
        Dim radius As Integer = Math.Min(Me.ClientRectangle.Width, Me.ClientRectangle.Height) / 2
 
        While radius > 0
            rc = New Rectangle(center.X - radius, center.Y - radius, radius * 2, radius * 2)
            C = Color.FromArgb(255, R.Next(0, 256), R.Next(0, 256), R.Next(0, 256))
            Using P As New Pen(C, 2)
                e.Graphics.DrawEllipse(P, rc)
            End Using
            radius = radius - 1
        End While
    End Sub
 
    Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        Me.Refresh()
    End Sub
 
End Class

Open in new window

CircleDemo3.jpg
0
 
NerdsOfTechTechnology ScientistAuthor Commented:
Perfection! Cool effects when you resize it too! Thank you Idle_Mind once again.
0
 
Daniel WilsonCommented:
Very nice.  What I was working on used a PictureBox.  Yours is cleaner.

And I was unfamiliar with DoubleBuffered.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx


0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
DoubleBuffered is great...but it only works for the FORM itself.  =\

If you want to DoubleBuffer a Panel or PictureBox (basically anything else) then you have to INHERIT that control and manual use SetStyle() to properly configure it:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle(VS.71).aspx

Something like:

    Public Class SomeThing
        Inherits SomeControl

        Public Sub New()
            Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
            Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
            Me.SetStyle(ControlStyles.UserPaint, True)
            Me.UpdateStyles()
        End Sub

    End Class

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
@NerdsOfTech...thanx for the totally unexpected donation!  Much appreciated...  =)

I was playing around, just for grits and shins...try this out:
(Right Click the Form)
Public Class Form1
 
    Private CM As New ContextMenu
    Private PreviewWidth As Integer = 15
    Private PreviewMargin As Integer = 3
    Private SelectedColor As Color = Color.Cyan
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DoubleBuffered = True
        For Each C As Color In System.ComponentModel.TypeDescriptor.GetConverter(GetType(Color)).GetStandardValues
            If Not C.IsSystemColor Then
                Dim mi As New MenuItem(C.ToKnownColor.ToString)
                mi.Tag = C.ToKnownColor
                mi.OwnerDraw = True
                AddHandler mi.MeasureItem, AddressOf mi_MeasureItem
                AddHandler mi.DrawItem, AddressOf mi_DrawItem
                AddHandler mi.Click, AddressOf mi_Click
                CM.MenuItems.Add(mi)
            End If
        Next
        Me.ContextMenu = CM
    End Sub
 
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Static R As New Random
 
        Dim C As Color
        Dim rc As Rectangle
        Dim percent As Double
 
        Dim center As New Point(Me.ClientRectangle.Width / 2, Me.ClientRectangle.Height / 2)
        Dim radius As Integer = Math.Min(Me.ClientRectangle.Width, Me.ClientRectangle.Height) / 2
        Dim startRadius As Integer = radius
 
        While radius > 0
            rc = New Rectangle(center.X - radius, center.Y - radius, radius * 2, radius * 2)
            percent = (startRadius - radius) / startRadius
            C = Color.FromArgb(255, percent * SelectedColor.R, percent * SelectedColor.G, percent * SelectedColor.B)
            Using P As New Pen(C, 2)
                e.Graphics.DrawEllipse(P, rc)
            End Using
            radius = radius - 1
        End While
    End Sub
 
    Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        Me.Refresh()
    End Sub
 
    Private Sub mi_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs)
        Dim mi As MenuItem = CType(sender, MenuItem)
        Dim sz As SizeF = e.Graphics.MeasureString(mi.Text, Me.Font)
        e.ItemHeight = sz.Height
        e.ItemWidth = sz.Width + PreviewWidth + (2 * PreviewMargin)
    End Sub
 
    Private Sub mi_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
        Dim mi As MenuItem = CType(sender, MenuItem)
        e.DrawBackground()
        e.Graphics.DrawString(mi.Text, Me.Font, Brushes.Black, e.Bounds.X + PreviewWidth + (2 * PreviewMargin), e.Bounds.Y)
        Dim rc As New Rectangle(e.Bounds.X + PreviewMargin, e.Bounds.Y + PreviewMargin, PreviewWidth, e.Bounds.Height - (2 * PreviewMargin))
        Using sb As New SolidBrush(Color.FromKnownColor(mi.Tag))
            e.Graphics.FillRectangle(sb, rc)
        End Using
        e.Graphics.DrawRectangle(Pens.Black, rc)
    End Sub
 
    Private Sub mi_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim mi As MenuItem = CType(sender, MenuItem)
        SelectedColor = Color.FromKnownColor(mi.Tag)
        Me.Refresh()
    End Sub
 
End Class

Open in new window

CircleDemo4.jpg
0
 
NerdsOfTechTechnology ScientistAuthor Commented:
WOW! THAT is stellar! Now that... is extremely cool.
0
 
NerdsOfTechTechnology ScientistAuthor Commented:
Your welcome. You deserved it.

=NerdsOfTech
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now