0% found this document useful (0 votes)
100 views

A Lab Report ON Computer Graphics by Aashutosh Kayastha TU Symbol No: 9182/18 Bim 5 Semester, Computer Graphics

This document is a lab report submitted by Aashutosh Kayastha to his professor Mr. Manoj Giri at Nepal Commerce Campus. The report contains 22 programs written in Java to illustrate various 2D geometric transformations and computer graphics algorithms. These include programs demonstrating translation, reflection, rotation, scaling, shearing, midpoint circle algorithm, DDA algorithm, Bresenham's line algorithm, boundary filling algorithm, and flood fill algorithm. Each program contains the source code, output, and brief explanation. The programs aim to fulfill the requirements for the course on Computer Graphics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

A Lab Report ON Computer Graphics by Aashutosh Kayastha TU Symbol No: 9182/18 Bim 5 Semester, Computer Graphics

This document is a lab report submitted by Aashutosh Kayastha to his professor Mr. Manoj Giri at Nepal Commerce Campus. The report contains 22 programs written in Java to illustrate various 2D geometric transformations and computer graphics algorithms. These include programs demonstrating translation, reflection, rotation, scaling, shearing, midpoint circle algorithm, DDA algorithm, Bresenham's line algorithm, boundary filling algorithm, and flood fill algorithm. Each program contains the source code, output, and brief explanation. The programs aim to fulfill the requirements for the course on Computer Graphics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

A

LAB REPORT

ON

Computer Graphics

By

Aashutosh Kayastha

TU Symbol No: 9182/18

BIM 5th Semester, Computer Graphics

Submitted to:
Mr. Manoj Giri
Department of Management

Nepal Commerce Campus

In partial fulfillment of the requirements for the Course

Computer Graphics

Minbhawan, Kathmandu

December, 2021
Table of Contents

1. WAP TO ILLUSTRATE “TRANSLATION (LINE TRANSLATION)”. ...................................................................... 1


2. WAP TO ILLUSTRATE “REFLECTION ON X-AXIS”. .......................................................................................... 3
3. WAP TO ILLUSTRATE “REFLECTION ON Y-AXIS”. .......................................................................................... 5
4. WAP TO ILLUSTRATE “REFLECTION ON X=Y”................................................................................................ 7
5. WAP TO ILLUSTRATE “REFLECTION ABOUT ORIGIN”. .................................................................................. 9
6. WAP TO ILLUSTRATE “REFLECTION ABOUT Y= - X”. ................................................................................... 11
7. WAP TO ILLUSTRATE “LINE ROTATION ABOUT ORIGIN IN CLOCKWISE DIRECTION”. ................................ 13
8. WAP TO ILLUSTRATE “LINE ROTATION ABOUT ORIGIN IN ANTI-CLOCKWISE DIRECTION”. ....................... 15
9. WAP TO ILLUSTRATE “LINE SCALING”. ....................................................................................................... 17
10. WAP TO ILLUSTRATE “LINE SCALING ON ARBITRARY POINTS”.............................................................. 19
11. WAP TO ILLUSTRATE “SHEARING THROUGH X-AXIS”. ........................................................................... 21
12. WAP TO ILLUSTRATE “SHEARING THROUGH Y-AXIS”. ........................................................................... 23
13. WAP TO ILLUSTRATE “SHEARING OF LINE THROUGH XYPLANE”. ......................................................... 25
14. WAP TO SHOW MIDPOINT CIRCLE DRAWING ALGORITHM. ................................................................. 27
15. WAP TO SHOW GENERAL CIRCLE DRAWING ALGORITHM. ................................................................... 29
16. WAP TO SHOW DDA ALGORITHM. ........................................................................................................ 32
17. WAP TO SHOW BRESENHEM ALGORITHM. .......................................................................................... 36
18. WAP TO SHOW BOUNDARY FILLING ALGORITHM................................................................................. 41
19. WAP TO SHOW FLOOD FILL ALGORITHM. ............................................................................................. 43
20. WAP TO ILLUSTRATE “REFLECTION ABOUT Y=MX+C” ........................................................................... 45
21. WAP TO ILLUSTRATE DRAWING STRINGS. ............................................................................................. 47
1. WAP to illustrate “Translation (Line Translation)”.

Source Codes:

package com.aasu.cglab;

import javax.swing.*;

import java.awt.Graphics;

