Draw pixel 2: Difference between revisions
Content added Content deleted
(Added Kotlin) |
No edit summary |
||
Line 56: | Line 56: | ||
The color of the pixel at (296, 15) is yellow |
The color of the pixel at (296, 15) is yellow |
||
</pre> |
</pre> |
||
=={{header|Ring}}== |
|||
<lang ring> |
|||
# Project : Draw pixel 2 |
|||
# Date : 2018/05/16 |
|||
# Author : Gal Zsolt (~ CalmoSoft ~) |
|||
# 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: |
|||
[https://www.dropbox.com/s/n31ppq8cu9ipru8/CalmoSoftPixelColor.jpg?dl=0 Draw pixel 2] |
Revision as of 03:30, 12 June 2018
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
Create a window and draw a pixel in it, subject to the following:
- the window is 640 x 480
- the color of the pixel must be yellow (255,255,0)
- the position of the pixel is random
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>
- Project : Draw pixel 2
- Date : 2018/05/16
- Author : Gal Zsolt (~ CalmoSoft ~)
- 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: