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.

Wednesday, 17 February 2021

Java Desktop Clock

  • This is a program for creating a desktop clock that shows the time in both digital and analogue form in java.
  • To set it as a startup app paste the jar formed from this file in your C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp folder
  • This is the code for the file:

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.MouseInfo;

import java.awt.Toolkit;

import java.awt.Window.Type;

import java.awt.geom.Ellipse2D;

import java.sql.Timestamp;

import java.text.SimpleDateFormat;

 

import javax.swing.JFrame;

import javax.swing.JPanel;

 

public class clockjar {

 

       @SuppressWarnings("serial")

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

              final JFrame frm=new JFrame();

              frm.setUndecorated(true);

              frm.setOpacity(1);

              frm.setSize(101, 101);

              frm.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width-frm.getWidth(),170);

              frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              frm.setAlwaysOnTop(true);

              frm.setType(Type.UTILITY);

              frm.setVisible(true);

              JPanel pnl=new JPanel();

              final SimpleDateFormat h = new SimpleDateFormat("HH");

              final SimpleDateFormat m = new SimpleDateFormat("mm");

              final SimpleDateFormat s = new SimpleDateFormat("ss");

              frm.setShape(new Ellipse2D.Double(0, 0, frm.getWidth(), frm.getHeight()));

              frm.setOpacity(0.65f);

              for(;;) {

                     frm.remove(pnl);

                     pnl=new JPanel() {

                           @Override

                           protected void paintComponent(Graphics g) {

                                  g.setColor(Color.black);

                                  g.drawOval(0,0, 100, 100);

                                  g.setColor(Color.green);

                                  g.drawOval(1,1, 98, 98);

                                  g.setColor(Color.black);

                                  g.drawOval(2,2, 96, 96);

                                  Timestamp timestamp1 = new Timestamp(System.currentTimeMillis());

                                  double hr=Double.parseDouble(h.format(timestamp1.getTime()));

                                  double min=Double.parseDouble(m.format(timestamp1.getTime()));

                                  double sec=Double.parseDouble(s.format(timestamp1.getTime()));

                                  double ahr=(hr+(min+sec/3600)/60+sec/3600);

                                  ahr=ahr*360/12;

                                  double amin=min+sec/3600;

                                  amin=amin*360/60;

                                  double asec=sec;

                                  asec=asec*360/60;

                                  ahr=(270+ahr)%360;

                                  amin=(270+amin)%360;

                                  asec=(270+asec)%360;

                                  g.drawLine(50, 50, 50+(int) (45*Math.cos(Math.toRadians(asec))), 50+(int) (45*Math.sin(Math.toRadians(asec))));

                                  Graphics2D g2 = (Graphics2D) g;

                                  g2.setStroke(new BasicStroke(2));

                                  g.setColor(Color.red);

                                  g.drawLine(50, 50, 50+(int) (35*Math.cos(Math.toRadians(amin))), 50+(int) (35*Math.sin(Math.toRadians(amin))));

                                  g2.setStroke(new BasicStroke(3));

                                  g.setColor(Color.blue);

                                  g.drawLine(50, 50, 50+(int) (20*Math.cos(Math.toRadians(ahr))), 50+(int) (20*Math.sin(Math.toRadians(ahr))));

                                  g.setColor(Color.red.darker());

                                  String minstr=(int)min+"";

                                  String hrstr=(int)hr%12+"";

                                  if ((int)min/10==0) {

                                         minstr="0"+minstr;

                                  }

                                  if ((int)hr%12/10==0) {

                                         hrstr="0"+hrstr;

                                  }

                                  g.drawString(hrstr+":"+minstr, 34, 80);

                           }

                     };

                     frm.add(pnl);

                     frm.invalidate();

                     frm.validate();

                     frm.repaint();

                     Thread.sleep(100);

                     if (MouseInfo.getPointerInfo().getLocation().x<=frm.getLocation().x+frm.getWidth()

                     && MouseInfo.getPointerInfo().getLocation().x>=frm.getLocation().x

                     && MouseInfo.getPointerInfo().getLocation().y<=frm.getLocation().y+frm.getHeight()

                     && MouseInfo.getPointerInfo().getLocation().y>=frm.getLocation().y) {

                           frm.setOpacity(0);

                     }

                     else {

                           frm.setOpacity(0.65f);

                     }

              }

       }

 

}

  • Run the file in your IDE 


Algorithmic Pixel Art: The Beauty of Aliasing

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