Java Program In Computer graphics

5th Semester

1. Write a java program to draw a line using DDA Algorithm.



import java.applet.Applet;
import java.awt.Graphics;
public class DDA_extends Applet {
  public void paint(Graphics g)
  {
    double dx,dy,steps,x,y,j;
    double xc,yc;
    double x1=200, y1= 500, x2= 600, y2= 200;
    dx = x2-x1;
    dy = y2-y1;
    if(Math.abs(dx) > Math.abs(dy))
    {
      steps = Math.abs(dx);
    }
    else
      steps = Math.abs(dy);
    xc = dx/steps;
    yc = dy/steps;
    x = x1;
    y = y1;
    //g.fillOval(x, y, width, height);
    g.fillOval((int)x, (int)y, 5, 5);
    for(j=1; j<=steps; j++)
    {
      x = x+xc;
      y= y+yc;
      g.fillOval((int)x, (int)y, 5, 5);
      System.out.println((int)y);
    }
  }
}

2.Write a java program to draw a line using Brasenhem Algorithm

package computer_Graphics;
import java.applet.*;
import java.awt.*;
public class Bresenhem extends Applet
{
    double x1=200,y1=100,x2=300,y2=500;
    double dx,dy,steps,x,y,j;
    double xc,yc;
    public void paint(Graphics g)
    {
    int ch=6;
    dx=Math.abs(x2-x1);
    dy=Math.abs(y2-y1);
    x=x1;
    y=y1;
    double p=(2*dy)-(dx);
    int i=1;
        one: for(j=10;ch==6;j++)
        {
            g.fillOval((int)x,(int)y,5,5);
            while(p>=0)
            {
                y=y+1;
                p=p-(2*dx);
            }
            x=x+1;
            p=p+(2*dy);
            i++;
            if(i<=dx)
            ch=6;
            else
            break one;
        }
    }
}

 

3. Write a java program to draw a line using mid-point Algorithm.



package computer_Graphics;
import java.awt.*;
import java.applet.*;
public class MidPointCircle extends Applet
{
    public void  paint(Graphics g)
     {
       int xc=120;
       int yc=120;
       int rad=100;
      int X=0;
      int Y=rad;
      int p = 5/4-rad;
      while(X<Y)
       {
         if(p<0)
          {
            X++;
            p=p+2*X+2+1;
          }
        else
          {
            X++;
            Y--;
            p = p+2*X+2+1-2*Y+2;
          }
           g.fillOval(xc+X,yc+Y,2,2);
           g.fillOval(xc+Y,yc-X,2,2);
           g.fillOval(xc-Y,yc+X,2,2);
           g.fillOval(xc-X,yc+Y,2,2);
           g.fillOval(xc+X,yc-Y,2,2);
           g.fillOval(xc+Y,yc+X,2,2);
           g.fillOval(xc-Y,yc-X,2,2);
           g.fillOval(xc-X,yc-Y,2,2);
       }
    }
}

4.Write a java program to perform translation on a line.

package computer_Graphics;
import java.awt.Graphics;
import javax.swing.JFrame;
class Translation extends JFrame{
  int x1 = 50;
  int y1 = 50;
  int x2 = 150;
  int y2 = 150;
  int tx = 200;
  int  ty = 200;
  int px1,px2,py1,py2;
  public Translation()
  {
    setTitle("Line Translation");
    setSize(500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    px1 = x1+tx;
    px2 = x2+tx;
    py1 = y1+ty;
    py2 = y2+ty;
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(px1, py1, px2, py2);
  }
  public static void main(String args[])
  {
    new Translation();
  }
}

5.Write a java program to perform scaling on a line about origin.

package computer_Graphics;
import java.awt.Graphics;
import javax.swing.JFrame;
class Scaling extends JFrame{
  int x1 = 50;
  int y1 = 50;
  int x2 = 100;
  int y2 = 100;
  int sx = 4;
  int  sy = 4;
  int px1,px2,py1,py2;
  public Scaling()
  {
    setTitle("Line Scaling");
    setSize(500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    px1 = x1*sx;
    px2 = x2*sx;
    py1 = y1*sy;
    py2 = y2*sy;
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(px1, py1, px2, py2);
  }
  public static void main(String args[])
  {
    new Scaling();
  }
}

 

7.Write a java program to perform scaling on a line at an arbitrary point.



package computer_Graphics;
import java.awt.Graphics;
import javax.swing.JFrame;
class ScalingAtArbitary extends JFrame{
  int x1 = 50;
  int y1 = 50;
  int x2 = 100;
  int y2 = 100;
  int sx = 4;
  int  sy = 4;
  //Arbitrary points are:
  int tx = 50;
  int ty = 50;
  int px1,px2,py1,py2;
  public ScalingAtArbitary()
  {
    setTitle("Line Scaling at an arbitrary point");
    setSize(1000,1000);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    px1 = x1*sx + tx*(1-sx);
    px2 = x2*sx + tx*(1-sx);
    py1 = y1*sy + ty*(1-sy);
    py2 = y2*sy + ty*(1-sy);
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.drawLine(x1, y1, x2, y2);
//    g.drawLine(px1, py1, px2, py2);
  }
  public static void main(String args[])
  {
    new ScalingAtArbitary();
  }
}

8.Write a java program to perform rotation on a line about origin in anticlockwise/ counterclockwise direction.

package computer_Graphics;
import java.awt.Graphics;
import javax.swing.JFrame;
class RotationCounterClockwise extends JFrame{
  int x1 = 50;
  int y1 = 50;
  int x2 = 100;
  int y2 = 100;
  int angle = 15;
  double t = angle * Math.PI / 180;
  int px1,px2,py1,py2;
  public RotationCounterClockwise()
  {
    setTitle("Rotation about origin in anticlockwise direction");
    setSize(500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    px1 = (int)(x1 * Math.cos(t) - y1 * Math.sin(t));
    px2 = (int)(x2*Math.cos(t) - y2 * Math.sin(t));
    py1 = (int)(y1*Math.cos(t) + x1*Math.sin(t));
    py2 = (int)(y2*Math.cos(t) + x2*Math.sin(t));
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(px1, py1, px2, py2);
  }
  public static void main(String args[])
  {
    new RotationCounterClockwise();
  }
}

9.Write a program to perform shearing on a rectangle through x-direction.



package computer_Graphics;
import java.awt.Graphics;
import javax.swing.JFrame;
class Shear_X extends JFrame{
  int x1 = 100;
  int y1 = 0;
  int x2 = 200;
  int y2 = 0;
  int x3 = 200;
  int y3 = 200;
  int x4 = 100;
  int y4 = 200;
  int shx = 2;
  int px1,px2,px3,px4;
  public Shear_X()
  {
    setTitle("X-Direction Shear of a rectangle");
    setSize(1000,1000);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    px1 = x1 + y1*shx;
    px2 = x2 + y2*shx;
    px3 = x3+ y3 *shx;
    px4 = x4+ y4 * shx;
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    //before shear
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(x2, y2, x3, y3);
    g.drawLine(x3, y3, x4, y4);
    g.drawLine(x4, y4, x1, y1);
    //after shear
    g.drawLine(px1, y1, px2, y2);
    g.drawLine(px2, y2, px3, y3);
    g.drawLine(px3, y3, px4, y4);
    g.drawLine(px4, y4, px1, y1);
  }
  public static void main(String args[])
  {
    new Shear_X();
  }
}

10.Write a program to perform shearing on a rectangle through y-direction.

package computer_Graphics;
import java.awt.Graphics;
import javax.swing.JFrame;
class Shear_Y extends JFrame{
  int x1 = 0;
  int y1 = 100;
  int x2 = 0;
  int y2 = 200;
  int x3 = 200;
  int y3 = 200;
  int x4 = 200;
  int y4 = 100;
  int shy = 2;
  int py1,py2,py3,py4;
  public Shear_Y()
  {
    setTitle("Y- Direction shear of a rectangle");
    setSize(1000,1000);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    py1 = y1 + x1*shy;
    py2 = y2 + x2*shy;
    py3 = y2 + x3*shy;
    py4 = y4 + x4 * shy;
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    //before shear
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(x2, y2, x3, y3);
    g.drawLine(x3, y3, x4, y4);
    g.drawLine(x4, y4, x1, y1);
    //after shear
    g.drawLine(x1, py1, x2, py2);
    g.drawLine(x2, py2, x3, py3);
    g.drawLine(x3, py3, x4, py4);
    g.drawLine(x4, py4, x1, py1);
  }
  public static void main(String args[])
  {
    new Shear_Y();
  }
}

