Link to home
Start Free TrialLog in
Avatar of hahahahahaha
hahahahahaha

asked on

draw a pixel

Hi,
I have a form in which I have a button . When I click on that button , I want to draw a pixel in that form with definite co-ordinate . How can I do that ?
ASKER CERTIFIED SOLUTION
Avatar of RealMrTea
RealMrTea

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RealMrTea
RealMrTea

I should also point out that you could use any shape you want...

A g.FillEllipse(Brushes.Blue, pc.X, pc.Y, 3, 3) Looks a little more like a single point.

Enjoy,
Eric
Avatar of hahahahahaha

ASKER

Public Class DDA
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'DDA
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(472, 358)
        Me.Name = "DDA"
        Me.Text = "DDA"

    End Sub

#End Region
    Dim xa As Integer
    Dim ya As Integer
    Dim xb As Integer
    Dim yb As Integer
    Dim count As Integer
    Dim x, y As Double
    Dim collection As New collection
    Dim p As New Point

    Private Sub DDA_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        If count Mod 2 = 0 Then
            xa = Me.MousePosition.X
            ya = Me.MousePosition.Y
        Else
            xb = Me.MousePosition.X
            yb = Me.MousePosition.Y
            LineDDA(xa, ya, xb, yb)
        End If
        count += 1
    End Sub

    Private Sub DDA_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        count = 0
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        For Each p In collection
            e.Graphics.DrawRectangle(New Pen(Color.Blue), p.X, p.Y, 1, 1)
        Next
    End Sub

    Private Sub LineDDA(ByVal xa As Integer, ByVal ya As Integer, ByVal xb As Integer, ByVal yb As Integer)
        Dim dx As Integer
        Dim dy As Integer
        Dim steps As Integer
        Dim k As Integer
        Dim xIner, yIner As Double

        dx = xb - xa
        dy = yb - ya
        x = xa
        y = ya
        If Math.Abs(dx) > Math.Abs(dy) Then
            steps = Math.Abs(dx)
        Else
            steps = Math.Abs(dy)
        End If
        xIner = dx / (steps * 1.0)
        yIner = dy / (steps * 1.0)
        p.X = Math.Round(x)
        p.Y = Math.Round(y)
        collection.Add(p)
        For k = 1 To steps
            x += xIner
            y += yIner
            p.X = Math.Round(x)
            p.Y = Math.Round(y)
            collection.Add(p)
        Next
        Me.Refresh()
    End Sub
End Class


I cant understand why it isn't drawn with correct position . And I want to draw a set of pixels , not a set of Rectangles or Ellipses .
In your MouseDown Event you need to get the points in the controls coordinates.  They will always be offset if you do it like that.

Dim pClient As Point = Me.PointToClient(Cursor.Position)
xa = pClient.X
ya = pClient.Y

etc...

You may choice to do this on the OnPaint event so they will always be correct even if the form is moved.

That will fix your offset problem,
Eric
>> A g.FillEllipse(Brushes.Blue, pc.X, pc.Y, 3, 3) Looks a little more like a single point.

We cant draw a pixel in VB.NET , right ? Your solution just is a trick , not actually a pixel . It's an Ellipse .
Yes.  There is no draw pixle.

Later,
ERic
your LineDDA procedure is setting values in a Collection (called collection), but then you never actuall draw anything on the form.  Me.Refresh does nothing in that procedure.  You need to use a Graphics context, and draw on that context, using GDI+

Me.Refresh does NOT force an OnPaint event.

try this change to your DDALine procedure:

        xIner = dx / (steps * 1.0)
        yIner = dy / (steps * 1.0)
        p.X = Math.Round(x)
        p.Y = Math.Round(y)
        collection.Add(p)
        For k = 1 To steps
            x += xIner
            y += yIner
            p.X = Math.Round(x)
            p.Y = Math.Round(y)
            collection.Add(p)
        Next
'++++++++++++++   New code here  draws the line, but not in the correct location
        Dim g As Graphics = Me.CreateGraphics()
        Dim mypen As New Pen(Color.Red)
        For i As Integer = 1 To collection.Count - 1
           Dim j As Integer = i + 1
           Dim p1 As Point, p2 As Point
           p1 = collection.Item(i)
           p2 = collection.Item(j)
           g.DrawLine(mypen, p1.X, p1.Y, p2.X, p2.Y)
        Next


this will at least now get something drawn on the screen.  It will be up to you to figfure out what it is supposed to do, from here.

AW