Draw pixel 2: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(6 intermediate revisions by 5 users not shown)
Line 10:
=={{header|Ada}}==
{{libheader|SDLAda}}
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Discrete_Random;
 
with SDL.Video.Windows.Makers;
Line 69:
Window.Finalize;
SDL.Finalise;
end Draw_A_Pixel_2;</langsyntaxhighlight>
 
=={{header|C}}==
Same as the [[Draw_a_pixel]] task, uses the random number functions of stdlib.h to plot a random point. Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.
<syntaxhighlight lang="c">
<lang C>
#include<graphics.h>
#include<stdlib.h>
Line 90:
return 0;
}
</syntaxhighlight>
</lang>
=={{header|Delphi}}==
{{libheader| Windows}}
{{libheader| Messages}}
{{libheader| SysUtils}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Draw_a_pixel2;
 
Line 185:
CreateWin(WIN_WIDTH, WIN_HEIGHT);
ShowModal();
end.</langsyntaxhighlight>
=={{header|Factor}}==
{{works with|Factor|0.99 development release 2019-07-10}}
<langsyntaxhighlight lang="factor">USING: kernel random raylib.ffi ;
 
640 480 2dup "random yellow pixel" init-window [ random ] bi@
Line 195:
begin-drawing BLACK clear-background 2dup YELLOW draw-pixel
end-drawing
] until close-window</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 04-07-2018
' compile with: fbc -s console
' or: fbc -s gui
Line 219:
WindowTitle "0, 0 is top left, pixel is at " & x & ", " & y & " hit any key to end program"
Sleep
End</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 260:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 268:
</pre>
 
=={{header|J}}==
See also [[Draw_a_pixel#J|Draw a pixel]]. Note that this pixel will be nearly invisible on most screens. There's very low contrast between yellow and white, and single pixels may be difficult to see on "modern" hardware. Remedying these visual problems would require going outside the requirements of the task. You might change the color, for example to black with <code>glrgb 0 0 0</code> and/or changing the radius to include more pixels, for example 8 with <code>glpen 8 1</code><syntaxhighlight lang="j">require 'gl2'
coinsert 'jgl2'
wd{{)n pc Rosetta closeok;cc task isidraw; set task wh 640 480;pshow}}
glpaint glpixel ?640 480 [ glpen 1 1 [ glrgb 255 255 0 [ glclear ''</syntaxhighlight>
=={{header|javascript}}==
<langsyntaxhighlight lang="javascript">let w = window.open("", "", "width=640,height=480");
let canvas = document.createElement("canvas");
canvas.width = 640;
Line 280 ⟶ 285:
let y = Math.random() * 481;
ctx.fillRect(x, y, 1, 1);
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Gtk, Graphics
 
const can = @GtkCanvas()
Line 305 ⟶ 310:
signal_connect(endit, win, :destroy)
wait(cond)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
This is a variation of the [[Draw a pixel]] task and so therefore is the code to accomplish it.
<langsyntaxhighlight lang="scala">// Version 1.2.41
 
import java.awt.Color
Line 347 ⟶ 352:
}
}
}</langsyntaxhighlight>
 
{{output}}
Line 357 ⟶ 362:
=={{header|Nim}}==
{{libheader|SDL2}}
<langsyntaxhighlight Nimlang="nim">import random
import sdl2
 
Line 373 ⟶ 378:
renderer.present()
 
delay(5000)</langsyntaxhighlight>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">use Game.SDL2;
use Game.Framework;
 
Line 426 ⟶ 431:
};
}
}</langsyntaxhighlight>
 
=={{header|Perl}}==
{{libheader|Gtk3}}
<langsyntaxhighlight lang="perl">use Gtk3 '-init';
 
my $width = 640;
Line 455 ⟶ 460:
$cr->rectangle( int rand $width , int rand $height, 1, 1);
$cr->stroke;
}</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
Pixel jumps around randomly, twice a second.
Resize the window to see the pixel jumping about.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include pGUI.e
<span style="color: #000080;font-style:italic;">--
 
-- demo\rosetta\Draw_pixel_2.exw
Ihandle dlg, canvas
--</span>
cdCanvas cddbuffer, cdcanvas
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
 
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
atom {w,h} = IupGetIntInt(canvas, "DRAWSIZE")
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span>
cdCanvasActivate(cddbuffer)
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span>
cdCanvasClear(cddbuffer)
cdCanvasPixel(cddbuffer, rand(w), rand(h), CD_YELLOW)
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
cdCanvasFlush(cddbuffer)
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">)</span>
return IUP_DEFAULT
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
 
