A Lab Report ON Computer Graphics by Aashutosh Kayastha TU Symbol No: 9182/18 Bim 5 Semester, Computer Graphics
A Lab Report ON Computer Graphics by Aashutosh Kayastha TU Symbol No: 9182/18 Bim 5 Semester, Computer Graphics
LAB REPORT
ON
Computer Graphics
By
Aashutosh Kayastha
Submitted to:
Mr. Manoj Giri
Department of Management
Computer Graphics
Minbhawan, Kathmandu
December, 2021
Table of Contents
Source Codes:
package com.aasu.cglab;
import javax.swing.*;
import java.awt.Graphics;
JLabel display;
int px1,px2,py1,py2;
public trans()
setSize(500,500); setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); }
@Override
1
public static void main(String args[])
{ new trans(); }}
Output:
2
2. WAP to illustrate “Reflection on X-axis”.
Source Codes:
package com.aasu.cglab;
import java.awt.Graphics;
import javax.swing.JFrame;
int a1,a2;
public ReflectionOnXaxis(){
setTitle("Reflection on X-axis");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a1= -y1+200;
a2=-y2+180;
setVisible(true);}
@Override
G.drawLine(x1,y1,x2,y2);
G.drawLine(150,a1,180,a2); }
new ReflectionOnXaxis();
}
}
3
Output:
4
3. WAP to illustrate “Reflection on Y-axis”.
Source Codes:
package com.aasu.cglab;
import java.awt.Graphics;
import javax.swing.JFrame;
int a1,a2;
public ReflectionOnYaxis(){
setTitle("Reflection on Y-axis");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); }
@Override
G.drawLine(x1,y1,x2,y2);
G.drawLine(a1,y1,a2,y2); }
new ReflectionOnYaxis();
5
Output:
6
4. WAP to illustrate “Reflection on X=Y”.
Source Codes:
package com.aasu.cglab;
import java.awt.Graphics;
import javax.swing.JFrame;
int a1,a2,b1,b2;
public ReflectionOnXequalY(){
setTitle("Reflection on X=Y");
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a1=y1; a2=y2;
setVisible(true); }
@Override
G.drawLine(x1,y1,x2,y2);
G.drawLine(a1,b1,a2,b2); }
new ReflectionOnXequalY();
7
Output:
8
5. WAP to illustrate “Reflection about Origin”.
Source Codes:
package com.aasu.cglab;
import java.awt.Graphics;
import javax.swing.JFrame;
int a1,a2,b1,b2;
public ReflectionAboutOrigin(){
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a1=-x1+600; a2=-x2+600;
setVisible(true); }
@Override
G.drawLine(x1,y1,x2,y2);
G.drawLine(a1,b1,a2,b2); }
9
Output:
10
6. WAP to illustrate “Reflection about Y= - X”.
Source Codes:
package com.aasu.cglab;
import java.awt.Graphics;
import javax.swing.JFrame;
int a1,a2,b1,b2;
public RefOnYequalminusX(){
setTitle("Reflection on Y = -X");
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a1=-y1+380; a2=-y2+380;
setVisible(true); }
@Override
G.drawLine(x1,y1,x2,y2);
G.drawLine(a1,b1,a2,b2); }
new RefOnYequalminusX();
11
Output:
12
7. WAP to illustrate “Line rotation about origin in clockwise direction”.
Source Codes:
package com.aasu.cglab;
import java.awt.Graphics;
import javax.swing.JFrame;
int a1,a2,b1,b2;
public Clockwise(){
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a1 = (int)(x1*Math.cos(t) + y1*Math.sin(t));
a2 = (int)(x2*Math.cos(t) + y2*Math.sin(t));
b1 = (int)(y1*Math.cos(t) - x1*Math.sin(t));
b2 = (int)(y2*Math.cos(t) - x2*Math.sin(t));
setVisible(true); }
@Override
new Clockwise(); }}
13
Ouput:
14
8. WAP to illustrate “Line rotation about origin in anti-clockwise direction”.
Source Codes:
package com.aasu.cglab;
import java.awt.Graphics;
import javax.swing.JFrame;
int a1,a2,b1,b2;
public anticlockwise(){
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a2 = (int)(x2*Math.cos(t) - y2 * Math.sin(t));
b1 = (int)(y1*Math.cos(t) + x1*Math.sin(t));
b2 = (int)(y2*Math.cos(t) + x2*Math.sin(t));
setVisible(true); }
new anticlockwise();
}}
15
Output:
16
9. WAP to illustrate “Line Scaling”.
Source Codes:
package com.aasu.cglab;
import java.awt.Graphics;
import javax.swing.JFrame;
int sx = 5; int sy = 5;
int px1,px2,py1,py2;
public scalling(){
setTitle("Line Scaling");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); }
new scalling(); }}
17
Output:
18
10. WAP to illustrate “Line Scaling on arbitrary points”.
Source Code:
package com.aasu.cglab;
package com.mycompany.javaapp.graphicsjava;
import java.awt.*;
class lines extends Frame
{
int x1=60; int y1=70; int x2=100; int y2=90;int sx=4;
int sy=6; int img1, img2, img3, img4;
public lines(){
setTitle("Scaling with arbitrary points");
setSize(400,450);
setVisible(true);
}
public void draws(int x3,int y3){
img1 = sx*x1+x2*(1-sx); img2 = sy*y1+y2*(1-sy);
img3 =sx*x2+x3*(1-sx); img4 =sy*y2+y3*(1-sy);
}
@Override
public void paint(Graphics g){
g.drawLine(x1,y1,x2,y2); g.drawLine(img1,img2,img3,img4);
}
}
public class arbitaryscaling{
public static void main(String[] args){
lines ob=new lines();
ob.draws(30,15);
}
}
19
OUTPUT:
20
11. WAP to illustrate “Shearing through X-axis”.
Source Codes:
package com.aasu.cglab;
import java.awt.Graphics;
import javax.swing.JFrame;
int px1,px2,px3,px4;
public shearxaxis(){
setSize(2000,2000);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
px1 = x1 + y1*shx;
px2 = x2 + y2*shx;
setVisible(true);}
//before shear
//after shear
21
g.drawLine(px1, y1, px2, y2);
new shearxaxis(); }}
Output:
22
12. WAP to illustrate “Shearing through Y-axis”.
Source Codes:
package com.aasu.cglab;
import java.awt.Graphics;
import javax.swing.JFrame;
int shy = 3;
int py1,py2,py3,py4;
public shearyaxis(){
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);}
@Override
//before shear
23
g.drawLine(x4, y4, x1, y1);
//after shear
new shearyaxis();}}
Output:
24
13. WAP to illustrate “Shearing of line through XYplane”.
Source Codes:
package com.aasu.cglab;
import java.awt.Graphics;
import javax.swing.JFrame;
int a,b,c,d;
public shearxy(){
setSize(400,450); setVisible(true);
// After Shearing
a=x1+shx*y1; b=y1+shy*x1;
c=x2+shx*y2; d=y2+shy*x2; }
@Override
g.drawLine(x1,y1,x2,y2);
g.drawLine(a,b,c,d); }
new shearxy(); }}
25
Output:
26
14. WAP to show Midpoint Circle Drawing Algorithm.
Source Codes:
import java.awt.*;
import javax.swing.JFrame;
import java.util.Scanner;
int x,y,p;
x=0; y=r;
fill(g,x,y,xc,yc);
p=1-r;
while(x<y){
x=x+1;
if(p<0){
p=p+2*x+1;}
else{
y=y-1;
p=p+2*x+1-2*y; }
fill(g,x,y,xc,yc); }}
g.fillOval(xc+x,yc+y,5,5);
g.fillOval(xc+x,yc-y,5,5);
g.fillOval(xc-x,yc+y,5,5);
g.fillOval(xc-x,yc-y,5,5);
g.fillOval(xc+y,yc+x,5,5);
27
g.fillOval(xc+y,yc-x,5,5);
g.fillOval(xc-y,yc+x,5,5);
g.fillOval(xc-y,yc-x,5,5); }
int xc=sc.nextInt();
int yc=sc.nextInt();
int r=sc.nextInt();
f.add(c);
f.setSize(800,800);
f.setVisible(true);}}
Output:
28
15. WAP to show General Circle Drawing Algorithm.
Source Codes:
package com.aasu.cglab;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
JTextField tf1,tf2,tf3;
Graphics g;
public GCDA(){
this.setSize(800,800);
lb1.setBounds(10,10,50,25);
lb2.setBounds(10,40,50,25);
lb3.setBounds(10,70,50,25);
tf1=new JTextField();
tf1.setBounds(70,10,80,25);
tf2=new JTextField();
tf2.setBounds(70,40,80,25);
tf3=new JTextField();
tf3.setBounds(70,70,80,25);
jb=new JButton("Draw");
jb.setBounds(70,100,80,25);
29
jb.addActionListener(this);
this.add(lb1); this.add(lb2);
this.add(lb3); this.add(tf1);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
g=this.getGraphics();}
new GCDA();}
@Override
int r=Integer.parseInt(tf1.getText());
int h=Integer.parseInt(tf2.getText());
int k=Integer.parseInt(tf3.getText());
for(int i=xk;i>=0;i--){
xk--;
yk=(int)Math.round(Math.sqrt((r*r)-(xk*xk)));
}}}
30
Output:
31
16. WAP to show DDA Algorithm.
Source Codes:
package com.aasu.cglab;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.lang.Math;
JTextField tf1,tf2,tf3,tf4;
JLabel lbl1,lbl2,lbl3,lbl4;
public DDA(){
this.setSize(600,400);
this.setTitle("DDA Algorithm");
lbl1.setBounds(10,10,30,25);
lbl2.setBounds(10,40,30,25);
lbl3.setBounds(10,70,30,25);
lbl4.setBounds(10,100,30,25);
tf1=new JTextField();
tf1.setBounds(70,10,80,25);
tf2=new JTextField();
tf2.setBounds(70,40,80,25);
32
tf3=new JTextField();
tf3.setBounds(70,70,80,25);
tf4=new JTextField();
tf4.setBounds(70,100,80,25);
jb=new JButton("Draw");
jb.setBounds(70,140,80,25);
jb.addActionListener(this);
this.add(lbl1); this.add(lbl2);
this.add(tf2); this.add(tf3);
this.add(tf4); this.add(jb);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
g=this.getGraphics();}
@Override
int x1=Integer.parseInt(tf1.getText());
int y1=Integer.parseInt(tf2.getText());
int x2=Integer.parseInt(tf3.getText());
int y2=Integer.parseInt(tf4.getText());
double m=(double)dely/delx;
System.out.println("m: "+m);
if(Math.abs(delx)>=Math.abs(dely)){
33
xk=x1; yk=y1;
xk=x2; yk=y2;
for(int j=x2;j>x1;j++){
xk++; yk=yk+m;
if(x1>x2){
for(int j=x1;j>x2;j--){
xk--;
yk=yk+m;
else{
for(int i=x1;i<x2;i++){
xk++; yk=yk+m;
}}}
if(Math.abs(dely)>Math.abs(delx)){
xk=x1; yk=y1;
xk=x2; yk=y2;
for(int j=y2;j>y1;j++){
yk++; xk=xk+(1/m);
34
System.out.println("xk: "+xk+" and yk:"+yk+" and
ykk: "+Math.round(yk));
}}
if(y1>y2){
for(int j=y1;j>y2;j--){
yk--; xk=xk+(1/m);
else{
for(int i=y1;i<y2;i++){
yk++; xk=xk+(1/m);
Output:
35
17. WAP to show Bresenhem Algorithm.
Source Codes:
package com.aasu.cglab;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;
JTextField tf1,tf2,tf3,tf4;
JLabel lbl1,lbl2,lbl3,lbl4;
public BLA(){
this.setTitle("BLA Algorithm");
this.setSize(600,400);
lbl1.setBounds(10,10,30,25);
lbl2.setBounds(10,40,30,25);
lbl3.setBounds(10,70,30,25);
lbl4.setBounds(10,100,30,25);
tf1=new JTextField();
tf1.setBounds(70,10,80,25);
tf2=new JTextField();
tf2.setBounds(70,40,80,25);
tf3=new JTextField();
36
tf3.setBounds(70,70,80,25);
tf4=new JTextField();
tf4.setBounds(70,100,80,25);
jb=new JButton("Draw");
jb.setBounds(70,140,80,25);
jb.addActionListener(this);
this.add(lbl1);
this.add(lbl2);
this.add(lbl3);
this.add(lbl4);
this.add(tf1);
this.add(tf2);
this.add(tf3);
this.add(tf4);
this.add(jb);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true); g=this.getGraphics();}
new BLA();}
@Override
int x1=Integer.parseInt(tf1.getText());
int y1=Integer.parseInt(tf2.getText());
int x2=Integer.parseInt(tf3.getText());
int y2=Integer.parseInt(tf4.getText());
int delx=x2-x1;
int dely=y2-y1;
int dx=Math.abs(delx);
37
int dy=Math.abs(dely);
int Pk,xk=x1,yk=y1;
if(dx>dy){
Pk=2*dy-dx;
do{
if(Pk>=0){
if(delx>0)
xk=xk+1;
if(delx<=0)
xk=xk-1;
if(dely>0)
yk=yk+1;
if(dely<=0)
yk=yk-1;
Pk=Pk+(2*dy)-(2*dx);}
else{
if(delx>0)
xk=xk+1;
if(delx<=0)
xk=xk-1;
yk=yk;
Pk=Pk+(2*dy);}
}while(xk!=x2);
else{
Pk=2*dx-dy;
38
do{
if(Pk>=0){
if(delx>0)
xk=xk+1;
if(delx<=0)
xk=xk-1;
if(dely>0)
yk=yk+1;
if(dely<=0)
yk=yk-1;
Pk=Pk+(2*dx)-(2*dy);}
else{
xk=xk;
if(dely>0)
yk=yk+1;
if(dely<=0)
yk=yk-1;
Pk=Pk+(2*dx);}
}while(yk!=y2);
39
Output:
40
18. WAP to show Boundary Filling Algorithm
Source Codes:
package com.aasu.cglab;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import javax.swing.JFrame;
raster.setPixel(x, y, fillColor);
41
public void paint(Graphics g) {
big2d.setColor(Color.red);
g2d.drawImage(bi, 0, 0, null); }
frame.setTitle("Boundary Fill");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setVisible(true); }}
Output:
42
19. WAP to show Flood fill Algorithm.
Source Codes:
package com.aasu.cglab;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(fill);
frame.pack();
frame.setVisible(true); }
public floodfill() {
setMinimumSize(getPreferredSize());
g2=image.createGraphics();
g2.setColor(Color.blue);
addMouseListener(new MouseAdapter() {
43
floodFill(e.getX(), e.getY(), image.getRGB(e.getX(), e.getY()));
}});}
@Override
g.drawImage(image, 0, 0, null); }
if(image.getRGB(seedX, seedY)==rgb) {
image.setRGB(seedX,seedY,Color.yellow.getRGB());
update(getGraphics());
floodFill(seedX-1,seedY+1,rgb);
floodFill(seedX+1,seedY-1,rgb);
floodFill(seedX+1,seedY+1,rgb);
floodFill(seedX,seedY-1,rgb);
floodFill(seedX,seedY+1,rgb);
floodFill(seedX-1,seedY,rgb);
floodFill(seedX+1,seedY,rgb);
} } }
Output:
44
20. WAP to illustrate “Reflection about Y=mx+c”
SOURCE CODE:
package com.mycompany.javaapp.graphicsjava;
import java.awt.*;
class reflect extends Frame {
int y1=100; int x1=180; int y2=110; int x2=100;
int m=1,b=1;int img1,img2,img3,img4;
public reflect()
{
setTitle("line reflection on y=mx+c");
setSize(600,600);
setVisible(true);
}
public void ymxc()
{
img1=((1-m^2)*x1+2*m*y1-2*m*b)/m^2+1;
img2 =((1-m^2)*x2+2*m*y2-2*m*b)/m^2+1;
img3=((m^2-1)*y1+2*m*x1+2*b)/m^2+1;
img4=((m^2-1)*y2+2*m*x2+2*b)/m^2+1;
}
@Override
public void paint(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
g.drawLine(img1,img2,img3,img4); }
}
public class reflectymxc{
public static void main(String[] args) {
reflect ob=new reflect();
ob.ymxc();
45
Output:
46
21. WAP to illustrate Drawing Strings.
SOURCE CODE:
package com.aasu.cglab;
package com.mycompany.javaapp.graphicsjava;
import java.awt.*;
public class Drawingstrings extends Frame {
public Drawingstrings(){
setSize(500,500);
setTitle("Printing string with color other than black");
setVisible(true);
Font f = new Font("CALIBRI",Font.BOLD,64);
setFont(f);
}
@Override
public void paint( Graphics g){
g.setColor(Color.pink);
}
public static void main(String args[]){
new Drawingstrings();
}
}
47
OUTPUT
48