gradient with drawline

fotoeye75

New member
Joined
Oct 28, 2007
Messages
2
Programming Experience
Beginner
I know this is extremly easy but I have been messing with it for 2 days and Im going to need help on this one.
I'm suppose to re-create this splash screen by using drawline and a loop structure.
drawline.jpg

The lines need to be going vertical not horizontal.

I have the gradient figured out but I have 3 main problems. First my fill area is going the wrong way. Second I can't get the second color to fill opposite to the green. Third, I can't get the fill to be solid. I can get a solid gradient but only by using a larger pen which wont create the same smoothness as what I need.
I think Im close but ... I've went through 2 packs of marlboro and 4 liters of dr.pepper and its still not working. Here's the code I have so far.

VB.NET:
Option Strict On
Option Explicit On 
Imports System.Convert

Public Class frmSplash
    Inherits System.Windows.Forms.Form

    Private Sub frmSplash_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim graCurrent As Graphics = e.Graphics
        Dim recCurrent As Rectangle, colCurrent As Color
        Dim sbCurrent As SolidBrush, penCurrent As Pen

        Dim pintXCurrent As Integer = ToInt32(Me.Width)
        Dim pintYCurrent As Integer = ToInt32(Me.Height)

        'fill the background black
        recCurrent = New Rectangle(0, 0, Me.Width, Me.Height)
        sbCurrent = New SolidBrush(Color.Black)
        graCurrent.FillRectangle(sbCurrent, recCurrent)

        Dim pintColorLoop As Integer = 1
        pintYCurrent = 0
        For pintXCurrent = 0 To Me.Width Step ToInt32(Me.Width / 100)
            'If structure prevents alpha from exceeding its limits when 
            ' creating the gradiant
            If pintYCurrent < 255 Then
                penCurrent = New Pen(colCurrent.FromArgb(pintYCurrent, 0, 255, 0), 5)
            Else
                penCurrent = New Pen(colCurrent.FromArgb(255, 0, 255, 0), 5)
            End If
            'Draw Lines
            graCurrent.DrawLine(penCurrent, pintXCurrent, 0, pintXCurrent, pintYCurrent)
            pintYCurrent = ToInt32(pintYCurrent + (Me.Height / 100))
        Next
    End Sub
End Class
 
It is not difficult to do with FillPolygon (or FillPath) and LinearGradientBrush, why do you want to use DrawLine?
 
Its for an assignment and I have to use drawline .. that's what is bugging me about this .. I finished the actual program in no time and have been spending all this time coding a splash page that looks tacky and outdated using drawline. I know I could do it faster and better other ways but thats the assignment and for some reason they feel the splash page of a financial application is worth 25% of the grade ... I know its stupid.
 
Um, can someone please explain what this does?
 
Here's code to draw the green part:
VB.NET:
Using p As New Pen(Color.Black)
    Dim colorIncrement As Double = 255 / Me.Panel1.Width
    Dim heightIncrement As Double = Me.Panel1.Height / Me.Panel1.Width

    For x As Integer = 0 To Me.Panel1.Width - 1 Step 1
        p.Color = Color.FromArgb(0, _
                                 CInt(Math.Floor(x * colorIncrement)), _
                                 0)
        e.Graphics.DrawLine(p, _
                            x, _
                            0, _
                            x, _
                            Me.Panel1.Height - CInt(Math.Floor(x * heightIncrement)))
    Next x
End Using
I'll leave the blue part to you.
 
Its for an assignment and I have to use drawline .. that's what is bugging me about this .. I finished the actual program in no time and have been spending all this time coding a splash page that looks tacky and outdated using drawline. I know I could do it faster and better other ways but thats the assignment and for some reason they feel the splash page of a financial application is worth 25% of the grade ... I know its stupid.
It's not stupid at all. The point of learning exercises is to learn, not create useful things. If you are unable to interpret that image as lines and colours and implement code to create it using lines and colours then you may not have the analytical skill to find solutions to complex problems.
 
"analyitical skill" I like that!

Would anyone know how to make that triangle move around? Either on debugging or via keyboard button?
 
Yes they would. The code tells the Graphics object where to draw the lines. If you want the drawing to "move" then you would tell the Graphics object to draw the lines in a different place.
 
I should of ask this, you used the help files to build the triangle, and msdn pages?
 
I'm assuming that this assignment is over with and I'm not doing anyone's homework for them. Here's the full code to draw that design on a form:
VB.NET:
Private xOrigin As Integer = 10
Private yOrigin As Integer = 20
Private xSize As Integer = 200
Private ySize As Integer = 150

Private Sub Form1_Paint(ByVal sender As Object, _
                        ByVal e As PaintEventArgs) Handles Me.Paint
    Using p As New Pen(Color.Black)
        Dim colorIncrement As Double = 255 / xSize
        Dim heightIncrement As Double = ySize / xSize

        Dim xOffset As Integer
        Dim yOffset As Integer

        For increment As Integer = 0 To xSize - 1 Step 1
            xOffset = xOrigin + increment
            yOffset = yOrigin + ySize

            p.Color = Color.FromArgb(0, _
                                     CInt(Math.Floor(increment * colorIncrement)), _
                                     0)
            e.Graphics.DrawLine(p, _
                                xOffset, _
                                yOrigin, _
                                xOffset, _
                                yOffset - CInt(Math.Floor(increment * heightIncrement)))

            p.Color = Color.FromArgb(0, _
                                     0, _
                                     255 - CInt(Math.Floor(increment * colorIncrement)))
            e.Graphics.DrawLine(p, _
                                xOffset, _
                                yOffset, _
                                xOffset, _
                                yOffset - CInt(Math.Floor(increment * heightIncrement)))
        Next increment
    End Using
End Sub
The position and size of the drawing are controlled by the member variables. If you wanted to animate the drawing you'd edit those variables and then force the form to repaint by calling Refresh or, preferably, Invalidate and Update, e.g.
VB.NET:
Private Sub Button1_Click(ByVal sender As Object, _
                          ByVal e As EventArgs) Handles Button1.Click
    Dim n As Integer

    n = Me.xOrigin
    Me.xOrigin = Me.yOrigin
    Me.yOrigin = n

    n = Me.xSize
    Me.xSize = Me.ySize
    Me.ySize = n

    Dim x As Integer = Me.xOrigin - 1
    Dim y As Integer = Me.yOrigin - 1
    Dim width As Integer = Me.xSize + 2
    Dim height As Integer = Me.ySize + 2

    Me.Invalidate(New Rectangle(x, _
                                y, _
                                width, _
                                height))
    Me.Invalidate(New Rectangle(y, _
                                x, _
                                height, _
                                width))
    Me.Update()
End Sub
 
Where you speaking to me?

If you were, I'm not here hwk. I'm just taking a small interest in programming. Btw I'm not even in a course or anything.
 
Where you speaking to me?

If you were, I'm not here hwk. I'm just taking a small interest in programming. Btw I'm not even in a course or anything.
The original poster said this was an assignment. I'm saying that I hope I'm not doing someone's assignment for them by providing you with the code to do this.
 
Back
Top