<span style="color: #7060A8;">cdCanvasPixel</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">),</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">),</span> <span style="color: #004600;">CD_YELLOW</span><span style="color: #0000FF;">)</span>
function map_cb(Ihandle ih)
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
cdcanvas = cdCreateCanvas(CD_IUP, ih)
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
cdCanvasSetBackground(cddbuffer, CD_BLACK)
return IUP_DEFAULT
<span style="color: #008080;">function</span> <span style="color: #000000;">map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
 
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
procedure main()
<span style="color: #7060A8;">cdCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_BLACK</span><span style="color: #0000FF;">)</span>
IupOpen()
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">timer_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupUpdate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_IGNORE</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=240x250"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallbacks</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"map_cb"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">)})</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Draw pixel"`</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">timer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupTimer</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"timer_cb"</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">500</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (2 fps)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|Processing}}==
canvas = IupCanvas(NULL)
<syntaxhighlight lang="java">
IupSetAttribute(canvas, "RASTERSIZE", "240x50")
//Aamrun, 26th June 2022
IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
 
size(640,480);
dlg = IupDialog(canvas)
IupSetAttribute(dlg, "TITLE", "Draw pixel")
 
stroke(#ffff00);
IupShow(dlg)
IupSetAttribute(canvas, "RASTERSIZE", NULL)
IupMainLoop()
IupClose()
end procedure
 
ellipse(random(640),random(480),1,1);
main()</lang>
</syntaxhighlight>
 
=={{header|Python}}==
Line 504 ⟶ 529:
{{libheader|Tkinter}}
{{libheader|random}}
<langsyntaxhighlight lang="python">import Tkinter,random
def draw_pixel_2 ( sizex=640,sizey=480 ):
pos = random.randint( 0,sizex-1 ),random.randint( 0,sizey-1 )
Line 515 ⟶ 540:
root.mainloop()
 
draw_pixel_2()</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket/gui
 
(define WIDTH 640)
Line 539 ⟶ 564:
(send dc draw-point (random WIDTH) (random HEIGHT)))])
 
(send frame show #t)</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 546 ⟶ 571:
Coordinates of random pixel displayed in window title. To make the single pixel show up better I filled in the drawing area background with black to get better contrast.
 
<syntaxhighlight lang="raku" perl6line>use GTK::Simple;
use GTK::Simple::DrawingArea;
use Cairo;
Line 572 ⟶ 597:
 
my $ctx = $da.add-draw-handler( &rect-do );
$app.run;</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring"># Project : Draw pixel 2
 
load "guilib.ring"
Line 635 ⟶ 660:
see "green : " + mycolor.green() + nl
see "blue : " + mycolor.blue() + nl
}</langsyntaxhighlight>
Outputimage:
Line 648 ⟶ 673:
{{libheader|Scastie qualified}}
{{works with|Scala|2.13}}
<langsyntaxhighlight Scalalang="scala">import java.awt.image.BufferedImage
import java.awt.Color
import scala.language.reflectiveCalls
Line 687 ⟶ 712:
println("Tests successfully completed with no errors found.")
 
}</langsyntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Zig">
import gg
import gx
import rand
 
const (
win_width = 640
win_height = 480
x = rand.int_in_range(1, win_width) or {exit(-1)}
y = rand.int_in_range(1, win_height) or {exit(-1)}
)
 
fn main() {
mut app := gg.new_context(
width: win_width
height: win_height
frame_fn: frame
window_title: 'Pixel 2'
)
app.run()
}
 
fn frame(mut ctx gg.Context) {
ctx.begin()
ctx.draw_pixel(x, y, gx.yellow)
ctx.end()
}
</syntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
<langsyntaxhighlight ecmascriptlang="wren">import "dome" for Window
import "graphics" for Canvas, Color
import "random" for Random
Line 712 ⟶ 767:
 
static getRGB(col) { "{%(col.r), %(col.g), %(col.b)}" }
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
This is all that's required on the Raspberry Pi version of XPL0.
<langsyntaxhighlight XPL0lang="xpl0">[SetFB(640, 480, 24); Point(Ran(640), Ran(480), $FFFF00)]</langsyntaxhighlight>
9,476

edits