FreeBasic: Grafik Beispiel
Aus Wikibooks
Natürlich sollte man bei ernsten Projekten Oberflächen auf keinen Fall mit den Grafikbefehlen erstellen, aber für den Wissensstand, den du aktuell haben solltest, ist das genau das Richtige. Du kannst es zur Übung auch noch erweitern. Mit "s" speichert man ab, das Bild landet im Ordner der .exe-Datei. Mit "e" wird das Programm beendet. Viel Spaß damit :). Auf älteren Computer wird es nur langsam bis gar nicht laufen. In diesem Fall ändere in Zeile 103 "sleep 10" zu "sleep 100" um. Es ist sicher nicht sauber programmiert, aber es soll ja auch nur zeigen, was die Grafikbefehle alles können.
dim xrand as integer=640 dim yrand as integer=480 WINDOWTITLE "Pset Bitmap" screen 19,24 Line (0,0)-(xrand+160,yrand+120),RGB(255,255,255),bf dim Taste2 as string dim Mauspos as string dim xpos as string dim ypos as string dim Radius as integer=50 dim key as string dim Pinsel as integer dim x1 as integer dim x2 as integer dim y1 as integer dim y2 as integer dim Taste as integer dim Rad as integer dim x as integer dim y as integer dim b1 as string dim a1 as string dim c1 as string dim b2 as string dim a2 as string dim c2 as string dim b3 as string dim a3 as string dim c3 as string dim Farbe3 as integer dim Farbe2 as integer dim Farbe1 as integer dim Farbe4 as integer dim Farbe5 as integer dim Farbe6 as integer dim Farbe7 as integer dim Farbe8 as integer dim Farbe9 as integer dim Rot as integer=0 dim Blau as integer=0 dim Gruen as integer=0 Dim As Integer breit,hoch Dim bild As Any Ptr dim Modus as integer=1 dim key2 as string dim Funktion as string dim Bildname as string="neues_Bild.bmp" dim Bildzwischenspeicher as string bild=IMAGECREATE(xrand,yrand,RGB(255,255,255)) line(xrand+1,0)-(xrand+1,yrand),2 line(xrand+2,0)-(xrand+2,yrand+1),2 line(0,yrand+1)-(xrand+1,yrand+1),2 line(0,yrand+2)-(xrand+2,yrand+2),2 line(9,yrand+19)-(101,yrand+111),2,b line(10,yrand+20)-(100,yrand+110),2,bf line(110,yrand+20)-(245,yrand+50),2,b line(110,yrand+35)-(245,yrand+35),2 line(125,yrand+20)-(125,yrand+50),2 line(140,yrand+20)-(140,yrand+50),2 line(155,yrand+20)-(155,yrand+50),2 line(170,yrand+20)-(170,yrand+50),2 line(185,yrand+20)-(185,yrand+50),2 line(200,yrand+20)-(200,yrand+50),2 line(215,yrand+20)-(215,yrand+50),2 line(230,yrand+20)-(230,yrand+50),2 line(245,yrand+20)-(245,yrand+50),2 line(111,yrand+36)-(124,yrand+49),RGB(255,255,255),bf line(111,yrand+21)-(124,yrand+34),RGB(0,0,0),bf line(126,yrand+36)-(139,yrand+49),RGB(200,200,200),bf line(126,yrand+21)-(139,yrand+34),RGB(100,100,100),bf line(141,yrand+21)-(154,yrand+34),RGB(255,0,0),bf line(141,yrand+36)-(154,yrand+49),RGB(255,60,80),bf line(156,yrand+21)-(169,yrand+34),RGB(0,255,0),bf line(156,yrand+36)-(169,yrand+49),RGB(154,255,0),bf line(171,yrand+21)-(184,yrand+34),RGB(0,0,255),bf line(171,yrand+36)-(184,yrand+49),RGB(0,168,255),bf line(186,yrand+21)-(199,yrand+34),RGB(255,255,0),bf line(186,yrand+36)-(199,yrand+49),RGB(255,255,150),bf line(201,yrand+21)-(214,yrand+34),RGB(255,170,0),bf line(201,yrand+36)-(214,yrand+49),RGB(255,190,200),bf line(216,yrand+21)-(229,yrand+34),RGB(255,0,255),bf line(216,yrand+36)-(229,yrand+49),RGB(255,120,255),bf line(231,yrand+21)-(244,yrand+34),RGB(0,255,255),bf line(231,yrand+36)-(244,yrand+49),RGB(150,255,255),bf line(260,yrand+20)-(400,yrand+110),2,b circle(270,yrand+30),5,2,,,1 circle(285,yrand+35),10,2,,,1 circle(310,yrand+40),15,2,,,1 circle(345,yrand+45),20,2,,,1 do do sleep 10 getmouse x,y,Rad,Taste key2 = inkey line(xrand,yrand)-(xrand+50,yrand+30),RGB(255,255,255),bf xpos=str(x) ypos=str(y) mauspos="x="+xpos draw string (xrand,yrand),mauspos,2 mauspos="y="+ypos draw string (xrand,yrand+12),mauspos,2 line(xrand+2,0)-(xrand+2,yrand+1),2 line(0,yrand+1)-(xrand+1,yrand+1),2 line(0,yrand+2)-(xrand+2,yrand+2),2 line(xrand+5,10)-(xrand+35,40),RGB(255,255,255),bf line(xrand+5,10)-(xrand+35,40),2,b circle(xrand+20,25),12,2,,,1 line(xrand+45,10)-(xrand+75,40),RGB(255,255,255),bf line(xrand+45,10)-(xrand+75,40),2,b line(xrand+50,35)-(xrand+70,15),2 line(xrand+85,10)-(xrand+115,40),2,b circle(xrand+100,25),15,RGB(255,0,0) circle(xrand+100,25),12,RGB(0,255,0),,,1 circle(xrand+100,25),9,RGB(0,0,255),,,1 line(xrand+125,10)-(xrand+155,40),RGB(255,255,255),bf line(xrand+125,10)-(xrand+155,40),2,b line(xrand+130,15)-(xrand+150,35),2,b line(xrand+131,16)-(xrand+149,34),2,b if x>xrand+5 and x<xrand+35 and y>10 and y<40 then line(xrand+5,10)-(xrand+35,40),RGB(255,217,15),bf line(xrand+5,10)-(xrand+35,40),2,b circle(xrand+20,25),12,2,,,1 elseif x>xrand+45 and x<xrand+75 and y>10 and y<40 then line(xrand+45,10)-(xrand+75,40),RGB(255,217,15),bf line(xrand+45,10)-(xrand+75,40),2,b line(xrand+50,35)-(xrand+70,15),2 elseif x>xrand+85 and x<xrand+115 and y>10 and y<40 then line(xrand+85,10)-(xrand+115,40),RGB(255,217,15),b circle(xrand+100,25),15,RGB(255,0,0) circle(xrand+100,25),12,RGB(0,255,0),,,1 circle(xrand+100,25),9,RGB(0,0,255),,,1 elseif x>xrand+125 and x<xrand+155 and y>10 and y<40 then line(xrand+125,10)-(xrand+155,40),RGB(255,217,15),bf line(xrand+125,10)-(xrand+155,40),2,b line(xrand+130,15)-(xrand+150,35),2,b line(xrand+131,16)-(xrand+149,34),2,b end if if Taste=1 and x>xrand+5 and x<xrand+35 and y>10 and y<40 then line(xrand+5,10)-(xrand+35,40),RGB(255,0,0),bf line(xrand+5,10)-(xrand+35,40),2,b circle(xrand+20,25),12,2,,,1 Modus=1 elseif Taste=1 and x>xrand+45 and x<xrand+75 and y>10 and y<40 then line(xrand+45,10)-(xrand+75,40),RGB(255,0,0),bf line(xrand+45,10)-(xrand+75,40),2,b line(xrand+50,35)-(xrand+70,15),2 Modus=2 elseif Taste=1 and x>xrand+85 and x<xrand+115 and yrand>10 and y<40 then circle(xrand+100,25),15,RGB(255,0,0),,,1 circle(xrand+100,25),12,RGB(0,255,0),,,1 circle(xrand+100,25),9,RGB(0,0,255),,,1 line(xrand+85,10)-(xrand+115,40),RGB(255,0,0),b Modus=5 elseif Taste=1 and 110 and 245 and y>yrand+20 and y<yrand+50 then if Taste=1 and x>111 and x<124 and y>yrand+21 and y<yrand+34 then Rot=0 Blau=0 Gruen=0 elseif Taste=1 and x>126 and x<139 and y>yrand+21 and y<yrand+34 then Rot=100 Blau=100 Gruen=100 elseif Taste=1 and x>141 and x<154 and y>yrand+21 and y<yrand+34 then Rot=255 Blau=0 Gruen=0 elseif Taste=1 and x>156 and x<169 and y>yrand+21 and y<yrand+34 then Rot=0 Blau=0 Gruen=255 elseif Taste=1 and x>171 and x<184 and y>yrand+21 and y<yrand+34 then Rot=0 Gruen=0 Blau=255 elseif Taste=1 and x>186 and x<199 and y>yrand+21 and y<yrand+34 then Rot=255 Gruen=255 Blau=0 elseif Taste=1 and x>201 and x<214 and y>yrand+21 and y<yrand+34 then Rot=255 Gruen=170 Blau=0 elseif Taste=1 and x>216 and x<229 and y>yrand+21 and y<yrand+34 then Rot=255 Gruen=0 Blau=255 elseif Taste=1 and x>231 and x<244 and y>yrand+21 and y<yrand+34 then Rot=0 Gruen=255 Blau=255 elseif Taste=1 and x>111 and x<124 and y>yrand+36 and y<yrand+49 then Rot=255 Blau=255 Gruen=255 elseif Taste=1 and x>126 and x<139 and y>yrand+36 and y<yrand+49 then Rot=200 Blau=200 Gruen=200 elseif Taste=1 and x>141 and x<154 and y>yrand+36 and y<yrand+49 then Rot=255 Blau=60 Gruen=80 elseif Taste=1 and x>156 and x<169 and y>yrand+36 and y<yrand+49 then Rot=154 Blau=255 Gruen=0 elseif Taste=1 and x>171 and x<184 and y>yrand+36 and y<yrand+49 then Rot=0 Gruen=168 Blau=255 elseif Taste=1 and x>186 and x<199 and y>yrand+36 and y<yrand+49 then Rot=255 Gruen=255 Blau=150 elseif Taste=1 and x>201 and x<214 and y>yrand+36 and y<yrand+49 then Rot=255 Gruen=190 Blau=200 elseif Taste=1 and x>216 and x<229 and y>yrand+36 and y<yrand+49 then Rot=255 Gruen=120 Blau=255 elseif Taste=1 and x>231 and x<244 and y>yrand+36 and y<yrand+49 then Rot=150 Gruen=255 Blau=255 end if line(9,yrand+19)-(101,yrand+111),2,b line(10,yrand+20)-(100,yrand+110),RGB(Rot,Gruen,Blau),bf elseif Taste=1 and x>xrand+125 and x<xrand+155 and y>10 and y<40 then line(xrand+125,10)-(xrand+155,40),RGB(255,0,0),bf line(xrand+125,10)-(xrand+155,40),2,b line(xrand+130,15)-(xrand+150,35),2,b line(xrand+131,16)-(xrand+149,34),2,b Modus=12 end if if key2="s" then Modus=0 if key2="e" then Modus=4 if Modus=1 then line(xrand+5,10)-(xrand+35,40),RGB(255,0,0),bf line(xrand+5,10)-(xrand+35,40),2,b circle(xrand+20,25),12,2,,,1 if Radius=25 then line(265,yrand+25)-(275,yrand+35),2,b end if elseif Modus=2 then line(xrand+45,10)-(xrand+75,40),RGB(255,0,0),bf line(xrand+45,10)-(xrand+75,40),2,b line(xrand+50,35)-(xrand+70,15),2 elseif Modus=5 then circle(xrand+100,25),15,RGB(255,0,0),,,1 circle(xrand+100,25),12,RGB(0,255,0),,,1 circle(xrand+100,25),9,RGB(0,0,255),,,1 line(xrand+85,10)-(xrand+115,40),RGB(255,0,0),b elseif Modus=12 then line(xrand+125,10)-(xrand+155,40),RGB(255,0,0),bf line(xrand+125,10)-(xrand+155,40),2,b line(xrand+130,15)-(xrand+150,35),2,b line(xrand+131,16)-(xrand+149,34),2,b end if loop until Taste=1 or key2<>"" if Modus=1 then line(xrand+5,10)-(xrand+35,40),RGB(255,0,0),bf line(xrand+5,10)-(xrand+35,40),2,b circle(xrand+20,25),12,2,,,1 do line(261,yrand+21)-(399,yrand+109),RGB(255,255,255),bf circle(270,yrand+30),5,2,,,1 circle(285,yrand+35),10,2,,,1 circle(310,yrand+40),15,2,,,1 circle(345,yrand+45),20,2,,,1 if Radius=25 then line(265,yrand+25)-(275,yrand+35),2,b end if getmouse x,y,Rad,Taste if Taste=1 and x<275 and x>265 and y>yrand+25 and y<yrand+35 then Radius=25 line(265,yrand+25)-(275,yrand+35),2,b elseif Taste=1 and x<295 and x>275 and y>yrand+25 and y<yrand+45 then Radius=50 elseif Taste=1 and x<325 and x>295 and y>yrand+25 and y<yrand+55 then Radius=75 end if loop Until Taste=1 x1 = x y1 = y if x1<xrand and y1<yrand then circle bild,(x1,y1),Radius,RGB(Rot,Gruen,Blau),,,1 circle (x1,y1),Radius,RGB(Rot,Gruen,Blau) ,,,1 end if elseif Modus=2 then line(xrand+45,10)-(xrand+75,40),RGB(255,0,0),bf line(xrand+45,10)-(xrand+75,40),2,b line(xrand+50,35)-(xrand+70,15),2 do line(261,yrand+21)-(399,yrand+109),RGB(255,255,255),bf getmouse x,y,Rad,Taste loop Until Taste=1 x1 = x y1 = y do getmouse x,y,Rad,Taste loop Until Taste=0 x2=x y2=y if x1<xrand and y1<yrand then if x2<xrand and y2<yrand then Line bild,(x1,y1)-(x2,y2),RGB(Rot,Gruen,Blau) Line (x1,y1)-(x2,y2),RGB(Rot,Gruen,Blau) end if end if elseif Modus=4 then end elseif Modus=5 then line(261,yrand+21)-(399,yrand+109),RGB(255,255,255),bf circle(xrand+100,25),15,RGB(255,0,0),,,1 circle(xrand+100,25),12,RGB(0,255,0),,,1 circle(xrand+100,25),9,RGB(0,0,255),,,1 line(xrand+85,10)-(xrand+115,40),RGB(255,0,0),b do a1=inkey sleep 500 loop until a1<>"" do b1=inkey sleep 500 loop until b1<>"" do c1=inkey sleep 500 loop until c1<>"" do a2=inkey sleep 500 loop until a2<>"" do b2=inkey sleep 500 loop until b2<>"" do c2=inkey sleep 500 loop until c2<>"" do a3=inkey sleep 500 loop until a3<>"" do b3=inkey sleep 500 loop until b3<>"" do c3=inkey sleep 500 loop until c3<>"" Farbe1=val(a1)*100 Farbe2=val(b1)*10 Farbe3=val(c1) Farbe4=val(a2)*100 Farbe5=val(b2)*10 Farbe6=val(c2) Farbe7=val(a3)*100 Farbe8=val(b3)*10 Farbe9=val(c3) Rot=Farbe1+Farbe2+Farbe3 Gruen=Farbe4+Farbe5+Farbe6 Blau=Farbe7+Farbe8+Farbe9 if Rot > 255 or Gruen > 255 or Blau > 255 then Blau=0 Rot=0 Gruen=0 end if line(9,yrand+19)-(101,yrand+111),2,b line(10,yrand+20)-(100,yrand+110),RGB(Rot,Gruen,Blau),bf Modus=1 elseif Modus=0 then Bsave Bildname, bild elseif Modus=9 then Bildname="" do Bildzwischenspeicher=inkey Bildname=Bildname+Bildzwischenspeicher loop until Bildzwischenspeicher="p" elseif Modus=8 then do getmouse x,y,Rad,Taste loop until Taste=1 or key<>"" if Pinsel=1 then do getmouse x,y,Rad,Taste loop until Taste=1 Pset (x,y),RGB(Rot,Gruen,Blau) Pset (x+1,y),RGB(Rot,Gruen,Blau) Pset (x,y+1),RGB(Rot,Gruen,Blau) Pset (x+1,y+1),RGB(Rot,Gruen,Blau) Pset (x-1,y),RGB(Rot,Gruen,Blau) Pset (x,y-1),RGB(Rot,Gruen,Blau) Pset (x-1,y-1),RGB(Rot,Gruen,Blau) Pset bild,(x,y),RGB(Rot,Gruen,Blau) Pset bild,(x+1,y),RGB(Rot,Gruen,Blau) Pset bild,(x,y+1),RGB(Rot,Gruen,Blau) Pset bild,(x+1,y+1),RGB(Rot,Gruen,Blau) Pset bild,(x-1,y),RGB(Rot,Gruen,Blau) Pset bild,(x,y-1),RGB(Rot,Gruen,Blau) Pset bild,(x-1,y-1),RGB(Rot,Gruen,Blau) elseif Pinsel=2 then do getmouse x,y,Rad,Taste loop until Taste=1 Pset (x,y),RGB(Rot,Gruen,Blau) Pset (x+1,y),RGB(Rot,Gruen,Blau) Pset (x,y+1),RGB(Rot,Gruen,Blau) Pset (x+1,y+1),RGB(Rot,Gruen,Blau) Pset (x-1,y),RGB(Rot,Gruen,Blau) Pset (x,y-1),RGB(Rot,Gruen,Blau) Pset (x-1,y-1),RGB(Rot,Gruen,Blau) Pset (x+2,y),RGB(Rot,Gruen,Blau) Pset (x,y+2),RGB(Rot,Gruen,Blau) Pset (x+2,y+2),RGB(Rot,Gruen,Blau) Pset (x-2,y),RGB(Rot,Gruen,Blau) Pset (x-2,y-2),RGB(Rot,Gruen,Blau) Pset (x,y-2),RGB(Rot,Gruen,Blau) Pset bild,(x,y),RGB(Rot,Gruen,Blau) Pset bild,(x+1,y),RGB(Rot,Gruen,Blau) Pset bild,(x,y+1),RGB(Rot,Gruen,Blau) Pset bild,(x+1,y+1),RGB(Rot,Gruen,Blau) Pset bild,(x-1,y),RGB(Rot,Gruen,Blau) Pset bild,(x,y-1),RGB(Rot,Gruen,Blau) Pset bild,(x-1,y-1),RGB(Rot,Gruen,Blau) Pset bild,(x+2,y),RGB(Rot,Gruen,Blau) Pset bild,(x,y+2),RGB(Rot,Gruen,Blau) Pset bild,(x+2,y+2),RGB(Rot,Gruen,Blau) Pset bild,(x-2,y),RGB(Rot,Gruen,Blau) Pset bild,(x-2,y-2),RGB(Rot,Gruen,Blau) Pset bild,(x,y-2),RGB(Rot,Gruen,Blau) elseif Pinsel=3 then do getmouse x,y,Rad,Taste loop until Taste=1 line(x-5,y+5)-(x+5,y-5),RGB(Rot,Gruen,Blau),bf line bild,(x-5,y+5)-(x+5,y-5),RGB(Rot,Gruen,Blau),bf end if elseif Modus=10 then do key=inkey loop until key<>"" if key="1" then Pinsel=1 elseif key="s" then Pinsel=2 elseif key="2" then Pinsel=3 end if key="" elseif Modus=11 then do getmouse x,y,Rad,Taste loop until Taste=1 line(x-5,y+5)-(x+5,y-5),RGB(255,255,255),bf line bild,(x-5,y+5)-(x+5,y-5),RGB(255,255,255),bf elseif Modus=12 then do line(261,yrand+21)-(399,yrand+109),RGB(255,255,255),bf getmouse x,y,Rad,Taste loop Until Taste=1 x1 = x y1 = y do getmouse x,y,Rad,Taste loop Until Taste=0 x2=x y2=y if x1<xrand and y1<yrand and x2<xrand and y2<yrand then Line (x1,y1)-(x2,y2),RGB(Rot,Gruen,Blau),b Line bild,(x1,y1)-(x2,y2),RGB(Rot,Gruen,Blau),b end if elseif Modus=13 then do getmouse x,y,Rad,Taste loop Until Taste=1 x1 = x y1 = y do getmouse x,y,Rad,Taste loop Until Taste=0 x2=x y2=y Line (x1,y1)-(x2,y2),RGB(Rot,Gruen,Blau),bf Line bild,(x1,y1)-(x2,y2),RGB(Rot,Gruen,Blau),bf end if LOOP
Dieser Quellcode wurde von Stueber am 22.03.2009 mit der Freebasic Version 0.20b getestet.