Monday, 22 March 2021

Directory tree in Java

  • Start your IDE and paste the following code.
  • Execute the code to build a directory tree of your selected folder. 

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.PrintStream;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.nio.file.attribute.BasicFileAttributes;

import java.util.Scanner;

 

public class file {

       static void print(File dir, int tab) throws IOException {

              File lastfolder = null;

              try {

                     for (int j = 0; j < tab - 1; j++) {

                           System.out.print("│   ");

                     }

                     for (int i = 0; i < dir.getParentFile().listFiles().length; i++) {

                           lastfolder = dir.getParentFile().listFiles()[i];

                     }

                     if (lastfolder.equals(dir) && !(tab == 0)) {

                           System.out.print("└──");

                     } else if (!(tab == 0)) {

                           System.out.print("├──");

                     }

                     printdir(dir);

              } catch (NullPointerException e) {

                     printdir(dir);

              }

              try {

                     for (int i = 0; i < dir.listFiles().length - 1; i++) {

                           if (dir.listFiles()[i].isFile() && dir.listFiles()[i].exists()) {

                                  for (int j = 0; j < tab; j++) {

                                         System.out.print("│   ");

                                  }

                                  /*if (!dir.equals(lastfolder)) {

                                         System.out.print("│   ");

                                  }

                                  else {

                                         System.out.print("    ");

                                  }*/

                                  System.out.print("├──");

                                  printfile(dir.listFiles()[i]);

                                  System.out.println();

                           } else {

                                  print(dir.listFiles()[i], tab + 1);

                           }

                     }

                     int l = dir.listFiles().length - 1;

                     if (l >= 0) {

                           if (dir.listFiles()[l].isFile() && dir.listFiles()[l].exists()) {

                                  for (int j = 0; j < tab; j++) {

                                         System.out.print("│   ");

                                  }

                                  System.out.print("└──");

                                  printfile(dir.listFiles()[l]);

                                  System.out.println();

                           } else {

                                  print(dir.listFiles()[l], tab + 1);

                           }

                     }

              } catch (NullPointerException e) {

                     // TODO: handle exception

              }

       }

 

       private static void printdir(File dir) throws IOException {

              BasicFileAttributes attr=Files.readAttributes(Paths.get(dir.toString()), BasicFileAttributes.class);

              System.out.print("[" + dir + "]"+" /CreatedOn: "+attr.creationTime()+" LastModified: "+attr.lastModifiedTime()+" LastAccesedOn: "+attr.lastAccessTime()+" Size: "+(double)(Files.size(Paths.get(dir.toString())))/1024+"kB/");

              System.out.println();

 

       }

 

       private static void printfile(File file) throws IOException {

              BasicFileAttributes attr=Files.readAttributes(Paths.get(file.toString()), BasicFileAttributes.class);

              System.out.print("("+file +") /CreatedOn: "+attr.creationTime()+" LastModifiedOn: "+attr.lastModifiedTime()+" LastAccesedOn: "+attr.lastAccessTime()+" Size: "+(double)(Files.size(Paths.get(file.toString())))/1024+"kB/");

 

       }

       @SuppressWarnings("resource")

       public static void main(String[] args) throws IOException {

              System.out.print("Enter search directory name: ");

              File dir = new File(new Scanner(System.in).next());

              System.out.print("Enter output filename: ");

              try {

                     System.setOut(new PrintStream(new File(new Scanner(System.in).next())));

              } catch (FileNotFoundException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

              if (dir.isDirectory() && dir.exists()) {

                     print(dir, 0);

              }

 

       }

 

}

Saturday, 27 February 2021

Java 3D Rotation Viewer

  • This is a 3D viewer in which a cube and the 3 axes can be seen. The view can be rotated by the 3 x,y and z sliders.
  • Paste the following code:

import java.awt.Color;

import java.awt.Graphics;

 

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JSlider;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

 

public class Three_djar {

 

