A cikin duniyar shirye-shirye, mun ci karo da yanayi iri-iri inda muke buฦatar share na'urar bidiyo don inganta karantawa ko kuma kawai don farawa da sabon slate. ฦaya daga cikin irin wannan misalin da za mu tattauna a yau shine a cikin harshen shirye-shiryen Java. Java, kasancewar ษaya daga cikin mashahuran harsuna, yana ba da ษimbin ษakunan karatu da ayyuka don magance matsalar da ke hannunsu. A cikin wannan labarin, za mu nutse cikin zurfin fahimtar mafita don share wasan bidiyo a Java, yin bayanin lambar mataki-mataki, da kuma tattauna wasu mahimman ษakunan karatu da ayyuka waษanda ke taka muhimmiyar rawa wajen warware wannan batu.
Ana share kayan wasan bidiyo a Java
Don share console a Java, babu hanyar ginawa wanda za a iya amfani da shi kai tsaye. Koyaya, zamu iya cimma wannan ta amfani da dabaru daban-daban dangane da dandamali (Windows, Mac, Linux) lambar mu tana gudana. A cikin wannan labarin, za mu mai da hankali kan hanyar da ake amfani da ita da yawa wacce ke amfani da ANSI lambobin tserewa don share na'urar wasan bidiyo, wanda ke zaman kansa.
public class ClearConsole { public static void main(String[] args) { clearConsole(); System.out.println("Console cleared!"); } public static void clearConsole() { System.out.print("33[H33[2J"); System.out.flush(); } }
A cikin snippet code a sama, muna da ajin Java mai sauฦi da ake kira ClearConsole tare da main hanya, wacce ita ce hanyar shiga shirinmu. A cikin babbar hanyar, muna kira da clearConsole() aiki, wanda ke yin sihiri na share na'ura mai kwakwalwa.
Fahimtar Magani
Bari mu rushe lambar don fahimtar yadda hanyar clearConsole() ke aiki.
The clearConsole Hanyar ta ฦunshi mahimman layi biyu:
1. `System.out.print ("33[H33[2J");'
2. `System.out.flush();`
Layi na farko yana amfani da lambobin tserewa na ANSI don ba da umarni ga na'ura wasan bidiyo don share abun ciki. Sashin "33[H" na lambar tserewa yana motsa siginan kwamfuta zuwa kusurwar sama-hagu na tasha, yayin da ษangaren "33[2J" yana share allon tashar gaba ษaya. Haษa waษannan lambobin tserewa guda biyu yana tabbatar da cewa an tsaftace allon kuma an saita siginan kwamfuta a wurin farawa. An rubuta waษannan lambobin a cikin aikin `System.out.print()` wanda ke buga su akan na'ura mai kwakwalwa.
Layi na biyu, `System.out.flush();`, ana amfani da shi don zubar da rafin fitarwa. Yana tabbatar da cewa duk bayanan da aka ษoye a cikin rafin fitarwa ana fitar da su nan da nan don nunawa akan na'urar bidiyo. Yana taimakawa wajen kiyaye kayan aikin na'urar bidiyo na zamani da aiki tare da rubutaccen umarni.
Laburaren Java da Ayyuka
Yayin da hanyar da aka nuna a sama sanannen zaษi ne don sauฦi da inganci, Java yana ba da ษakunan karatu da ayyuka daban-daban waษanda za a iya amfani da su don ayyuka iri ษaya. Wasu daga cikin manyan ษakunan karatu da ayyukansu sune:
- java.io: Wannan ษakin karatu yana ba da ayyuka masu alaฦa da ayyukan shigarwa/fitarwa, gami da sarrafa fayil da karantawa/rubutu bayanai daga/zuwa na'ura wasan bidiyo. Ana iya amfani da shi a yanayi daban-daban, kamar karanta bayanai daga mai amfani yayin da shirin ke gudana.
- java.util: Wannan ษakin karatu yana ฦunshe da tsarin bayanai masu amfani da azuzuwan amfani don tarawa, ranaku, da lokuta. Ana iya amfani da shi don sarrafawa da tsara bayanai a cikin shirin yadda ya kamata.
- java.lang.System: Wannan ajin ya ฦunshi filayen aji da yawa masu amfani da hanyoyin, kamar fita, ciki, currentTimeMillis(), da fita(). Ana amfani da waษannan galibi don ayyukan shigarwa/fitarwa, sarrafa lokaci, da sarrafa tafiyar aiwatar da shirin Java.
A ฦarshe, share na'urar bidiyo a Java za a iya samu ta hanyoyi daban-daban, dangane da dandamali. Hanyar tserewa ta ANSI dalla-dalla a cikin wannan labarin yana ba da mafita mai zaman kansa. Dakunan karatu na Java, irin su java.io, java.util, da java.lang.System, suna ba da ayyuka da yawa don sarrafa sassa daban-daban na ayyukan na'ura wasan bidiyo da kuma biyan takamaiman bukatun shirin Java.