tag:blogger.com,1999:blog-5144326766971398625.post2031639668809980211..comments2013-11-28T01:44:53.766-08:00Comments on Java Game Programming Step by Step (Applets): Drawing a filled polygon example.Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-5144326766971398625.post-32717993305623502762013-11-28T01:44:53.766-08:002013-11-28T01:44:53.766-08:00 public void saltaAfila(RandomAccessFile rFile,... public void saltaAfila(RandomAccessFile rFile, int fila) <br /> throws IOException{<br /> rFile.seek(fila*MIDA_FILA);<br /> }<br /> <br /> public void saltaAlIniciDeFila(RandomAccessFile rFile) <br /> throws IOException{<br /> long pos = rFile.getFilePointer();<br /> pos = pos - pos % MIDA_FILA; //EXACTAMENT AL INICI DE LA FILA<br /> <br /> rFile.seek(pos);<br /> }<br /> <br /> public void saltaASeguentFila(RandomAccessFile rFile) <br /> throws IOException{<br /> long pos = rFile.getFilePointer() -1;<br /> pos = pos - pos % MIDA_FILA + MIDA_FILA; //EXACTAMENT AL INICI DE LA FILA SEGUENT<br /> <br /> rFile.seek(pos);<br /> }<br /> <br /> <br /> public void saltaAColumna(RandomAccessFile rFile, int columna) <br /> throws IOException{<br /> rFile.seek(patroColumnes[columna]);<br /> }<br /> <br /> public String obteText(RandomAccessFile rFile)throws IOException{<br /> String text="";<br /> char c = rFile.readChar();<br /> while (c != 0) {<br /> text += c;<br /> c = rFile.readChar();<br /> }<br /> return text; <br /> }<br /> <br /> public boolean esFinal(RandomAccessFile rFile) throws IOException{<br /> return rFile.length()==rFile.getFilePointer();<br /> }<br /> <br /> public long numFiles(RandomAccessFile rFile) throws IOException{<br /> return rFile.length()%MIDA_FILA;<br /> }<br /> <br /> public void escriuText(RandomAccessFile rFile, String text)<br /> throws IOException{<br /> for(int i=0; i<text.length(); i++){<br /> rFile.writeChar(text.charAt(i));<br /> }<br /> rFile.writeChar(0);<br /> }<br /> <br /> <br /> /*<br /> * Mètodes específics<br /> */<br /> <br /> public void escriuNom(RandomAccessFile rFile, int fila, String text)<br /> throws IOException{<br /> saltaAfila(rFile, fila);<br /> saltaAColumna(rFile, NOM);<br /> escriuText(rFile, text);<br /> }<br /> <br /> public void escriuEdat(RandomAccessFile rFile, int fila, int valor)<br /> throws IOException{<br /> saltaAfila(rFile, fila);<br /> saltaAColumna(rFile, EDAT);<br /> rFile.writeInt(valor);<br /> }<br /> <br /> public void insereixFilaAlFinal(RandomAccessFile rFile, String nom, <br /> String cognom1, String cognom2, <br /> int edat) throws IOException{<br /> rFile.seek(rFile.length());<br /> <br /> escriuText(rFile, nom);<br /> saltaAColumna(rFile, COGNOM1);<br /> escriuText(rFile, cognom1);<br /> saltaAColumna(rFile, COGNOM2);<br /> escriuText(rFile, cognom2);<br /> saltaAColumna(rFile, EDAT);<br /> rFile.writeInt(edat);<br /> }<br /> <br /><br /> public String obteNom(RandomAccessFile rFile, int fila) <br /> throws IOException{<br /> String text;<br /> saltaAfila(rFile, fila);<br /> saltaAColumna(rFile, NOM);<br /> text = obteText(rFile);<br /> return text;<br /> }<br /> <br /> public int obteEdat(RandomAccessFile rFile, int fila) <br /> throws IOException{<br /> int valor;<br /> saltaAfila(rFile, fila);<br /> saltaAColumna(rFile, EDAT);<br /> valor = rFile.readInt();<br /> return valor;<br /> }Anonymoushttps://www.blogger.com/profile/15298679992454475580noreply@blogger.comtag:blogger.com,1999:blog-5144326766971398625.post-30138219641976184852013-11-28T01:43:26.504-08:002013-11-28T01:43:26.504-08:00 public void escriu(){
Scanner scanner ... public void escriu(){<br /> Scanner scanner = new Scanner(System.in);<br /> RandomAccessFile rFile = null;<br /> String nom;<br /> String c1;<br /> String c2;<br /> int edat;<br /> <br /> try {<br /> <br /> rFile = new RandomAccessFile(file, "rw");<br /> <br /> do{<br /> nom = scanner.nextLine();<br /> if(nom.length()>0){<br /> c1 = scanner.nextLine();<br /> c2 =scanner.nextLine();<br />edat = scanner.nextInt(); <br /><br /> insereixFilaAlFinal(rFile, nom, c1, c2, edat);<br /> } <br /> }while (nom.length()>0);<br /> <br /> rFile.close();<br /> <br /> } catch (IOException ex) {<br />System.out.println("ERRROR");<br /> }<br /> }<br /> <br /> <br /> public void llegeixParells(){<br /> RandomAccessFile rFile = null;<br /> long files;<br /> <br /> try {<br /> <br /> rFile = new RandomAccessFile(file, "rw");<br /> <br /> files = numFiles(rFile);<br /> <br /> for(int i=0; i<files; i+=2){<br /> System.out.print(i); <br /> System.out.print(": "); <br /> System.out.print(obteNom(rFile, i));<br /> System.out.print("("); <br /> System.out.print(obteEdat(rFile, i));<br /> System.out.println(")"); <br /> }<br /> <br /> rFile.close();<br /> <br /> } catch (IOException ex) {<br /> System.out.println("ERRROR");<br /> }<br /> }<br /><br /> <br /> public void llegeixTot(){<br /> RandomAccessFile rFile = null;<br /> <br /> try {<br /> <br /> rFile = new RandomAccessFile(file, "rw");<br /> rFile.seek(0);<br /> <br /> while(!esFinal(rFile)){<br /> System.out.print("- "); <br /> System.out.print(obteText(rFile));<br /> saltaAColumna(rFile, EDAT);<br /> System.out.print(" ("); <br /> System.out.print(rFile.readInt());<br /> System.out.println(")"); <br /> }<br /> <br /> rFile.close();<br /> <br /> } catch (IOException ex) {<br /> System.out.println("ERRROR");<br /> }<br /> }<br />public class ExempleComplertAmbCharsMarca0Estructurat{<br /> File file = new File("carpeta/dades.dat");<br /> <br /> /*FORMAT D'EMMAGATZEMATGE:<br /> * 1 2 3 4 5 6 7 <br /> * 024680246802468024680246802468024680... BYTES<br /> * NOM_______0COGNOM1___0COGNOM2___0EDAT PATRO DADES<br /> */<br /> <br /> public static int[] patroColumnes={0, 22, 44, 66};<br /> public static int NOM = 0; <br /> public static int COGNOM1 = 1;<br /> public static int COGNOM2 = 2; <br /> public static int EDAT = 3;<br /> public static int MIDA_FILA = 70;Anonymoushttps://www.blogger.com/profile/15298679992454475580noreply@blogger.comtag:blogger.com,1999:blog-5144326766971398625.post-27690933373754954822013-11-28T01:37:43.148-08:002013-11-28T01:37:43.148-08:00
/*
* Mètodes genèrics
*/
... <br /> <br /> /*<br /> * Mètodes genèrics<br /> */<br /> public void saltaAfila(RandomAccessFile rFile, int fila) <br /> throws IOException{<br /> rFile.seek(fila*MIDA_FILA);<br /> }<br /> <br /> public void saltaAlIniciDeFila(RandomAccessFile rFile) <br /> throws IOException{<br /> long pos = rFile.getFilePointer();<br /> pos = pos - pos % MIDA_FILA; //EXACTAMENT AL INICI DE LA FILA<br /> <br /> rFile.seek(pos);<br /> }<br /> <br /> public void saltaASeguentFila(RandomAccessFile rFile) <br /> throws IOException{<br /> long pos = rFile.getFilePointer() -1;<br /> pos = pos - pos % MIDA_FILA + MIDA_FILA; //EXACTAMENT AL INICI DE LA FILA SEGUENT<br /> <br /> rFile.seek(pos);<br /> }<br /> <br /> <br /> public void saltaAColumna(RandomAccessFile rFile, int columna) <br /> throws IOException{<br /> rFile.seek(patroColumnes[columna]);<br /> }<br /> <br /> public String obteText(RandomAccessFile rFile)throws IOException{<br /> String text="";<br /> char c = rFile.readChar();<br /> while (c != 0) {<br /> text += c;<br /> c = rFile.readChar();<br /> }<br /> return text; <br /> }<br /> <br /> public boolean esFinal(RandomAccessFile rFile) throws IOException{<br /> return rFile.length()==rFile.getFilePointer();<br /> }<br /> <br /> public long numFiles(RandomAccessFile rFile) throws IOException{<br /> return rFile.length()%MIDA_FILA;<br /> }<br /> <br /> public void escriuText(RandomAccessFile rFile, String text)<br /> throws IOException{<br /> for(int i=0; i0){<br /> c1 = scanner.nextLine();<br /> c2 =scanner.nextLine();<br />edat = scanner.nextInt(); <br /><br /> insereixFilaAlFinal(rFile, nom, c1, c2, edat);<br /> } <br /> }while (nom.length()>0);<br /> <br /> rFile.close();<br /> <br /> } catch (IOException ex) {<br />System.out.println("ERRROR");<br /> }<br /> }<br /> <br /> <br /> public void llegeixParells(){<br /> RandomAccessFile rFile = null;<br /> long files;<br /> <br /> try {<br /> <br /> rFile = new RandomAccessFile(file, "rw");<br /> <br /> files = numFiles(rFile);<br /> <br /> for(int i=0; i<files; i+=2){<br /> System.out.print(i); <br /> System.out.print(": "); <br /> System.out.print(obteNom(rFile, i));<br /> System.out.print("("); <br /> System.out.print(obteEdat(rFile, i));<br /> System.out.println(")"); <br /> }<br /> <br /> rFile.close();<br /> <br /> } catch (IOException ex) {<br /> System.out.println("ERRROR");<br /> }<br /> }<br /><br /> <br /> public void llegeixTot(){<br /> RandomAccessFile rFile = null;<br /> <br /> try {<br /> <br /> rFile = new RandomAccessFile(file, "rw");<br /> rFile.seek(0);<br /> <br /> while(!esFinal(rFile)){<br /> System.out.print("- "); <br /> System.out.print(obteText(rFile));<br /> saltaAColumna(rFile, EDAT);<br /> System.out.print(" ("); <br /> System.out.print(rFile.readInt());<br /> System.out.println(")"); <br /> }<br /> <br /> rFile.close();<br /> <br /> } catch (IOException ex) {<br /> System.out.println("ERRROR");<br /> }<br /> }<br /><br /> <br /> /**<br /> * @param args the command line arguments<br /> */<br /> public static void main(String[] args) {<br /> ExempleTractamentFitxer exemple = new ExempleTractamentFitxer();<br /> <br /> exemple.escriu();<br /> exemple.llegeixParells();<br /> exemple.llegeixTot();<br /> } <br />}<br /><br /> <br /> /**<br /> * @param args the command line arguments<br /> */Anonymoushttps://www.blogger.com/profile/15298679992454475580noreply@blogger.comtag:blogger.com,1999:blog-5144326766971398625.post-38054415825827182262013-11-28T01:36:15.141-08:002013-11-28T01:36:15.141-08:00public static void main(String[] args) {
E...public static void main(String[] args) {<br /> ExempleTractamentFitxer exemple = new ExempleTractamentFitxer();<br /> <br /> exemple.escriu();<br /> exemple.llegeixParells();<br /> exemple.llegeixTot();<br /> } Anonymoushttps://www.blogger.com/profile/15298679992454475580noreply@blogger.com