Draw pixel 2

Revision as of 12:24, 12 June 2018 by Aamrun (talk | contribs) (Added C implementation)

Create a window and draw a pixel in it, subject to the following:

Draw pixel 2 is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task
  1.  the window is 640 x 480
  2.  the color of the pixel must be yellow (255,255,0)
  3.  the position of the pixel is random


C

Same as the Draw a pixel task, uses the random number functions of stdlib.h to plot a random point. Requires the WinBGIm library. <lang C> /*Abhishek Ghosh, 12th June 2018*/

  1. include<graphics.h>
  2. include<stdlib.h>
  3. include<time.h>

int main() { srand(time(NULL));

initwindow(640,480,"Red Pixel");

putpixel(rand()%640,rand()%480,YELLOW);

getch();

return 0; } </lang>

Kotlin

This is a variation of the Draw a pixel task and so therefore is the code to accomplish it. <lang scala>// Version 1.2.41

import java.awt.Color import java.awt.Graphics import java.awt.image.BufferedImage import java.util.Random

class BasicBitmapStorage(width: Int, height: Int) {

   val image = BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR)
   fun fill(c: Color) {
       val g = image.graphics
       g.color = c
       g.fillRect(0, 0, image.width, image.height)
   }
   fun setPixel(x: Int, y: Int, c: Color) = image.setRGB(x, y, c.getRGB())
   fun getPixel(x: Int, y: Int) = Color(image.getRGB(x, y))

}

fun main(args: Array<String>) {

   val rand = Random()
   val bbs = BasicBitmapStorage(640, 480)
   with (bbs) {
       fill(Color.white) // say
       val x = rand.nextInt(image.width)
       val y = rand.nextInt(image.height)
       setPixel(x, y, Color.yellow)
       // check there's exactly one random yellow pixel
       for (i in 0 until image.width) {
           for (j in 0 until image.height) {
               if (getPixel(i, j) == Color.yellow) {
                   println("The color of the pixel at ($i, $j) is yellow")
               }
           }
       }
   }

}</lang>

Output:

Sample output:

The color of the pixel at (296, 15) is yellow

Ring

<lang ring>

  1. Project  : Draw pixel 2
  2. Date  : 2018/06/12
  3. Author  : Gal Zsolt (~ CalmoSoft ~)
  4. Email  : <calmosoft@gmail.com>

load "guilib.ring"

new qapp {

      nwidth = 320
      nheight= 240
      win1 = new qwidget() {
                 setwindowtitle("Draw Pixel 2")
                 setgeometry(100,100,640,480)
                 label1 = new qlabel(win1) {
                             setgeometry(10,10,640,480)
                             settext("")
                 }
                 new qpushbutton(win1) {
                        setgeometry(200,400,100,30)
                        settext("draw")
                        setclickevent("draw()")
                 }
                 new qpushbutton(win1) {
                        setgeometry(300,400,100,30)
                        settext("get pixel color")
                        setclickevent("PixelColor()")
                 }
                 show()
      }
      exec()

}

func draw()

       p1 = new qpicture()
       color = new qcolor() {
                  setrgb(255,255,0,255)
                 }
       pen = new qpen() {
                 setcolor(color)
                 setwidth(10)
                 }
       new qpainter() {
              begin(p1)
              setpen(pen)
              x = random(nwidth-1) + 1
              y = random(nheight-1) + 1
              see "x = " + x + " y = " + y + nl
              drawpoint(x,y)
              endpaint()
              }
              label1 { setpicture(p1) show() }

func PixelColor()

      oapp = new qapp(0,null)  {
                 screen = win1.windowhandle().screen()
                 pixmap = screen.grabwindow(0,0,0,-1,-1)
                 image = pixmap.toimage()
                 color = image.pixel(100,100)
                 mycolor = new qcolor()
                 mycolor.setrgb(255,255,0,255)
                 see nl+"red : " + mycolor.red() + nl
                 see "green : " + mycolor.green() + nl
                 see "blue : " + mycolor.blue() + nl
                 }

</lang> Outputimage:

Draw pixel 2