class trans extends JFrame{

JLabel display;

int x1 = 50; int y1 = 200;

int x2 = 200; int y2 = 100;

int tx = 100; //Translation Point

int ty = 200; //Translation Point

int px1,px2,py1,py2;

public trans()

display = new JLabel("Translation Example");

display.setBounds(20, 20, 100, 100);

add(display); setTitle("Line Translation");

setSize(500,500); setLayout(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

px1 = x1+tx; px2 = x2+tx;

py1 = y1+ty; py2 = y2+ty;

setVisible(true); }

@Override

public void paint(Graphics g)

g.drawLine(x1, y1, x2, y2);

g.drawLine(px1, py1, px2, py2); }

1
public static void main(String args[])

{ new trans(); }}

Output:

Output i Line Translation

2
2. WAP to illustrate “Reflection on X-axis”.

Source Codes:

package com.aasu.cglab;

import java.awt.Graphics;

import javax.swing.JFrame;

public class ReflectionOnXaxis extends JFrame{

int x1=180; int y1=110;

int x2=150; int y2=100;

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

public void paint(Graphics G){

G.drawLine(x1,y1,x2,y2);

G.drawLine(150,a1,180,a2); }

public static void main(String[]args){

new ReflectionOnXaxis();
}
}

3
Output:

Output ii Reflection on X-axis

4
3. WAP to illustrate “Reflection on Y-axis”.

Source Codes:

package com.aasu.cglab;

import java.awt.Graphics;

import javax.swing.JFrame;

