Draw pixel 2: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
(Corrected link to Draw a pixel task) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(15 intermediate revisions by 11 users not shown) | |||
Line 8:
::# the position of the pixel is random
=={{header|Ada}}==
{{libheader|SDLAda}}
<syntaxhighlight lang="ada">with Ada.Numerics.Discrete_Random;
with SDL.Video.Windows.Makers;
with SDL.Video.Renderers.Makers;
with SDL.Events.Events;
procedure Draw_A_Pixel_2 is
Width : constant := 640;
Height : constant := 480;
use SDL.C;
subtype Width_Range is SDL.C.int range 0 .. Width - 1;
subtype Height_Range is SDL.C.int range 0 .. Height - 1;
package Random_Width is new Ada.Numerics.Discrete_Random (Width_Range);
package Random_Height is new Ada.Numerics.Discrete_Random (Height_Range);
Width_Gen : Random_Width.Generator;
Height_Gen : Random_Height.Generator;
Window : SDL.Video.Windows.Window;
Renderer : SDL.Video.Renderers.Renderer;
procedure Wait is
use type SDL.Events.Event_Types;
Event : SDL.Events.Events.Events;
begin
loop
while SDL.Events.Events.Poll (Event) loop
if Event.Common.Event_Type = SDL.Events.Quit then
return;
end if;
end loop;
delay 0.100;
end loop;
end Wait;
begin
if not SDL.Initialise (Flags => SDL.Enable_Screen) then
return;
end if;
Random_Width.Reset (Width_Gen);
Random_Height.Reset (Height_Gen);
SDL.Video.Windows.Makers.Create (Win => Window,
Title => "Draw a pixel 2",
Position => SDL.Natural_Coordinates'(X => 10, Y => 10),
Size => SDL.Positive_Sizes'(Width, Height),
Flags => 0);
SDL.Video.Renderers.Makers.Create (Renderer, Window.Get_Surface);
Renderer.Set_Draw_Colour ((0, 0, 0, 255));
Renderer.Fill (Rectangle => (0, 0, Width, Height));
Renderer.Set_Draw_Colour ((255, 255, 0, 255));
Renderer.Draw (Point => (X => Random_Width.Random (Width_Gen),
Y => Random_Height.Random (Height_Gen)));
Window.Update_Surface;
Wait;
Window.Finalize;
SDL.Finalise;
end Draw_A_Pixel_2;</syntaxhighlight>
=={{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">
#include<graphics.h>
#include<stdlib.h>
Line 28 ⟶ 90:
return 0;
}
</syntaxhighlight>
=={{header|Delphi}}==
{{libheader| Windows}}
{{libheader| Messages}}
{{libheader| SysUtils}}
<syntaxhighlight lang="delphi">
program Draw_a_pixel2;
{$APPTYPE CONSOLE}
uses
Windows,
Messages,
SysUtils;
const
WIN_WIDTH = 640;
WIN_HEIGHT = 480;
var
Msg: TMSG;
LWndClass: TWndClass;
hMainHandle: HWND;
procedure Paint(Handle: hWnd); forward;
procedure ReleaseResources;
begin
PostQuitMessage(0);
end;
function WindowProc(hWnd, Msg: Longint; wParam: wParam; lParam: lParam): Longint; stdcall;
begin
case Msg of
WM_PAINT:
Paint(hWnd);
WM_DESTROY:
ReleaseResources;
end;
Result := DefWindowProc(hWnd, Msg, wParam, lParam);
end;
procedure CreateWin(W, H: Integer);
var
Title: string;
begin
LWndClass.hInstance := hInstance;
with LWndClass do
begin
lpszClassName := 'OneYellowPixel';
Style := CS_PARENTDC or CS_BYTEALIGNCLIENT;
hIcon := LoadIcon(hInstance, 'MAINICON');
lpfnWndProc := @WindowProc;
hbrBackground := COLOR_BTNFACE + 1;
hCursor := LoadCursor(0, IDC_ARROW);
end;
Title := Format('Draw a YELLOW pixel random position in [%d, %d] ', [WIN_WIDTH,
WIN_HEIGHT]);
RegisterClass(LWndClass);
hMainHandle := CreateWindow(LWndClass.lpszClassName, Pchar(Title), WS_CAPTION
or WS_MINIMIZEBOX or WS_SYSMENU or WS_VISIBLE, ((GetSystemMetrics(SM_CXSCREEN)
- W) div 2), ((GetSystemMetrics(SM_CYSCREEN) - H) div 2), W, H, 0, 0, hInstance, nil);
end;
procedure ShowModal;
begin
while GetMessage(Msg, 0, 0, 0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
procedure Paint(Handle: hWnd);
var
ps: PAINTSTRUCT;
Dc: HDC;
begin
Dc := BeginPaint(Handle, ps);
// Fill bg with black
FillRect(Dc, ps.rcPaint, CreateSolidBrush($0));
// Do the magic
SetPixel(Dc, Random(WIN_WIDTH), Random(WIN_HEIGHT), $FFFF);
EndPaint(Handle, ps);
end;
begin
Randomize;
CreateWin(WIN_WIDTH, WIN_HEIGHT);
ShowModal();
end.</syntaxhighlight>
=={{header|Factor}}==
{{works with|Factor|0.99 development release 2019-07-10}}
<
640 480 2dup "random yellow pixel" init-window [ random ] bi@
Line 39 ⟶ 195:
begin-drawing BLACK clear-background 2dup YELLOW draw-pixel
end-drawing
] until close-window</
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
' or: fbc -s gui
Line 63 ⟶ 219:
WindowTitle "0, 0 is top left, pixel is at " & x & ", " & y & " hit any key to end program"
Sleep
End</
=={{header|Go}}==
<
import (
Line 104 ⟶ 260:
}
}
}</
{{out}}
Line 112 ⟶ 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}}==
<
let canvas = document.createElement("canvas");
canvas.width = 640;
Line 124 ⟶ 285:
let y = Math.random() * 481;
ctx.fillRect(x, y, 1, 1);
</syntaxhighlight>
=={{header|Julia}}==
<
const can = @GtkCanvas()
Line 149 ⟶ 310:
signal_connect(endit, win, :destroy)
wait(cond)
</syntaxhighlight>
=={{header|Kotlin}}==
This is a variation of the [[Draw a pixel]] task and so therefore is the code to accomplish it.
<
import java.awt.Color
Line 191 ⟶ 352:
}
}
}</
{{output}}
Line 198 ⟶ 359:
The color of the pixel at (296, 15) is yellow
</pre>
=={{header|Nim}}==
{{libheader|SDL2}}
<syntaxhighlight lang="nim">import random
import sdl2
discard sdl2.init(INIT_EVERYTHING)
let window = createWindow("Pixel", 300, 300, 640, 480, SDL_WINDOW_SHOWN)
let renderer = createRenderer(window, -1, Renderer_Accelerated)
randomize()
renderer.clear()
renderer.setDrawColor((255u8, 255u8, 0u8, 0u8))
let x = rand(639)
let y = rand(479)
renderer.drawPoint(x.cint, y.cint)
renderer.present()
delay(5000)</syntaxhighlight>
=={{header|Objeck}}==
<syntaxhighlight lang="objeck">use Game.SDL2;
use Game.Framework;
class DrawPixel {
@framework : GameFramework;
function : Main(args : String[]) ~ Nil {
DrawPixel->New()->Run();
}
New() {
@framework := GameFramework->New(640, 480, "RGB");
@framework->SetClearColor(Color->New(0, 0, 0));
}
method : Run() ~ Nil {
if(@framework->IsOk()) {
e := @framework->GetEvent();
x := Int->Random(640);
y := Int->Random(480);
quit := false;
while(<>quit) {
@framework->FrameStart();
@framework->Clear();
# process input
while(e->Poll() <> 0) {
if(e->GetType() = EventType->SDL_QUIT) {
quit := true;
};
};
@framework->GetRenderer()->SetDrawColor(255, 255, 0, 0);
@framework->GetRenderer()->DrawPoint(x, y);
@framework->Show();
@framework->FrameEnd();
};
}
else {
"--- Error Initializing Game Environment ---"->ErrorLine();
return;
};
leaving {
@framework->Quit();
};
}
}</syntaxhighlight>
=={{header|Perl}}==
{{libheader|Gtk3}}
<
my $width = 640;
Line 226 ⟶ 460:
$cr->rectangle( int rand $width , int rand $height, 1, 1);
$cr->stroke;
}</
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
Pixel jumps around randomly, twice a second.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Draw_pixel_2.exw
--</span>
<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>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span>
<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>
<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>
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<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>
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<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;">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>
<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>
<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>
<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}}==
<syntaxhighlight lang="java">
//Aamrun, 26th June 2022
size(640,480);
stroke(#ffff00);
ellipse(random(640),random(480),1,1);
</syntaxhighlight>
=={{header|Python}}==
{{works with|Python|2.7}}
{{libheader|Tkinter}}
{{libheader|random}}
<syntaxhighlight lang="python">import Tkinter,random
def draw_pixel_2 ( sizex=640,sizey=480 ):
pos = random.randint( 0,sizex-1 ),random.randint( 0,sizey-1 )
root = Tkinter.Tk()
can = Tkinter.Canvas( root,width=sizex,height=sizey,bg='black' )
can.create_rectangle( pos*2,outline='yellow' )
can.pack()
root.title('press ESCAPE to quit')
root.bind('<Escape>',lambda e : root.quit())
root.mainloop()
draw_pixel_2()</syntaxhighlight>
=={{header|Racket}}==
<
(define WIDTH 640)
Line 326 ⟶ 564:
(send dc draw-point (random WIDTH) (random HEIGHT)))])
(send frame show #t)</
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.05}}
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" line>use GTK::Simple;
use GTK::Simple::DrawingArea;
use Cairo;
my ($w, $h) = 640, 480;
my ($x, $y) = (^$w).pick, (^$h).pick;
my $app = GTK::Simple::App.new(:title("Draw Pixel 2 @ $x,$y"));
my $da = GTK::Simple::DrawingArea.new;
gtk_simple_use_cairo;
$app.set-content( $da );
$app.border-width = 5;
$da.size-request($w, $h);
sub rect-do( $d, $ctx ) {
given $ctx {
.rgb(0, 0, 0);
.rectangle(0, 0, $w, $h);
.fill;
.rgb(1, 1, 0);
.rectangle($x, $y, 1, 1);
.fill;
}
}
my $ctx = $da.add-draw-handler( &rect-do );
$app.run;</syntaxhighlight>
=={{header|Ring}}==
<
load "guilib.ring"
Line 389 ⟶ 660:
see "green : " + mycolor.green() + nl
see "blue : " + mycolor.blue() + nl
}</
Outputimage:
[https://www.dropbox.com/s/n31ppq8cu9ipru8/CalmoSoftPixelColor.jpg?dl=0 Draw pixel 2]
=={{header|Scala}}==
===Java Swing Interoperability===
Line 401 ⟶ 673:
{{libheader|Scastie qualified}}
{{works with|Scala|2.13}}
<
import java.awt.Color
import scala.language.reflectiveCalls
Line 440 ⟶ 712:
println("Tests successfully completed with no errors found.")
}</
=={{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}}
<syntaxhighlight lang="wren">import "dome" for Window
import "graphics" for Canvas, Color
import "random" for Random
class Game {
static init() {
Window.title = "Draw a pixel 2"
Window.resize(320, 240)
Canvas.resize(320, 240)
var yellow = Color.rgb(255, 255, 0)
var rand = Random.new()
var randX = rand.int(320)
var randY = rand.int(240)
Canvas.pset(randX, randY, yellow)
}
static update() {}
static draw(dt) {}
static getRGB(col) { "{%(col.r), %(col.g), %(col.b)}" }
}</syntaxhighlight>
=={{header|XPL0}}==
This is all that's required on the Raspberry Pi version of XPL0.
<
|