Draw a pixel

From Rosetta Code
Task
Draw a pixel
You are encouraged to solve this task according to the task description, using any language you may know.
Task

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

  1.  the window is 320 x 240
  2.  the color of the pixel must be red (255,0,0)
  3.  the position of the pixel is x = 100, y = 100


Kotlin[edit]

This task seems very similar to the Bitmap task and so therefore is the code to accomplish it.

// Version 1.2.41
 
import java.awt.Color
import java.awt.Graphics
import java.awt.image.BufferedImage
 
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 bbs = BasicBitmapStorage(320, 240)
with (bbs) {
fill(Color.white) // say
setPixel(100, 100, Color.red)
// check it worked
val c = getPixel(100, 100)
print("The color of the pixel at (100, 100) is ")
println(if (c == Color.red) "red" else "white")
}
}
Output:
The color of the pixel at (100, 100) is red

Python[edit]

Works with: Python version 2.7.14
Library: PIL
from PIL import Image
 
img = Image.new('RGB', (320, 240))
pixels = img.load()
pixels[100,100] = (255,0,0)
img.show()
 

Racket[edit]

#lang racket
(require racket/draw)
(let ((b (make-object bitmap% 320 240)))
(send b set-argb-pixels 100 100 1 1 (bytes 255 0 0 255))
b)

Ring[edit]

 
# Project  : Draw a pixel
# Date  : 2018/05/16
# Author  : Gal Zsolt (~ CalmoSoft ~)
# Email  : <[email protected]>
 
load "guilib.ring"
 
new qapp {
win1 = new qwidget() {
setwindowtitle("Drawing Pixels")
setgeometry(100,100,320,240)
label1 = new qlabel(win1) {
setgeometry(10,10,300,200)
settext("")
}
new qpushbutton(win1) {
setgeometry(200,200,100,30)
settext("draw")
setclickevent("draw()")
}
show()
}
exec()
}
 
func draw()
p1 = new qpicture()
color = new qcolor() {
setrgb(255,0,0,255)
}
pen = new qpen() {
setcolor(color)
setwidth(5)
}
new qpainter() {
begin(p1)
setpen(pen)
drawpoint(100,100)
endpaint()
}
label1 { setpicture(p1) show() }
 

Output image:

Draw a pixel