  11.Write a program to perform shearing along XY axis

package computer_Graphics;
import java.awt.*;
import javax.swing.JFrame;
class Shear_XY extends JFrame{
   int x1=100; int y1=100; int x2=50; int y2=50; int shx=2; int shy=3;
   int a,b,c,d;
   public Shear_XY(){
          setTitle(" Shearing of a line about xy plane");
          setSize(400,450);
          setVisible(true);
          a=x1+shx*y1; b=y1+shy*x1;
          c=x2+shx*y2; d=y2+shy*x2;
                         }
    public void paint(Graphics g){
           g.drawLine(x1,y1,x2,y2);
           g.drawLine(a,b,c,d);
   }
      public static void main(String[]args){
         new Shear_XY();
      }
}

12.Write a program to perform reflection on a line along X-axis.

package computer_Graphics;
import java.awt.Graphics;
import javax.swing.JFrame;
class Reflection_Y_equals_X extends JFrame{
  int x1 = 50;
  int y1 = 50;
  int x2 = 70;
  int y2 = 250;
  int px1,px2,py1,py2;
  public Reflection_Y_equals_X_()
  {
    setTitle("Reflection of line on Y = X");
    setSize(500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    px1 = y1;
    px2 = y2;
    py1 = x1;
    py2 = x2;
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(px1, py1, px2, py2);
  }
  public static void main(String args[])
  {
    new Reflection_Y_equals_X();
  }
}

13.  Write a program to perform reflection on a line along Y-axis



package computer_Graphics;
import java.awt.Graphics;
import javax.swing.JFrame;
class Reflection_Y extends JFrame{
  int x1 = 50;
  int y1 = 50;
  int x2 = 50;
  int y2 = 150;
  int px1,px2;
  public Reflection_Y()
  {
    setTitle("Reflection of line on Y axis");
    setSize(500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    px1 = -x1+150;
    px2 = -x2+150;
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(px1, y1, px2, y2);
  }
  public static void main(String args[])
  {
    new Reflection_Y();
  }
}

14.Write a program to perform reflection on a line about origin.

package computer_Graphics;
import java.awt.Graphics;
import javax.swing.JFrame;
class Reflection_Origin extends JFrame{
  int x1 = 50;
  int y1 = 50;
  int x2 = 150;
  int y2 = 150;
  int px1,px2,py1,py2;
  public Reflection_Origin()
  {
    setTitle("Reflection of line about origin");
    setSize(500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    px1 = -x1+550;
    px2 = -x2+550;
    py1 = -y1+550;
    py2 = -y2+550;
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(px1, py1, px2, py2);
  }
  public static void main(String args[])
  {
    new Reflection_Origin();
  }
}

 

15.Write a program to perform reflection on a line about Y = X



package computer_Graphics;
import java.awt.Graphics;
import javax.swing.JFrame;
class Reflection_Y_equals_X extends JFrame{
  int x1 = 50;
  int y1 = 50;
  int x2 = 70;
  int y2 = 250;
  int px1,px2,py1,py2;
  public Reflection_Y_equals_X_Santo()
  {
    setTitle("Reflection of line on Y = X");
    setSize(500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    px1 = y1;
    px2 = y2;
    py1 = x1;
    py2 = x2;
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(px1, py1, px2, py2);
  }
  public static void main(String args[])
  {
    new Reflection_Y_equals_X();
  }
}



16.Write a program to perform reflection on a line about Y = -X.

package computer_Graphics;
import java.awt.Graphics;
import javax.swing.JFrame;
@SuppressWarnings("serial")
class Reflection_Y_equals_minus_X extends JFrame{
  int x1 = 50;
  int y1 = 50;
  int x2 = 70;
  int y2 = 250;
  int px1,px2,py1,py2;
  public Reflection_Y_equals_minus_X()
  {
    setTitle("Reflection of line on Y = -X");
    setSize(500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    px1 = -y1+350;
    px2 = -y2+350;
    py1 = -x1+350;
    py2 = -x2+350;
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(px1, py1, px2, py2);
  }
  public static void main(String args[])
  {
    new Reflection_Y_equals_minus_X();
  }
}