public class ReflectionOnYaxis extends JFrame{

int x1=40; int y1=60;

int x2=60; int y2=180;

int a1,a2;

public ReflectionOnYaxis(){

setTitle("Reflection on Y-axis");

setSize(500,500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

a1= -x1+170; a2=-x2+170;

setVisible(true); }

@Override

public void paint(Graphics G){

G.drawLine(x1,y1,x2,y2);

G.drawLine(a1,y1,a2,y2); }

public static void main(String[]args){

new ReflectionOnYaxis();

5
Output:

Output iii Reflection on Y-axis

6
4. WAP to illustrate “Reflection on X=Y”.

Source Codes:

package com.aasu.cglab;

import java.awt.Graphics;

import javax.swing.JFrame;

public class ReflectionOnXequalY extends JFrame{

int x1=60; int y1=70;

int x2=80; int y2=280;

int a1,a2,b1,b2;

public ReflectionOnXequalY(){

setTitle("Reflection on X=Y");

setSize(600,600);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

a1=y1; a2=y2;

b1= x1; b2=x2;

setVisible(true); }

@Override

public void paint(Graphics G){

G.drawLine(x1,y1,x2,y2);

G.drawLine(a1,b1,a2,b2); }

public static void main(String[]args){

new ReflectionOnXequalY();

7
Output:

Output iv Reflection on X=Y

8
5. WAP to illustrate “Reflection about Origin”.

Source Codes:

package com.aasu.cglab;

import java.awt.Graphics;

import javax.swing.JFrame;

public class ReflectionAboutOrigin extends JFrame{

int x1=70; int y1=70;

int x2=200; int y2=200;

int a1,a2,b1,b2;

public ReflectionAboutOrigin(){

setTitle("Reflection about Origin");

setSize(500,500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

a1=-x1+600; a2=-x2+600;

b1= -y1+600; b2=-x2+600;

setVisible(true); }

@Override

public void paint(Graphics G){

G.drawLine(x1,y1,x2,y2);

G.drawLine(a1,b1,a2,b2); }

public static void main(String[]args){

new ReflectionAboutOrigin ();

9
Output:

Output v Reflection about origin

10
6. WAP to illustrate “Reflection about Y= - X”.

Source Codes:

package com.aasu.cglab;

import java.awt.Graphics;

import javax.swing.JFrame;

public class RefOnYequalminusX extends JFrame{

int x1=60; int y1=60;

int x2=90; int y2=280;

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;

b1= -x1+380; b2=-x2+380;

setVisible(true); }

@Override

public void paint(Graphics G){

G.drawLine(x1,y1,x2,y2);

G.drawLine(a1,b1,a2,b2); }

public static void main(String[]args){

new RefOnYequalminusX();

11
Output:

Output vi reflection Y=-X

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;

class Clockwise extends JFrame{

int x1 = 120; int y1 = 220;

int x2 = 250; int y2 = 350;

int angle = 50;

double t = angle * Math.PI / 180;

int a1,a2,b1,b2;

public Clockwise(){

setTitle("Line rotation about origin in clockwise direction");

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

public void paint(Graphics g){

g.drawLine(x1, y1, x2, y2);

g.drawLine(a1, b1, a2, b2); }

public static void main(String args[]){

new Clockwise(); }}

13
Ouput:

Output vii Line rotation about origin in clockwise direction

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;

class anticlockwise extends JFrame{

int x1 = 90; int y1 = 180; int x2 = 180; int y2 = 260;

int angle = 30;

double t = angle * Math.PI / 180;

int a1,a2,b1,b2;

public anticlockwise(){

setTitle("Rotation about origin in anticlockwise direction");

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); }

public void paint(Graphics g){

g.drawLine(x1, y1, x2, y2);

g.drawLine(a1, b1, a2, b2); }

public static void main(String args[]) {

new anticlockwise();

}}

15
Output:

Output viii Rotation about origin in anticlockwise direction

16
9. WAP to illustrate “Line Scaling”.

Source Codes:

package com.aasu.cglab;

import java.awt.Graphics;

import javax.swing.JFrame;

class scalling extends JFrame{

int x1 = 60; int y1 = 60;

int x2 = 100; int y2 = 100;

int sx = 5; int sy = 5;

int px1,px2,py1,py2;

public scalling(){

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 scalling(); }}

17
Output:

Output ix Line scaling

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:

Output x Line Scaling on arbitrary points

20
11. WAP to illustrate “Shearing through X-axis”.

Source Codes:

package com.aasu.cglab;

import java.awt.Graphics;

import javax.swing.JFrame;

class shearxaxis extends JFrame{

int x1 = 150; int y1 = 0; int x2 = 300;

int y2 = 0; int x3 = 300; int y3 = 300;

int x4 = 150; int y4 = 300; int shx = 3;

int px1,px2,px3,px4;

public shearxaxis(){

setTitle("X-Direction Shear of a rectangle");

setSize(2000,2000);

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

21
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 shearxaxis(); }}

Output:

Output xi Shearing through X-axis

22
12. WAP to illustrate “Shearing through Y-axis”.

Source Codes:

package com.aasu.cglab;

import java.awt.Graphics;

import javax.swing.JFrame;

class shearyaxis extends JFrame{

int x1 = 0; int y1 = 50;

int x2 = 0; int y2 = 100;

int x3 = 100; int y3 = 100;

int x4 = 100; int y4 = 50;

int shy = 3;

int py1,py2,py3,py4;

public shearyaxis(){

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);}

@Override

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);

23
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 shearyaxis();}}

Output:

Output xii Shearing through Y-axis

24
13. WAP to illustrate “Shearing of line through XYplane”.

Source Codes:

package com.aasu.cglab;

import java.awt.Graphics;

import javax.swing.JFrame;

class shearxy extends JFrame{

int x1=150; int y1=150;

int x2=50; int y2=50;

int shx=2; int shy=3;

int a,b,c,d;

public shearxy(){

setTitle(" Shearing of a line about xy plane");

setSize(400,450); setVisible(true);

// After Shearing

a=x1+shx*y1; b=y1+shy*x1;

c=x2+shx*y2; d=y2+shy*x2; }

@Override

public void paint(Graphics g){

g.drawLine(x1,y1,x2,y2);

g.drawLine(a,b,c,d); }

public static void main(String[]args){

new shearxy(); }}

25
Output:

Output xiii Shearing of line through XYplane

26
14. WAP to show Midpoint Circle Drawing Algorithm.

Source Codes:

import java.awt.*;

import javax.swing.JFrame;

import java.util.Scanner;

public class midpoint extends Canvas{

static int xc,yc,r;

midpoint(int xc,int yc,int r) {

this.xc=xc; this.yc=yc; this.r=r; }

public void paint(Graphics g){

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); }}

public void fill(Graphics g,int x,int y,int xc,int 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); }

public static void main(String args[]){

Scanner sc=new Scanner(System.in);

System.out.println("Enter center (xc,yc): ");

int xc=sc.nextInt();

int yc=sc.nextInt();

System.out.println("Enter radius r: ");

int r=sc.nextInt();

midpoint c = new midpoint(xc,yc,r);

JFrame f=new JFrame();

f.add(c);

f.setSize(800,800);

f.setVisible(true);}}

Output:

Output xiv Midpoint Circle Drawing Algorithm

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.*;

public class GCDA extends JFrame implements ActionListener{

JTextField tf1,tf2,tf3;

JLabel lb1,lb2,lb3; JButton jb;

Graphics g;

public GCDA(){

this.setTitle("General Circle algorithm");

this.setSize(800,800);

lb1=new JLabel("Radius ");

lb1.setBounds(10,10,50,25);

lb2=new JLabel("X Co: ");

lb2.setBounds(10,40,50,25);

lb3=new JLabel("Y Co: ");

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.add(tf2); this.add(tf3); this.add(jb);

this.setLayout(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

g=this.getGraphics();}

public static void main(String[] args){

new GCDA();}

@Override

public void actionPerformed(ActionEvent ae){

int r=Integer.parseInt(tf1.getText());

int h=Integer.parseInt(tf2.getText());

int k=Integer.parseInt(tf3.getText());

System.out.println("Radius:"+r+" h: "+h+" k: "+k);

int xk=r; int yk=0;

for(int i=xk;i>=0;i--){

g.fillRect(xk+h, yk+k, 1, 1);

g.fillRect(-xk+h, yk+k, 1, 1);

g.fillRect(-xk+h, -yk+k, 1, 1);

g.fillRect(xk+h, -yk+k, 1, 1);

System.out.println("xk: "+(xk+h)+" yk: "+(yk+k)+" || "+"xk:


"+(-xk+h)+" -yk: "+(yk+k)+" || "+"xk: "+(-xk+h)+" yk: "+(-yk+k)+" ||
"+"xk: "+(xk+h)+" yk: "+(-yk+k)+" || ");

xk--;

yk=(int)Math.round(Math.sqrt((r*r)-(xk*xk)));

}}}

30
Output:

Output xv General Circle Drawing Algorithm

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;

public class DDA extends JFrame implements ActionListener{

JTextField tf1,tf2,tf3,tf4;

JLabel lbl1,lbl2,lbl3,lbl4;

JButton jb; Graphics g;

double xk; double yk;

public DDA(){

this.setSize(600,400);

this.setTitle("DDA Algorithm");

lbl1=new JLabel("X1: ");

lbl1.setBounds(10,10,30,25);

lbl2=new JLabel("Y1: ");

lbl2.setBounds(10,40,30,25);

lbl3=new JLabel("X2: ");

lbl3.setBounds(10,70,30,25);

lbl4=new JLabel("Y2: ");

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(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();}

public static void main(String[] args){

DDA obj= new DDA();}

@Override

public void actionPerformed(ActionEvent ae){

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;

double m=(double)dely/delx;

System.out.println("x1: "+x1+" y1: "+y1+" x2: "+x2+" y2: "+y2+"


delx: "+delx+" dely: "+dely);

System.out.println("m: "+m);

if(Math.abs(delx)>=Math.abs(dely)){

33
xk=x1; yk=y1;

if(x1>x2 && m>0){

xk=x2; yk=y2;

for(int j=x2;j>x1;j++){

g.fillRect((int) xk, (int) Math.round(yk),1,1);

xk++; yk=yk+m;

System.out.println("xk: "+xk+" and yk:"+yk+" and


ykk: "+Math.round(yk));}}

if(x1>x2){

for(int j=x1;j>x2;j--){

g.fillRect((int) xk, (int) Math.round(yk),1,1);

xk--;

yk=yk+m;

System.out.println("xk: "+xk+" and yk:"+yk+" and


ykk: "+Math.round(yk));} }

else{

for(int i=x1;i<x2;i++){

g.fillRect((int) xk, (int) Math.round(yk),1,1);

xk++; yk=yk+m;

System.out.println("xk: "+xk+" and yk:"+yk+" and ykk:


"+Math.round(yk));

}}}

if(Math.abs(dely)>Math.abs(delx)){

xk=x1; yk=y1;

if(y1>y2 && m>0){

xk=x2; yk=y2;

for(int j=y2;j>y1;j++){

g.fillRect((int) xk, (int) Math.round(yk),1,1);

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--){

g.fillRect((int) xk, (int) Math.round(yk),1,1);

yk--; xk=xk+(1/m);

System.out.println("xk: "+xk+" and yk:"+yk+" and


ykk: "+Math.round(xk));}}

else{

for(int i=y1;i<y2;i++){

g.fillRect((int) xk, (int) Math.round(yk),1,1);

yk++; xk=xk+(1/m);

System.out.println("xk: "+xk+" and yk:"+yk+" and


ykk: "+Math.round(xk)); }}}}}

Output:

Output xvi DDA Algorithm.

35
17. WAP to show Bresenhem Algorithm.

Source Codes:

package com.aasu.cglab;

import java.awt.event.*;

import javax.swing.*;

import java.awt.Graphics;

public class BLA extends JFrame implements ActionListener{

JTextField tf1,tf2,tf3,tf4;

JLabel lbl1,lbl2,lbl3,lbl4;

JButton jb; Graphics g;

public BLA(){

this.setTitle("BLA Algorithm");

this.setSize(600,400);

lbl1=new JLabel("X1: ");

lbl1.setBounds(10,10,30,25);

lbl2=new JLabel("Y1: ");

lbl2.setBounds(10,40,30,25);

lbl3=new JLabel("X2: ");

lbl3.setBounds(10,70,30,25);

lbl4=new JLabel("Y2: ");

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();}

public static void main(String[] args){

new BLA();}

@Override

public void actionPerformed(ActionEvent ae){

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{

System.out.println("xk: "+xk+" yk: "+yk+" Pk: "+Pk);

g.fillRect(xk, yk, 1, 1);

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);

System.out.println("xk: "+xk+" yk: "+yk+" Pk: "+Pk);

g.fillRect(xk, yk, 1, 1);}

else{

Pk=2*dx-dy;

38
do{

System.out.println("xk: "+xk+" yk: "+yk+" Pk: "+Pk);

g.fillRect(xk, yk, 1, 1);

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);

System.out.println("xk: "+xk+" yk: "+yk+" Pk: "+Pk);

g.fillRect(xk, yk, 1, 1);}}}

39
Output:

Output xvii Bresenhem Algorithm

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;

public class boundaryfill extends JFrame {

public static int width = 500;

public static int height = 500;

public void BoundaryFilling(int x, int y, Color fill, Color Boundary,


WritableRaster raster){

int[] fillColor = {fill.getRed(), fill.getGreen(),


fill.getBlue(),fill.getAlpha()};

int[] BoundaryColor = {Boundary.getRed(),


Boundary.getGreen(),Boundary.getBlue(), Boundary.getAlpha()};

int[] curColor = raster.getPixel(x,y,new int[]{255,255,255,255}) ;

if ((!isEqualRgba(curColor, BoundaryColor)) &&


(!isEqualRgba(curColor,fillColor))){

raster.setPixel(x, y, fillColor);

BoundaryFilling(x + 1, y, fill, Boundary, raster);

BoundaryFilling(x - 1, y, fill, Boundary, raster);

BoundaryFilling(x, y + 1, fill, Boundary, raster);

BoundaryFilling(x, y - 1, fill, Boundary, raster); } }

private boolean isEqualRgba(int[] pix1, int[] pix2) {

return pix1[0] == pix2[0] && pix1[1] == pix2[1] && pix1[2] == pix2[2]


&& pix1[3] == pix2[3]; }

41
public void paint(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

BufferedImage bi= new


BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);

Graphics2D big2d = bi.createGraphics();

big2d.setColor(Color.red);

big2d.drawRect(332, 332, 25, 25);

WritableRaster raster = bi.getRaster();

BoundaryFilling(333, 333, Color.yellow,Color.red, raster);

g2d.drawImage(bi, 0, 0, null); }

public static void main(String[] args) {

boundaryfill frame = new boundaryfill();

frame.setTitle("Boundary Fill");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(800,600);

frame.setVisible(true); }}

Output:

Output xviii Boundary Filling Algorithm

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;

public class floodfill extends JPanel {

private BufferedImage image;

private Graphics2D g2;

public static void main(String[] args) {

JFrame frame = new JFrame("FloodFill");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

floodfill fill=new floodfill();

frame.add(fill);

frame.pack();

frame.setVisible(true); }

public floodfill() {

image= new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);

setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));

setMinimumSize(getPreferredSize());

g2=image.createGraphics();

g2.setColor(Color.blue);

g2.drawRect(150, 150, 150, 150);

addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

43
floodFill(e.getX(), e.getY(), image.getRGB(e.getX(), e.getY()));

}});}

@Override

public void paintComponent(Graphics g) {

g.drawImage(image, 0, 0, null); }

public void floodFill(int seedX, int seedY, int rgb) {

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+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:

Output xix Flood fill Algorithm

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:

Output xx Reflection about Y=mx+c

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);

g.drawString("HI! My Name is AASU D ACE♠",100,100);

}
public static void main(String args[]){
new Drawingstrings();
}
}

47
OUTPUT

Output xxi Drawing Strings.

48

You might also like