       @SuppressWarnings("serial")

       public static void main(String[] args) throws InterruptedException {

              //Dynamic

              final int s=300;

              //Statics

              final JFrame frm=new JFrame("3DViewer");

              frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              frm.setSize(s+15+210, s+39);

              frm.setLayout(null);

              frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              final double[][][] a=new double[s][s][s];

              for(int i=100;i<200;i++) {

                     a[100][100][i]=1;

                     a[200][100][i]=1;

                     a[100][200][i]=1;

                     a[200][200][i]=1;

                     a[100][i][100]=1;

                     a[200][i][100]=1;

                     a[100][i][200]=1;

                     a[200][i][200]=1;

                     a[i][100][100]=1;

                     a[i][200][100]=1;

                     a[i][100][200]=1;

                     a[i][200][200]=1;

              }

              for(int i=0;i<s;i++) {

                     a[s/2][s/2][i]=1;

                     a[i][s/2][s/2]=1;

                     a[s/2][i][s/2]=1;

              }

              final JSlider xf=new JSlider(JSlider.HORIZONTAL, 0, 360, 0);

              final JSlider yf=new JSlider(JSlider.HORIZONTAL, 0, 360, 0);

              final JSlider zf=new JSlider(JSlider.HORIZONTAL, 0, 360, 0);

              xf.setSize(200,20);

              yf.setSize(200,20);

              zf.setSize(200,20);

              xf.setLocation(s+5,5);

              yf.setLocation(s+5,25);

              zf.setLocation(s+5,45);

              frm.add(xf);frm.add(yf);frm.add(zf);

              final JPanel pnl[]= {new JPanel()};

              ChangeListener ad=new ChangeListener() {

 

                     @Override

                     public void stateChanged(ChangeEvent arg0) {

                           frm.remove(pnl[0]);

                           final double xth=Math.toRadians(xf.getValue());

                           final double yth=Math.toRadians(yf.getValue());

                           final double zth=Math.toRadians(zf.getValue());

                           pnl[0]=new JPanel() {

                                  @Override

                                  protected void paintComponent(Graphics g) {

                                         g.setColor(Color.black);

                                         for(int i=0;i<s;i++) {

                                                for(int j=0;j<s;j++) {

                                                       for(int k=0;k<s;k++) {

                                                              if (a[i][j][k]==1) {

                                                                     double x=i-s/2;

                                                                     double y=j-s/2;

                                                                     double z=k-s/2;

                                                                     //X-Yaw

                                                                     double ytemp=y;

                                                                     y=Math.cos(xth)*y-Math.sin(xth)*z;

                                                                     z=Math.cos(xth)*z+Math.sin(xth)*ytemp;

                                                                     //Y-Pitch

                                                                     double xtemp=x;

                                                                     x=x*Math.cos(yth)+z*Math.sin(yth);

                                                                     z=z*Math.cos(yth)-xtemp*Math.sin(yth);

                                                                     //Z-Roll

                                                                     xtemp=x;

                                                                     x=Math.cos(zth)*x-Math.sin(zth)*y;

                                                                     y=Math.sin(zth)*xtemp+Math.cos(zth)*y;

                                                                     g.setColor(new Color(0,0,0,0.2f));

                                                                     g.fillRect((int) (x+s/2), (int) (y+s/2), 2, 2);

                                                              }

                                                       }

                                                }

                                         }

                                  }

                           };

                           pnl[0].setSize(s,s);

                           frm.add(pnl[0]);

                           frm.invalidate();

                           frm.validate();

                           frm.repaint();

 

                     }

              };      

              xf.addChangeListener(ad);

              yf.addChangeListener(ad);

              zf.addChangeListener(ad);

              frm.setVisible(true);

 

       }

 

}


       

  • Run this code.

Algorithmic Pixel Art: The Beauty of Aliasing

Discussed  here More images in  Google drive Some outputs: Forward to anyone who says aliasing is ugly.