Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 82

TRIBHUVAN UNIVERSITY

ASIAN SCHOOL OF MANAGEMENT AND TECHNOLOGY


Gongabu, Kathmandu

LAB REPORT ON COMPUTER GRAPHICS

SUBMITTED BY: SUBMITTED TO:


SANJAYA KHANAL Er. MANOJ GIRI
5TH SEMESTER
SYMBOL NO: 1121/20
Contents
1. Draw a line using DDA……………………………………………………………………………………..1
2. Draw a line using BLA………………………………………………………………………………………4
3. General circle drawing algorithm…………………………………………………………………….7
4. Midpoint circle drawing algorithm………………………………………………………………….11
5. Translation………………………………………………………………………………………………………13
6. 2D Scaling……………………………………………………………………………………………………….16
7. 2D Rotation…………………………………………………………………………………………………….19
8. Reflection in x-axis………………………………………………………………………………………….23
9. Reflection in Y axis………………………………………………………………………………………….26
10. Reflection in origin……………………………………………………………………………………….29
11. Reflection in Reflection in Y=X………………………………………………………………………33
12. Reflection in y= mx+c……………………………………………………………………………………37
13. Shearing in X…………………………………………………………………………………………………43
14. Shearing in xy……………………………………………………………………………………………….46
15. Line clipping algorithm………………………………………………………………………………….49
16. Window to ViewPort Transformation……………………………………………………………55
17. Translation in 3 dimensional…………………………………………………………………………57
18. 3D scaling……………………………………………………………………………………………………..61
19. 3D Rotation…………………………………………………………………………………………………..67
20. Boundary Fill Algorithm………………………………………………………………………………..74
21. Flood fill algorithm……………………………………………………………………………………….76
22. Demonstrate 2D lines……………………………………………………………………………………78
23. Demonstrate text in java……………………………………………………………………………….80
1.DDA Algorithm
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

class D1 extends JFrame implements ActionListener { JLabel l1, l2, l3, l4;

JTextField t1, t2, t3, t4;

Graphics g;

D1() {

this.setTitle("11211/20- DDA Line Algorithm");

this.setSize(800, 700);

this.setDefaultCloseOperation(JFrame.EXITONCLOSE);

this.setLayout(null);

l1 = new JLabel("X1:");

t1 = new JTextField();

l2 = new JLabel("Y1:");

t2 = new JTextField();

l3 = new JLabel("X2:");

t3 = new JTextField();

l4 = new JLabel("Y2:");

t4 = new JTextField();

JButton btn = new JButton("Draw");

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

l2.setBounds(10, 50, 80, 25);

t2.setBounds(100, 50, 100, 25);

l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

1
t4.setBounds(100, 130, 100, 25);

btn.setBounds(10, 170, 100, 25);

btn.addActionListener(this);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(l3);

this.add(t3);

this.add(l4);

this.add(t4);

this.add(btn);

this.setVisible(true);

g = this.getGraphics();

@Override

public void actionPerformed(ActionEvent e) {

int x0 = Integer.parseInt(t1.getText());

int y0 = Integer.parseInt(t2.getText());

int x1 = Integer.parseInt(t3.getText());

int y1 = Integer.parseInt(t4.getText());

int dx = x1 - x0;

int dy = y1 - y0;

int steps = Math.max(Math.abs(dx), Math.abs(dy));

float xincrement = (float) dx / steps;

float yincrement = (float) dy / steps;

float x = x0;

float y = y0;

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

g.fillRect(Math.round(x), Math.round(y), 2, 2);


2
x += xincrement;

y += yincrement;

public static void main(String[] args) {

new D1();

Output:

3
2. BLA Algorithm
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

class BLALineAlgorithm extends JFrame implements ActionListener { JLabel l1, l2, l3, l4;

JTextField t1, t2, t3, t4;

Graphics g;

BLALineAlgorithm() {

this.setTitle("11211/20 - BLA Line Algorithm");

this.setSize(800, 700);

this.setLayout(null);

this.setDefaultCloseOperation(JFrame.EXITONCLOSE);

l1 = new JLabel("X1:");

t1 = new JTextField();

l2 = new JLabel("Y1:");

t2 = new JTextField();

l3 = new JLabel("X2:");

t3 = new JTextField();

l4 = new JLabel("Y2:");

t4 = new JTextField();

JButton btn = new JButton("Draw");

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

l2.setBounds(10, 50, 80, 25);

t2.setBounds(100, 50, 100, 25);

l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

4
t4.setBounds(100, 130, 100, 25);

btn.setBounds(10, 170, 100, 25);

btn.addActionListener(this);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(l3);

this.add(t3);

this.add(l4);

this.add(t4);

his.add(btn);

this.setVisible(true);

g = this.getGraphics();

@Override

public void actionPerformed(ActionEvent e) {

int x0 = Integer.parseInt(t1.getText());

int y0 = Integer.parseInt(t2.getText());

int x1 = Integer.parseInt(t3.getText());

int y1 = Integer.parseInt(t4.getText());

int dx = Math.abs(x1 - x0);

int dy = Math.abs(y1 - y0);

int sx = (x1 - x0) > 0 ? 1 : -1;

int sy = (y1 - y0) > 0 ? 1 : -1;

int xn = x0, yn = y0, p0;

if (dx > dy) {

p0 = 2 * dy - dx;

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

g.fillRect(xn, yn, 2, 2);


5
xn = xn + sx;

if (p0 >= 0) {

yn = yn + sy;

p0 = p0 + 2 * (dy - dx);

} else {

p0 = p0 + 2 * dy;

}}}

else {

p0 = 2 * dx - dy;

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

g.fillRect(xn, yn, 2, 2);

if (p0 >=0) {

xn = xn + sx;

yn = yn + sy;

p0 = p0 + 2 * (dx - dy); } else {

yn = yn + sy;

p0 = p0 + 2 * dx;

}}}}

public static void main(String[] args) {

new BLALineAlgorithm();

Output:

6
3. General Circle Drawing Algorithm

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class GCD extends JFrame implements ActionListener {

JLabel l1, l2, l3;

JTextField t1, t2, t3;

Graphics g;

GCD() {

l1 = new JLabel("h:");

l1.setBounds(10, 10, 40, 25);

this.add(l1);

t1 = new JTextField();

t1.setBounds(20, 10, 40, 25);

this.add(t1);

l2 = new JLabel("k:");

l2.setBounds(100, 10, 40, 25);

this.add(l2);

t2 = new JTextField();

t2.setBounds(120, 10, 40, 25);

this.add(t2);

l3 = new JLabel("r:");

l3.setBounds(200, 10, 40, 25);

this.add(l3);

t3 = new JTextField();

t3.setBounds(220, 10, 40, 25);

this.add(t3);

JButton btn = new JButton("Draw");

btn.setBounds(300, 10, 100, 25);

7
btn.addActionListener(this);

this.add(btn);

this.setTitle(" General Circle Algorithm ");

this.setSize(800, 700);

this.setLayout(null);

this.setVisible(true);

g = this.getGraphics();

@Override

public void actionPerformed(ActionEvent e) {

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

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

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

int xk = r;

int yk = 0;

while (xk > yk) {

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

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

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

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

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

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

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

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

xk = xk - 1;

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

public static void main(String[] args) {

new GCD();
8
}

Output:

9
4.Midpoint Circle Algorithm Program
import javax.swing.*;

import java.awt.Graphics;

import java.awt.event.*;

class MidpointCircle extends JFrame implements ActionListener {

JLabel l1, l2, l3;

JTextField t1, t2, t3;

Graphics g;

MidpointCircle() {

l1 = new JLabel("h:");

l1.setBounds(10, 10, 40, 25);

add(l1);

t1 = new JTextField();

t1.setBounds(20, 10, 40, 25);

add(t1);

l2 = new JLabel("k:");

l2.setBounds(70, 10, 40, 25);

add(l2);

t2 = new JTextField();

t2.setBounds(90, 10, 40, 25);

add(t2);

l3 = new JLabel("r:");

l3.setBounds(130, 10, 40, 25);

this.add(l3);

t3 = new JTextField();

t3.setBounds(150, 10, 40, 25);

this.add(t3);

JButton btn = new JButton("Draw");

btn.setBounds(200, 10, 100, 25);

10
btn.addActionListener(this);

this.add(btn);

this.setTitle(" Mid-Point Circle Algorithm ");

this.setSize(600, 700);

this.setLayout(null);

this.setVisible(true);

g = this.getGraphics();

@Override

public void actionPerformed(ActionEvent e) {

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

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

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

int xk = 0;

int yk = r;

int pk = 1 - r;

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

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

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

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

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

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

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

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

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

if (Math.round(xk) >= Math.round(yk)) {

break;

xk = xk + 1;

if (pk >= 0) {
11
yk = yk - 1;

pk = pk + 2 * (xk - yk) + 1;

else {

pk = pk + 2 * xk + 1;

public static void main(String[] args) {

new MidpointCircle();

Output:

12
5.Translation
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

class Translation extends JFrame implements ActionListener {

JLabel l1, l2, l3, l4, l5, l6;

JTextField t1, t2, t3, t4, t5, t6;

Graphics g;

Translation() {

this.setTitle("11211/20 - 2D Translation");

this.setSize(800, 700);

this.setLayout(null);

this.setDefaultCloseOperation(JFrame.EXITONCLOSE);

l1= new JLabel("X1:");

t1 = new JTextField();

l2 = new JLabel("Y1:");

t2 = new JTextField();

l3 = new JLabel("X2:");

t3 = new JTextField();

l4 = new JLabel("Y2:");

t4 = new JTextField();

l5 = new JLabel("tx:");

t5 = new JTextField();

l6 = new JLabel("ty:");

t6 = new JTextField();

JButton btn = new JButton("Translate");

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

13
l2.setBounds(10, 50, 80, 25);

t2.setBounds(100, 50, 100, 25);

l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

t4.setBounds(100, 130, 100, 25);

l5.setBounds(250, 10, 50, 25);

t5.setBounds(320, 10, 100, 25);

l6.setBounds(250, 50, 50, 25);

t6.setBounds(320, 50, 100, 25);

btn.setBounds(10, 170, 100, 25);

btn.addActionListener(this);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(l3);

this.add(t3);

this.add(l4);

this.add(t4);

this.add(l5);

this.add(t5);

this.add(l6);

this.add(t6);

this.add(btn);

this.setVisible(true);

g = this.getGraphics();

@Override

public void actionPerformed(ActionEvent e) {


14
int x1 = Integer.parseInt(t1.getText());

int y1 = Integer.parseInt(t2.getText());

int x2 = Integer.parseInt(t3.getText());

int y2 = Integer.parseInt(t4.getText());

int tx = Integer.parseInt(t5.getText());

int ty = Integer.parseInt(t6.getText());

int[][] orgMatrix = {{x1, x2}, {y1, y2}, {1, 1}};

int[][] translationVector = {{1, 0, tx}, {0, 1, ty}, {0, 0, 1}};

int[][] finalMatrix = new int[3][2];

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

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

finalMatrix[i][j] = 0;

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

finalMatrix[i][j] += translationVector[i][k] * orgMatrix[k][j]; }

public static void main(String[] args) {

new Translation();

Output:

15
6.Write a Java Program to demonstrate 2D scaling
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Scaling extends JFrame implements ActionListener {

JLabel l1, l2, l3, l4, l5, l6;

JTextField t1, t2, t3, t4, t5, t6;

Graphics g;

Scaling() {

this.setTitle("11211/20 -- Scaling");

this.setSize(800, 700);

this.setLayout(null);

this.setDefaultCloseOperation(JFrame.EXITONCLOSE);

l1 = new JLabel("X1:");

t1 = new JTextField();

l2 = new JLabel("Y1:");

t2 = new JTextField();

l3 = new JLabel("X2:");

t3 = new JTextField();

l4 = new JLabel("Y2:");

t4 = new JTextField();

l5 = new JLabel("sx:");

t5 = new JTextField();

l6 = new JLabel("sy:");

t6 = new JTextField();

JButton btn = new JButton("Scale");

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

16
l2.setBounds(10, 50, 80, 25);

t2.setBounds(100, 50, 100, 25);

l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

t4.setBounds(100, 130, 100, 25);

l5.setBounds(250, 10, 50, 25);

t5.setBounds(320, 10, 100, 25);

l6.setBounds(250, 50, 50, 25);

t6.setBounds(320, 50, 100, 25);

btn.setBounds(10, 170, 100, 25);

btn.addActionListener(this);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(l3);

this.add(t3);

this.add(l4);

this.add(t4);

this.add(l5);

this.add(t5);

this.add(l6);

this.add(t6);

this.add(btn);

this.setVisible(true);

@Override

public void actionPerformed(ActionEvent e) {

int x1 = Integer.parseInt(t1.getText());
17
int y1 = Integer.parseInt(t2.getText());

int x2 = Integer.parseInt(t3.getText());

int y2 = Integer.parseInt(t4.getText());

int sx = Integer.parseInt(t5.getText());

int sy = Integer.parseInt(t6.getText());

int[][] orgMatrix = {{x1, x2}, {y1, y2}, {1, 1}};

int[][] scalingVector = {{sx, 0, 0}, {0, sy, 0}, {0, 0, 1}};

int[][] finalMatrix = new int[3][2];

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

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

finalMatrix[i][j] = 0;

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

finalMatrix[i][j] += scalingVector[i][k] * orgMatrix[k][j];

}}}

g = this.getGraphics();

g.drawLine(orgMatrix[0][0], orgMatrix[1][0], orgMatrix[0][1], orgMatrix[1][1]);

g.drawString("Before Scaling", 100, 350);

g.drawLine(finalMatrix[0][0], finalMatrix[1][0], finalMatrix[0][1], finalMatrix[1][1]);

g.drawString("After Scaling", 380, 660);}

public static void main(String[] args) {

new Scaling();

}}

Output:

18
7.WAP to demonstrate 2D Rotation
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Rotation extends JFrame implements ActionListener {

JLabel l1, l2, l3, l4, l5;

JTextField t1, t2, t3, t4, t5;

Graphics g;

Rotation() {

this.setTitle("2D Rotation");

this.setSize(800, 700);

this.setLayout(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

l1 = new JLabel("X1:");

t1 = new JTextField();

l2 = new JLabel("Y1:");

t2 = new JTextField();

l3 = new JLabel("X2:");

t3 = new JTextField();

l4 = new JLabel("Y2:");

t4 = new JTextField();

l5 = new JLabel("Angle:");

t5 = new JTextField();

JButton btn = new JButton("Rotate");

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

l2.setBounds(10, 50, 80, 25);

t2.setBounds(100, 50, 100, 25);

19
l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

t4.setBounds(100, 130, 100, 25);

l5.setBounds(250, 10, 50, 25);

t5.setBounds(320, 10, 100, 25);

btn.setBounds(10, 170, 100, 25);

btn.addActionListener(this);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(l3);

this.add(t3);

this.add(l4);

this.add(t4);

this.add(l5);

this.add(t5);

this.add(btn);

this.setVisible(true);

g = this.getGraphics();

void counterClockWise(int[][] orgMatrix, double rad) {

double[][] rotationVector = {{Math.cos(rad), -Math.sin(rad), 0}, {Math.sin(rad), Math.cos(rad), 0}, {0, 0, 1}};

int[][] finalMatrix = new int[3][2];

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

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

finalMatrix[i][j] = 0;

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

finalMatrix[i][j] += rotationVector[i][k] * orgMatrix[k][j];


20
}

g.drawLine(finalMatrix[0][0], finalMatrix[1][0], finalMatrix[0][1], finalMatrix[1][1]);

g.drawString("Rotation in Counter Clockwise", 80, 560);

void clockWise(int[][] orgMatrix, double rad) {

double[][] rotationVector = {{Math.cos(rad), -Math.sin(rad), 0}, {Math.sin(rad), Math.cos(rad), 0}, {0, 0, 1}};

int[][] finalMatrix = new int[3][2];

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

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

finalMatrix[i][j] = 0;

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

finalMatrix[i][j] += rotationVector[i][k] * orgMatrix[k][j];

g.drawLine(finalMatrix[0][0], finalMatrix[1][0], finalMatrix[0][1], finalMatrix[1][1]);

g.drawString("Rotation in Clockwise", 320, 410);

@Override

public void actionPerformed(ActionEvent e) {

int x1 = Integer.parseInt(t1.getText());

int y1 = Integer.parseInt(t2.getText());

int x2 = Integer.parseInt(t3.getText());

int y2 = Integer.parseInt(t4.getText());

int angle = Integer.parseInt(t5.getText());

double rad = Math.toRadians(angle);

int[][] orgMatrix = {{x1, x2}, {y1, y2}, {1, 1}};

g.drawLine(orgMatrix[0][0], orgMatrix[1][0], orgMatrix[0][1], orgMatrix[1][1]);


21
g.drawString("Before Rotation", 200, 480);

counterClockWise(orgMatrix, rad);

clockWise(orgMatrix, -rad);

public static void main(String[] args) {

new Rotation();

Output:

22
8.WAP to demonstrate 2D Reflection in X axis
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Refx extends JFrame implements ActionListener { JLabel l1, l2, l3, l4;

JTextField t1, t2, t3, t4;

Graphics g;

Refx() {

this.setTitle("11211/20 - Reflection Through X-Axis");

this.setSize(800, 700);

this.setLayout(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

l1 = new JLabel("X1:");

t1 = new JTextField();

l2 = new JLabel("Y1:");

t2 = new JTextField();

l3 = new JLabel("X2:");

t3 = new JTextField();

l4 = new JLabel("Y2:");

t4 = new JTextField();

JButton btn = new JButton("Reflect");

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

l2.setBounds(10, 50, 80, 25);

t2.setBounds(100, 50, 100, 25);

l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

23
t4.setBounds(100, 130, 100, 25);

btn.setBounds(10, 170, 100, 25);

btn.addActionListener(this);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(l3);

this.add(t3);

this.add(l4);

this.add(t4);

this.add(btn);

this.setVisible(true); // Added this line to make the JFrame visible g = this.getGraphics();

int[][] translate(int[][] orgMatrix, int tx, int ty) {

int[][] translationVector = {{1, 0, tx}, {0, 1, ty}, {0, 0, 1}};

int[][] tempMatrix = new int[3][2];

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

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

tempMatrix[i][j] = 0;

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

tempMatrix[i][j] += translationVector[i][k] * orgMatrix[k][j];

}}}

return tempMatrix;

@Override

public void actionPerformed(ActionEvent e) {

int x1 = Integer.parseInt(t1.getText());

int y1 = Integer.parseInt(t2.getText());

int x2 = Integer.parseInt(t3.getText());
24
int y2 = Integer.parseInt(t4.getText());

g.drawLine(400, 0, 400, 700);

g.drawLine(0, 350, 800, 350);

g.drawString("(400,350)", 340, 370);

int[][] orgMatrix = {{x1, x2}, {y1, y2}, {1, 1}};

int[][] translatedMatrix = translate(orgMatrix, 400, 350);

int[][] reflectionVector = {{1, 0, 0}, {0, -1, 0}, {0, 0, 1}};

int[][] reflectedMatrix = new int[3][2];

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

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

reflectedMatrix[i][j] = 0;

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

reflectedMatrix[i][j] += reflectionVector[i][k] * translatedMatrix[k][j];

}}}

int[][] finalMatrix = translate(reflectedMatrix, 0, 750);

g.drawLine(translatedMatrix[0][0], translatedMatrix[1][0], translatedMatrix[0][1], translatedMatrix[1][1]);

g.drawString("Before Reflection", 580, 650);

g.drawLine(finalMatrix[0][0], finalMatrix[1][0], finalMatrix[0][1], finalMatrix[1][1]);

g.drawString("After Reflection", 550, 250);

public static void main(String[] args) {

new Refx();

}}

Output:

25
9.WAP to demonstrate 2D Reflection in Y axis.
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Refy extends JFrame implements ActionListener { JLabel l1, l2, l3, l4;

JTextField t1, t2, t3, t4;

Graphics g;

public Refy() {

this.setTitle("11211/20 - Reflection Through Y-Axis");

this.setSize(800, 700);

this.setLayout(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

l1 = new JLabel("X1:");

t1 = new JTextField();

l2 = new JLabel("Y1:");

t2 = new JTextField();

l3 = new JLabel("X2:");

t3 = new JTextField();

l4 = new JLabel("Y2:");

t4 = new JTextField();

JButton btn = new JButton("Reflect");

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

l2.setBounds(10, 50, 80, 25);

t2.setBounds(100, 50, 100, 25);

l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

26
t4.setBounds(100, 130, 100, 25);

btn.setBounds(10, 170, 100, 25);

btn.addActionListener(this);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(l3);

this.add(t3);

this.add(l4);

this.add(t4);

this.add(btn);

this.setVisible(true);

g = this.getGraphics();

int[][] translate(int[][] orgMatrix, int tx, int ty) {

int[][] translationVector = {{1, 0, tx}, {0, 1, ty}, {0, 0, 1}};

int[][] tempMatrix = new int[3][2];

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

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

tempMatrix[i][j] = 0;

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

tempMatrix[i][j] += translationVector[i][k] * orgMatrix[k][j]; }

}}

return tempMatrix;}

@Override

public void actionPerformed(ActionEvent e) {

int x1 = Integer.parseInt(t1.getText());

int y1 = Integer.parseInt(t2.getText());

int x2 = Integer.parseInt(t3.getText());
27
int y2 = Integer.parseInt(t4.getText());

g.drawLine(400, 0, 400, 700);

g.drawLine(0, 350, 800, 350);

g.drawString("(400,350)", 340, 370);

int[][] orgMatrix = {{x1, x2}, {y1, y2}, {1, 1}};

int[][] translatedMatrix = translate(orgMatrix, 400, 350);

int[][] reflectionVector = {{-1, 0, 0}, {0, 1, 0}, {0, 0, 1}};

int[][] reflectedMatrix = new int[3][2];

for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) {

reflectedMatrix[i][j] = 0; for (int k = 0; k < 3; k++) {

reflectedMatrix[i][j] += reflectionVector[i][k] * translatedMatrix[k][j];

}}}

int[][] finalMatrix = translate(reflectedMatrix, 750, 0);

g.drawLine(translatedMatrix[0][0], translatedMatrix[1][0], translatedMatrix[0][1], translatedMatrix[1][1]);

g.drawString("Before Reflection", 580, 650);

g.drawLine(finalMatrix[0][0], finalMatrix[1][0], finalMatrix[0][1], finalMatrix[1][1]);

g.drawString("After Reflection", 80, 650);

public static void main(String[] args) {

new Refy();

}}

Output:

28
10. WAP to demonstrate 2D Reflection in Reflection in origin.
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class RefO extends JFrame implements ActionListener { JLabel l1, l2, l3, l4, l5, l6;

JTextField t1, t2, t3, t4, t5, t6;

Graphics g;

RefO() {

this.setTitle("11211/20 - Reflection Through Origin");

this.setSize(800, 700);

this.setLayout(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

l1 = new JLabel("X1:");

t1 = new JTextField();

l2 = new JLabel("Y1:");

t2 = new JTextField();

l3 = new JLabel("X2:");

t3 = new JTextField();

l4 = new JLabel("Y2:");

t4 = new JTextField();

l5 = new JLabel("X3:");

t5 = new JTextField();

l6 = new JLabel("Y3:");

t6 = new JTextField();

JButton btn = new JButton("Reflect");

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

l2.setBounds(10, 50, 80, 25);

29
t2.setBounds(100, 50, 100, 25);

l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

t4.setBounds(100, 130, 100, 25);

l5.setBounds(10, 170, 50, 25);

t5.setBounds(100, 170, 100, 25);

l6.setBounds(10, 210, 50, 25);

t6.setBounds(100, 210, 100, 25);

btn.setBounds(10, 250, 100, 25);

btn.addActionListener(this);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(l3);

this.add(t3);

this.add(l4);

this.add(t4);

this.add(l5);

this.add(t5);

this.add(l6);

this.add(t6);

this.add(btn);

this.setVisible(true);

g = this.getGraphics();}

int[][] translate(int[][] orgMatrix, int tx, int ty) {

int[][] translationVector = {{1, 0, tx}, {0, 1, ty}, {0, 0, 1}};

int[][] tempMatrix = new int[3][3];

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


30
for (int j = 0; j < 3; j++) {

tempMatrix[i][j] = 0;

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

tempMatrix[i][j] += translationVector[i][k] * orgMatrix[k][j]; }

}}

return tempMatrix;}

@Override

public void actionPerformed(ActionEvent e) {

int x1 = Integer.parseInt(t1.getText());

int y1 = Integer.parseInt(t2.getText());

int x2 = Integer.parseInt(t3.getText());

int y2 = Integer.parseInt(t4.getText());

int x3 = Integer.parseInt(t5.getText());

int y3 = Integer.parseInt(t6.getText());

g.drawLine(400, 0, 400, 700);

g.drawLine(0, 350, 800, 350);

g.drawString("(400,350)", 340, 370);

int[][] orgMatrix = {{x1, x2, x3}, {y1, y2, y3}, {1, 1, 1}};

int[][] translatedMatrix = translate(orgMatrix, 400, 350);

int[][] reflectionVector = {{-1, 0, 0}, {0, -1, 0}, {0, 0, 1}};

int[][] reflectedMatrix = new int[3][3];

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

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

reflectedMatrix[i][j] = 0;

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

reflectedMatrix[i][j] += reflectionVector[i][k] * translatedMatrix[k][j]; }

}}

int[][] finalMatrix = translate(reflectedMatrix, 800, 750);

int x1Translated = translatedMatrix[0][0];

int y1Translated = translatedMatrix[1][0];


31
int x2Translated = translatedMatrix[0][1];

int y2Translated = translatedMatrix[1][1];

int x3Translated = translatedMatrix[0][2];

int y3Translated = translatedMatrix[1][2];

g.drawLine(x1Translated, y1Translated, x2Translated, y2Translated);

g.drawLine(x2Translated, y2Translated, x3Translated, y3Translated);

g.drawLine(x3Translated, y3Translated, x1Translated, y1Translated);

int x1Final = finalMatrix[0][0];

int y1Final = finalMatrix[1][0];

int x2Final = finalMatrix[0][1];

int y2Final = finalMatrix[1][1];

int x3Final = finalMatrix[0][2];

int y3Final = finalMatrix[1][2];

g.drawLine(x1Final, y1Final, x2Final, y2Final);

g.drawLine(x2Final, y2Final, x3Final, y3Final);

g.drawLine(x3Final, y3Final, x1Final, y1Final);

g.drawString("Before Reflection", 580, 550);

g.drawString("After Reflection", 280, 310);

public static void main(String[] args) {

new RefO();

Output:

32
11. WAP to demonstrate 2D Reflection in Reflection in Y=X
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Refyequalsx extends JFrame implements ActionListener { JLabel l1, l2, l3, l4, l5, l6;

JTextField t1, t2, t3, t4, t5, t6;

Graphics g;

Refyequalsx() {

this.setTitle("11211/20- Reflection Through Line Y=X");

this.setSize(700, 700);

this.setLayout(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

l1 = new JLabel("X1:");

t1 = new JTextField();

l2 = new JLabel("Y1:");

t2 = new JTextField();

l3 = new JLabel("X2:");

t3 = new JTextField();

l4 = new JLabel("Y2:");

t4 = new JTextField();

l5 = new JLabel("X3:");

t5 = new JTextField();

l6 = new JLabel("Y3:");

t6 = new JTextField();

JButton btn = new JButton("Reflect");

btn.addActionListener(this);

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

33
l2.setBounds(10, 50, 80, 25);

t2.setBounds(100, 50, 100, 25);

l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

t4.setBounds(100, 130, 100, 25);

l5.setBounds(10, 170, 50, 25);

t5.setBounds(100, 170, 100, 25);

l6.setBounds(10, 210, 50, 25);

t6.setBounds(100, 210, 100, 25);

btn.setBounds(10, 250, 100, 25);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(l3);

this.add(t3);

this.add(l4);

this.add(t4);

this.add(l5);

this.add(t5);

this.add(l6);

this.add(t6);

this.add(btn);

this.setVisible(true);

g = this.getGraphics();

int[][] translate(int[][] orgMatrix) {

int[][] translationVector = {{1, 0, 350}, {0, 1, 350}, {0, 0, 1}};

int[][] temp = new int[3][3];


34
for (int i = 0; i < 3; i++) {

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

temp[i][j] = 0;

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

temp[i][j] += translationVector[i][k] * orgMatrix[k][j];

}}}

return temp;

@Override

public void actionPerformed(ActionEvent e) {

int x1 = Integer.parseInt(t1.getText());

int y1 = Integer.parseInt(t2.getText());

int x2 = Integer.parseInt(t3.getText());

int y2 = Integer.parseInt(t4.getText());

int x3 = Integer.parseInt(t5.getText());

int y3 = Integer.parseInt(t6.getText());

g.drawLine(350, 0, 350, 700);

g.drawLine(0, 350, 700, 350);

g.drawLine(350, 350, 700, 700);

g.drawString("(350,350)", 290, 370);

int[][] orgMatrix = {{x1, x2, x3}, {y1, y2, y3}, {1, 1, 1}};

int[][] translatedMatrix = translate(orgMatrix);

int[][] reflectionVector = {{0, 1, 0}, {1, 0, 0}, {0, 0, 1}};

int[][] finalMatrix = new int[3][3];

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

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

finalMatrix[i][j] = 0;

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

finalMatrix[i][j] += reflectionVector[i][k] * translatedMatrix[k][j]; }

}}
35
x1 = finalMatrix[0][0];

y1 = finalMatrix[1][0];

x2 = finalMatrix[0][1];

y2 = finalMatrix[1][1];

x3 = finalMatrix[0][2];

y3 = finalMatrix[1][2];

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

g.drawLine(x2, y2, x3, y3);

g.drawLine(x3, y3, x1, y1);

g.drawString("Before Reflection", 550, 530);

x1 = translatedMatrix[0][0];

y1 = translatedMatrix[1][0];

x2 = translatedMatrix[0][1];

y2 = translatedMatrix[1][1];

x3 = translatedMatrix[0][2];

y3 = translatedMatrix[1][2];

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

g.drawLine(x2, y2, x3, y3);

g.drawLine(x3, y3, x1, y1);

g.drawString("After Reflection", 400, 620); }

public static void main(String[] args) {

new Refyequalsx();

}}

Output:

36
12.WAP to demonstrate 2D reflection in y=mx+c
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class reflection_Y_mx extends JFrame implements ActionListener {

JLabel l1_, l2_, l3_, l4_, l5_, l6_, l7, l8_;

JTextField t1, t2_, t3_, t4_, t5, t6_, t7_, t8_;

Graphics g;

reflection_Y_mx() {

this.setTitle(" Reflection Through Line Y = mx + c");

this.setSize(800, 700);

this.setLayout(null);

l1_ = new JLabel("X1:");

t1 = new JTextField();

l2_ = new JLabel("Y1:");

t2_ = new JTextField();

l3_ = new JLabel("X2:");

t3_ = new JTextField();

l4_ = new JLabel("Y2:");

t4_ = new JTextField();

l5_ = new JLabel("X3:");

t5 = new JTextField();

l6_ = new JLabel("Y3:");

t6_ = new JTextField();

l7 = new JLabel("m:");

t7_ = new JTextField();

37
l8_ = new JLabel("c:");

t8_ = new JTextField();

JButton btn = new JButton("Reflect");

l1_.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

l2_.setBounds(10, 50, 80, 25);

t2_.setBounds(100, 50, 100, 25);

l3_.setBounds(10, 90, 80, 25);

t3_.setBounds(100, 90, 100, 25);

l4_.setBounds(10, 130, 80, 25);

t4_.setBounds(100, 130, 100, 25);

l5_.setBounds(10, 170, 50, 25);

t5.setBounds(100, 170, 100, 25);

l6_.setBounds(10, 210, 50, 25);

t6_.setBounds(100, 210, 100, 25);

l7.setBounds(250, 10, 50, 25);

t7_.setBounds(320, 10, 100, 25);

l8_.setBounds(250, 50, 50, 25);

t8_.setBounds(320, 50, 100, 25);

btn.setBounds(10, 250, 100, 25);

btn.addActionListener(this);

this.add(l1_);

this.add(t1);

this.add(l2_);

this.add(t2_);

this.add(l3_);

38
this.add(t3_);

this.add(l4_);

this.add(t4_);

this.add(l5_);

this.add(t5);

this.add(l6_);

this.add(t6_);

this.add(l7);

this.add(t7_);

this.add(l8_);

this.add(t8_);

this.add(btn);

this.setVisible(true);

g = this.getGraphics();

double[][] translate(double[][] orgMatrix, int tx, int ty) {

double[][] translationVector = {{1, 0, tx}, {0, 1, ty}, {0, 0, 1}};

double[][] tempMatrix = new double[3][3];

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

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

tempMatrix[i][j] = 0;

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

tempMatrix[i][j] += translationVector[i][k] * orgMatrix[k][j];

} }}

return tempMatrix;

39
@Override

public void actionPerformed(ActionEvent e) {

int x1 = Integer.parseInt(t1.getText());

int y1 = Integer.parseInt(t2_.getText());

int x2 = Integer.parseInt(t3_.getText());

int y2 = Integer.parseInt(t4_.getText());

int x3 = Integer.parseInt(t5.getText());

int y3 = Integer.parseInt(t6_.getText());

int m = Integer.parseInt(t7_.getText());

int c = Integer.parseInt(t8_.getText());

g.drawLine(400, 0, 400, 700);

g.drawLine(0, 350, 800, 350);

g.drawString("(400,350)", 340, 370);

double[][] orgMatrix = {{x1, x2, x3}, {y1, y2, y3}, {1, 1, 1}};

double[][] translatedMatrix = translate(orgMatrix, 400, 350);

x1 = (int) translatedMatrix[0][0];

y1 = (int) translatedMatrix[1][0];

x2 = (int) translatedMatrix[0][1];

y2 = (int) translatedMatrix[1][1];

x3 = (int) translatedMatrix[0][2];

y3 = (int) translatedMatrix[1][2];

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

g.drawLine(x2, y2, x3, y3);

g.drawLine(x3, y3, x1, y1);

g.drawString("Before Reflection", 550, 570);

x1 = -(c / m);

40
y2 = c;

g.drawLine(x1 + 400, 350, 400, y2 + 350);

g.drawString("y = " + m + "x" + " + " + c, 420, 580);

double a = m * m - 1;

double b = m * m + 1;

double[][] reflectionVector = {

{-a / b, (2 * m) / b, (-2 * m * c) / b},{(2 * m) / b, a / b, (2 * c) / b},{0, 0, 1}};

double[][] reflectedMatrix = new double[3][3];

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

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

reflectedMatrix[i][j] = 0;

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

reflectedMatrix[i][j] += reflectionVector[i][k] * translatedMatrix[k][j];

double[][] finalMatrix = translate(reflectedMatrix, 800, 500);

x1 = (int) finalMatrix[0][0];

y1 = (int) finalMatrix[1][0];

x2 = (int) finalMatrix[0][1];

y2 = (int) finalMatrix[1][1];

x3 = (int) finalMatrix[0][2];

y3 = (int) finalMatrix[1][2];

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

g.drawLine(x2, y2, x3, y3);

g.drawLine(x3, y3, x1, y1);

41
g.drawString("After Reflection", 160, 650);

public static void main(String[] args) {

new reflection_Y_mx();

Output:

42
13. WAP to demonstrate Shearing in X.
import javax.swing.*;

import java.awt.*;

import java.util.Scanner;

public class Shearing extends JPanel {

static int x1, y1, x2, y2, x3, y3, x4, y4;

static int px1, px2, px3, px4;

int shx = 2;

public Shearing(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { this.x1 = x1;

this.y1 = y1;

this.x2 = x2;

this.y2 = y2;

this.x3 = x3;

this.y3 = y3;

this.x4 = x4;

this.y4 = y4;

px1 = y1 * shx + x1;

px2 = y2 * shx + x2;

px3 = y3 * shx + x3;

px4 = y4 * shx + x4;

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawString("Before Shearing", x1 - 20, y1 - 20);

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

g.drawLine(x2, y2, x3, y3);

g.drawLine(x3, y3, x4, y4);

g.drawLine(x4, y4, x1, y1);

43
g.drawString("After Shearing", px1 - 20, y1 - 20);

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

JFrame f = new JFrame();

Scanner sc = new Scanner(System.in);

System.out.println("Enter x1:");

int x1 = sc.nextInt();

System.out.println("Enter y1:");

int y1 = sc.nextInt();

System.out.println("Enter x2:");

int x2 = sc.nextInt();

System.out.println("Enter y2:");

int y2 = sc.nextInt();

System.out.println("Enter x3:");

int x3 = sc.nextInt();

System.out.println("Enter y3:");

int y3 = sc.nextInt();

System.out.println("Enter x4:");

int x4 = sc.nextInt();

System.out.println("Enter y4:");

int y4 = sc.nextInt();

Shearing shearing = new Shearing(x1, y1, x2, y2, x3, y3, x4, y4);

f.add(shearing);

f.setTitle("Shearing in X-axis");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(500, 500);
44
f.setVisible(true);

Input:

Output:

45
14. WAP to demonstrate shearing in xy
import javax.swing.*;

import java.awt.*;

import java.util.Scanner;

public class Shearingxy extends JPanel {

static int x1, y1, x2, y2, x3, y3, x4, y4;

static int px1, px2, px3, px4;

int shx = 2;

public Shearingxy(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)

{ this.x1 = x1;

this.y1 = y1;

this.x2 = x2;

this.y2 = y2;

this.x3 = x3;

this.y3 = y3;

this.x4 = x4;

this.y4 = y4;

px1 = x1 + y1 * shx;

px2 = x2 + y2 * shx;

px3 = x3 + y3 * shx;

px4 = x4 + y4 * shx;

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawString("Before Shearing", x1 - 20, y1 - 20);

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

g.drawLine(x2, y2, x3, y3);

g.drawLine(x3, y3, x4, y4);

46
g.drawLine(x4, y4, x1, y1);

g.drawString("After Shearing (XY-axis)", px1 - 20, y1 - 20);

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

JFrame f = new JFrame();

Scanner sc = new Scanner(System.in);

System.out.println("Enter x1:");

int x1 = sc.nextInt();

System.out.println("Enter y1:");

int y1 = sc.nextInt();

System.out.println("Enter x2:");

int x2 = sc.nextInt();

System.out.println("Enter y2:");

int y2 = sc.nextInt();

System.out.println("Enter x3:");

int x3 = sc.nextInt();

System.out.println("Enter y3:");

int y3 = sc.nextInt();

System.out.println("Enter x4:");

int x4 = sc.nextInt();

System.out.println("Enter y4:");

int y4 = sc.nextInt();

Shearingxy = new Shearingxy(x1, y1, x2, y2, x3, y3, x4, y4);

f.add(Shearingxy);

f.setTitle("Shearing in X-axis");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
47
f.setSize(600, 500);

f.setVisible(true);

Input:

Output:

48
15. Line Clipping (Cohen–Sutherland Algorithm)
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Cohen_Sutherland extends JFrame implements ActionListener {

final int INSIDE = 0; // 0000

final int LEFT = 1; // 0001

final int RIGHT = 2; // 0010

final int BOTTOM = 4; // 0100

final int TOP = 8; // 1000

int x_min, y_min, x_max, y_max;

JLabel l1, l2, l3, l4, l5, l6, l7, l8;

JTextField t1, t2, t3, t4, t5, t6, t7, t8;

Graphics g;

Cohen_Sutherland() {

this.setTitle("Cohen-Sutherland Line Clipping Algorithm");

this.setSize(800, 700);

this.setLayout(null);

l1 = new JLabel("Xmin:");

t1 = new JTextField();

l2 = new JLabel("Ymin:");

t2 = new JTextField();

l3 = new JLabel("Xmax:");

t3 = new JTextField();

l4 = new JLabel("Ymax:");

t4 = new JTextField();

l5 = new JLabel("X1:");

t5 = new JTextField();

l6 = new JLabel("Y1:");

49
t6 = new JTextField();

l7 = new JLabel("X2:");

t7 = new JTextField();

l8 = new JLabel("Y2:");

t8 = new JTextField();

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

l2.setBounds(10, 50, 80, 25);

t2.setBounds(100, 50, 100, 25);

l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

t4.setBounds(100, 130, 100, 25);

l5.setBounds(250, 10, 50, 25);

t5.setBounds(320, 10, 100, 25);

l6.setBounds(250, 50, 50, 25);

t6.setBounds(320, 50, 100, 25);

l7.setBounds(250, 90, 50, 25);

t7.setBounds(320, 90, 100, 25);

l8.setBounds(250, 130, 50, 25);

t8.setBounds(320, 130, 100, 25);

JButton btn = new JButton("Clip Line");

btn.setBounds(10, 170, 100, 25);

btn.addActionListener(this);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(l3);

this.add(t3);
50
this.add(l4);

add(t4);

this.add(l5);

this.add(t5);

this.add(l6);

this.add(t6);

this.add(l7);

this.add(t7);

this.add(l8);

this.add(t8);

this.add(btn);

this.setVisible(true);

g = this.getGraphics();

int computeCode(double x, double y) {

int code = INSIDE;

if (x < x_min) // to the left of rectangle

code |= LEFT;

else if (x > x_max) // to the right of rectangle

code |= RIGHT;

if (y < y_min) // below the rectangle

code |= BOTTOM;

else if (y > y_max) // above the rectangle

code |= TOP;

return code;

@Override

public void actionPerformed(ActionEvent e) {

x_min = Integer.parseInt(t1.getText());

y_min = Integer.parseInt(t2.getText());
51
x_max = Integer.parseInt(t3.getText());

y_max = Integer.parseInt(t4.getText());

double x1 = Integer.parseInt(t5.getText());

double y1 = Integer.parseInt(t6.getText());

double x2 = Integer.parseInt(t7.getText());

double y2 = Integer.parseInt(t8.getText());

g.drawLine(x_min, y_min, x_min, y_max);

g.drawLine(x_min, y_max, x_max, y_max);

g.drawLine(x_max, y_max, x_max, y_min);

g.drawLine(x_max, y_min, x_min, y_min);

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

g.drawString("Before Clipping", 150, 600);

int code1 = computeCode(x1, y1);

int code2 = computeCode(x2, y2);

boolean accept = false;

while (true) {

if ((code1 == 0) && (code2 == 0)) {

accept = true;

break;

else if ((code1 & code2) != 0) {

break;

else {

int code_out;

double x = 0, y = 0;

if (code1 != 0) {

code_out = code1;

else {
52
code_out = code2;

if ((code_out & TOP) != 0) {

x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);

y = y_max;

else if ((code_out & BOTTOM) != 0) {

x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);

y = y_min;

else if ((code_out & RIGHT) != 0) {

y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);

x = x_max;

else if ((code_out & LEFT) != 0) {

y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);

x = x_min;

if (code_out == code1) {

x1 = x;

y1 = y;

code1 = computeCode(x1, y1);

else {

x2 = x;

y2 = y;

code2 = computeCode(x2, y2);

}
53
int tx = 300;

if (accept) {

g.drawLine(x_min + tx, y_min, x_min + tx, y_max);

g.drawLine(x_min + tx, y_max, x_max + tx, y_max);

g.drawLine(x_max + tx, y_max, x_max + tx, y_min);

g.drawLine(x_max + tx, y_min, x_min + tx, y_min);

g.drawLine((int) x1 + tx, (int) y1, (int) x2 + tx, (int) y2);

g.drawString("After Clipping", 460, 600);

else {

System.out.println("Line is rejected.");

public static void main(String[] args) {

new Cohen_Sutherland();

Output:

54
16. WAP in JAVA program to demonstrate Window to ViewPort Transformation.
import java.awt.*;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class WindowViewPort extends JPanel {

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

int windowWidth = 200;

int windowHeight = 200;

int viewportWidth = 400;

int viewportHeight = 400;

float sx = (float) viewportWidth / windowWidth;

float sy = (float) viewportHeight / windowHeight;

g.drawString("Window space", 100, 100);

g.drawRect(100, 100, windowWidth, windowHeight);

g.drawString("Viewport space", 400, 400);

g.drawRect(100, 100, viewportWidth, viewportHeight);

int x1 = 50;

int y1 = 50;

int xt1 = (int) (x1 * sx);

int yt1 = (int) (y1 * sy);

g.setColor(Color.RED); // Set color for the transformed point

g.fillOval(xt1 + 100, yt1 + 100, 5, 5); // Draw transformed point

public static void main(String[] args) {

JFrame mango = new JFrame("Window to Viewport Transformation Example");

mango.setSize(600, 600); // Increased size to show the transformations clearly

mango.add(new WindowViewPort());

55
mango.setVisible(true);

Output:

56
17. WAP to demonstrate Translation in 3 dimensional.
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Translation3D extends JFrame implements ActionListener {

JLabel l1, l2, l3, l4, l5, l6;

JTextField t1, t2, t3, t4, t5, t6;

Graphics g;

Translation3D() {

setTitle("3D Translation");

setSize(800, 700);

setLayout(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

l1 = new JLabel("X");

t1 = new JTextField();

l2 = new JLabel("Y");

t2 = new JTextField();

l3 = new JLabel("Length");

t3 = new JTextField();

l4 = new JLabel("Shift");

t4 = new JTextField();

l5 = new JLabel("tx");

t5 = new JTextField();

l6 = new JLabel("ty");

t6 = new JTextField();

JButton btn = new JButton("Draw");

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

57
l2.setBounds(10, 50, 80, 25);

t2.setBounds(100, 50, 100, 25);

l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

t4.setBounds(100, 130, 100, 25);

l5.setBounds(250, 10, 50, 25);

t5.setBounds(320, 10, 100, 25);

l6.setBounds(250, 50, 50, 25);

t6.setBounds(320, 50, 100, 25);

btn.setBounds(10, 170, 100, 25);

btn.addActionListener(this);

add(l1);

add(t1);

add(l2);

add(t2);

add(l3);

add(t3);

add(l4);

add(t4);

add(l5);

add(t5);

add(l6);

add(t6);

add(btn);

setVisible(true);

g = this.getGraphics();

Point[] getCubeOnePoints(int x, int y, int length) { Point[] points = new Point[4];

points[0] = new Point(x, y);


58
points[1] = new Point(x + length, y);

points[2] = new Point(x + length, y + length);

points[3] = new Point(x, y + length);

return points;

Point[] getCubeTwoPoints(int x, int y, int length, int shift) {

int newX = x + shift;

int newY = y + shift;

Point[] points = new Point[4];

points[0] = new Point(newX, newY);

points[1] = new Point(newX + length, newY);

points[2] = new Point(newX + length, newY + length);

points[3] = new Point(newX, newY + length);

return points;

@Override

public void actionPerformed(ActionEvent e) {

int x = Integer.parseInt(t1.getText());

int y = Integer.parseInt(t2.getText());

int length = Integer.parseInt(t3.getText());

int shift = Integer.parseInt(t4.getText());

int tx = Integer.parseInt(t5.getText());

int ty = Integer.parseInt(t6.getText());

Point[] cubeOnePoints = getCubeOnePoints(x, y, length);

Point[] cubeTwoPoints = getCubeTwoPoints(x, y, length, shift);

g.setColor(Color.BLACK);

g.drawRect(x, y, length, length);

g.drawRect(x + shift, y + shift, length, length);

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

g.drawLine(cubeOnePoints[i].x, cubeOnePoints[i].y, cubeTwoPoints[i].x, cubeTwoPoints[i].y);


59
}

g.drawString("Before Translation", x - 20, y - 20);

g.setColor(Color.RED);

g.drawRect(x + tx, y + ty, length, length);

g.drawRect(x + shift + tx, y + shift + ty, length, length);

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

g.drawLine(cubeOnePoints[i].x + tx, cubeOnePoints[i].y + ty, cubeTwoPoints[i].x + tx, cubeTwoPoints[i].y + ty);

g.drawString("After Translation", x + tx - 20, y + ty - 20);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> new Translation3D());

Output:

60
18. Write a Java program to demonstrate 3D scaling.
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Scaling3D extends JFrame implements ActionListener {

JLabel l1, l2, l3, l4, l5, l6;

JTextField t1, t2, t3, t4, t5, t6;

Graphics g;

Scaling3D() {

this.setTitle(" 3D Scaling");

this.setSize(800, 700);

this.setLayout(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

l1 = new JLabel("X:");

t1 = new JTextField();

l2 = new JLabel("Y:");

t2 = new JTextField();

l3 = new JLabel("Length:");

t3 = new JTextField();

l4 = new JLabel("Shift:");

t4 = new JTextField();

l5 = new JLabel("sx:");

t5 = new JTextField();

l6 = new JLabel("sy:");

t6 = new JTextField();

JButton btn = new JButton("Draw");

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);


61
l2.setBounds(10, 50, 80, 25);

t2.setBounds(100, 50, 100, 25);

l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

t4.setBounds(100, 130, 100, 25);

l5.setBounds(250, 10, 50, 25);

t5.setBounds(320, 10, 100, 25);

l6.setBounds(250, 50, 50, 25);

t6.setBounds(320, 50, 100, 25);

btn.setBounds(10, 170, 100, 25);

btn.addActionListener(this);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(l3);

this.add(t3);

this.add(l4);

this.add(t4);

this.add(l5);

this.add(t5);

this.add(l6);

this.add(t6);

this.add(btn);

this.setVisible(true);

g = this.getGraphics();

Point[] getCubeOnePoints(int x, int y, int length) {


62
Point[] points = new Point[4];

points[0] = new Point(x, y);

points[1] = new Point(x + length, y);

points[2] = new Point(x + length, y + length);

points[3] = new Point(x, y + length);

return points;

Point[] getCubeTwoPoints(int x, int y, int length, int shift) {

int newX = x + shift;

int newY = y + shift;

Point[] points = new Point[4];

points[0] = new Point(newX, newY);

points[1] = new Point(newX + length, newY);

points[2] = new Point(newX + length, newY + length);

points[3] = new Point(newX, newY + length);

return points;

Point[] drawCubeOnePointsAfterScaling(int x, int y, int length, int sx, int sy) {

Point[] points = new Point[4];

points[0] = new Point(x * sx, y * sy);

points[1] = new Point((x + length) * sx, y * sy);

points[2] = new Point((x + length) * sx, (y + length) * sy);

points[3] = new Point(x * sx, (y + length) * sy);

int x1 = points[0].x;

int y1 = points[0].y;

int x2 = points[1].x;

int y2 = points[1].y;

int x3 = points[2].x;

int y3 = points[2].y;
63
int x4 = points[3].x;

int y4 = points[3].y;

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

g.drawLine(x2, y2, x3, y3);

g.drawLine(x3, y3, x4, y4);

g.drawLine(x4, y4, x1, y1);

return points;

Point[] drawCubeTwoPointsAfterScaling(int x, int y, int length, int shift, int sx, int sy) {

int newX = x + shift;

int newY = y + shift;

Point[] points = new Point[4];

points[0] = new Point(newX * sx, newY * sy);

points[1] = new Point((newX + length) * sx, newY * sy);

points[2] = new Point((newX + length) * sx, (newY + length) * sy);

points[3] = new Point(newX * sx, (newY + length) * sy);

int x1 = points[0].x;

int y1 = points[0].y;

int x2 = points[1].x;

int y2 = points[1].y;

int x3 = points[2].x;

int y3 = points[2].y;

int x4 = points[3].x;

int y4 = points[3].y;

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

g.drawLine(x2, y2, x3, y3);

g.drawLine(x3, y3, x4, y4);

g.drawLine(x4, y4, x1, y1);

return points;
64
}

@Override

public void actionPerformed(ActionEvent e) {

int x = Integer.parseInt(t1.getText());

int y = Integer.parseInt(t2.getText());

int length = Integer.parseInt(t3.getText());

int shift = Integer.parseInt(t4.getText());

int sx = Integer.parseInt(t5.getText());

int sy = Integer.parseInt(t6.getText());

Point[] cubeOnePoints = getCubeOnePoints(x, y, length);

Point[] cubeTwoPoints = getCubeTwoPoints(x, y, length, shift);

g.drawRect(x, y, length, length);

g.drawRect(x + shift, y + shift, length, length);

g.drawString("Before Scaling", 100, 360);

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

g.drawLine(cubeOnePoints[i].x, cubeOnePoints[i].y, cubeTwoPoints[i].x,

cubeTwoPoints[i].y);

Point[] finalCubeOnePoints = drawCubeOnePointsAfterScaling(x, y, length, sx, sy);

Point[] finalCubeTwoPoints = drawCubeTwoPointsAfterScaling(x, y, length, shift, sx,

sy);

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

g.drawLine(finalCubeOnePoints[i].x, finalCubeOnePoints[i].y,

finalCubeTwoPoints[i].x, finalCubeTwoPoints[i].y);

g.drawString("After Scaling", 560, 620);

public static void main(String[] args) {

new Scaling3D();
65
}

Output:

66
19. WAP to demonstrate 3D Rotation.
import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Rotation3D extends JFrame implements ActionListener {

JLabel l1, l2, l3, l4, l5;

JTextField t1, t2, t3, t4, t5;

Graphics g;

Rotation3D() {

this.setTitle("3D Rotation");

this.setSize(800, 700);

this.setLayout(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

l1 = new JLabel("X:");

t1 = new JTextField();

l2 = new JLabel("Y:");

t2 = new JTextField();

l3 = new JLabel("Length:");

t3 = new JTextField();

l4 = new JLabel("Shift:");

t4 = new JTextField();

l5 = new JLabel("Angle:");

t5 = new JTextField();

JButton btn = new JButton("Draw");

l1.setBounds(10, 10, 80, 25);

t1.setBounds(100, 10, 100, 25);

l2.setBounds(10, 50, 80, 25);

t2.setBounds(100, 50, 100, 25);

67
l3.setBounds(10, 90, 80, 25);

t3.setBounds(100, 90, 100, 25);

l4.setBounds(10, 130, 80, 25);

t4.setBounds(100, 130, 100, 25);

l5.setBounds(250, 10, 50, 25);

t5.setBounds(320, 10, 100, 25);

btn.setBounds(10, 170, 100, 25);

btn.addActionListener(this);

this.add(l1);

this.add(t1);

this.add(l2);

this.add(t2);

this.add(l3);

this.add(t3);

this.add(l4);

this.add(t4);

this.add(l5);

this.add(t5);

this.add(btn);

this.setVisible(true);

g = this.getGraphics();

Point[] getCubeOnePoints(int x, int y, int length) {

Point[] points = new Point[4];

points[0] = new Point(x, y);

points[1] = new Point(x + length, y);

points[2] = new Point(x + length, y + length);

points[3] = new Point(x, y + length);

return points;

}
68
Point[] getCubeTwoPoints(int x, int y, int length, int shift) {

int newX = x + shift;

int newY = y + shift;

Point[] points = new Point[4];

points[0] = new Point(newX, newY);

points[1] = new Point(newX + length, newY);

points[2] = new Point(newX + length, newY + length);

points[3] = new Point(newX, newY + length);

return points;

Point[] drawCubeOnePointsAfterRotation(int x, int y, int length, double rad) {

int[][] orgMatrix = {

{x, x + length, x + length, x},

{y, y, y + length, y + length},

{1, 1, 1, 1}

};

double[][] rotationVector = {

{Math.cos(rad), -Math.sin(rad), 0},

{Math.sin(rad), Math.cos(rad), 0},

{0, 0, 1}

};

int[][] finalMatrix = new int[3][4];

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

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

finalMatrix[i][j] = 0;

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

finalMatrix[i][j] += rotationVector[i][k] * orgMatrix[k][j];

}
69
int x1 = finalMatrix[0][0];

int y1 = finalMatrix[1][0];

int x2 = finalMatrix[0][1];

int y2 = finalMatrix[1][1];

int x3 = finalMatrix[0][2];

int y3 = finalMatrix[1][2];

int x4 = finalMatrix[0][3];

int y4 = finalMatrix[1][3];

Point[] points = new Point[4];

points[0] = new Point(x1, y1);

points[1] = new Point(x2, y2);

points[2] = new Point(x3, y3);

points[3] = new Point(x4, y4);

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

g.drawLine(x2, y2, x3, y3);

g.drawLine(x3, y3, x4, y4);

g.drawLine(x4, y4, x1, y1);

return points;

Point[] drawCubeTwoPointsAfterRotation(int x, int y, int length, int shift, double rad) {

int newX = x + shift;

int newY = y + shift;

int[][] orgMatrix = {{newX, newX + length, newX + length, newX},{newY, newY, newY + length, newY + length},
{1, 1, 1, 1}};

double[][] rotationVector = {{Math.cos(rad), -Math.sin(rad), 0},{Math.sin(rad), Math.cos(rad), 0},{0, 0, 1}};

int[][] finalMatrix = new int[3][4];

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

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

finalMatrix[i][j] = 0;

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

70
finalMatrix[i][j] += rotationVector[i][k] * orgMatrix[k][j];

int x1 = finalMatrix[0][0];

int y1 = finalMatrix[1][0];

int x2 = finalMatrix[0][1];

int y2 = finalMatrix[1][1];

int x3 = finalMatrix[0][2];

int y3 = finalMatrix[1][2];

int x4 = finalMatrix[0][3];

int y4 = finalMatrix[1][3];

Point[] points = new Point[4];

points[0] = new Point(x1, y1);

points[1] = new Point(x2, y2);

points[2] = new Point(x3, y3);

points[3] = new Point(x4, y4);

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

g.drawLine(x2, y2, x3, y3);

g.drawLine(x3, y3, x4, y4);

g.drawLine(x4, y4, x1, y1);

return points;

@Override

public void actionPerformed(ActionEvent e) {

int x = Integer.parseInt(t1.getText());

int y = Integer.parseInt(t2.getText());

int length = Integer.parseInt(t3.getText());

int shift = Integer.parseInt(t4.getText());

int angle = Integer.parseInt(t5.getText());


71
double rad = Math.toRadians(angle);

Point[] cubeOnePoints = getCubeOnePoints(x, y, length);

Point[] cubeTwoPoints = getCubeTwoPoints(x, y, length, shift);

g.drawRect(x, y, length, length);

g.drawRect(x + shift, y + shift, length, length);

g.drawString("Before Rotation", 370, 530);

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

g.drawLine(cubeOnePoints[i].x, cubeOnePoints[i].y, cubeTwoPoints[i].x,

cubeTwoPoints[i].y);

g.drawString("Rotation in Counter-Clockwise", 200, 650);

Point[] finalCubeOnePoints = drawCubeOnePointsAfterRotation(x, y, length, rad);

Point[] finalCubeTwoPoints = drawCubeTwoPointsAfterRotation(x, y, length, shift, rad);

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

g.drawLine(finalCubeOnePoints[i].x,

finalCubeOnePoints[i].y,

finalCubeTwoPoints[i].x,

finalCubeTwoPoints[i].y

);

Point[] finalCubeOnePointsClockwise = drawCubeOnePointsAfterRotation(x, y, length,

-rad);

Point[] finalCubeTwoPointsClockwise = drawCubeTwoPointsAfterRotation(x, y,

length, shift, -rad);

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

g.drawLine(

finalCubeOnePointsClockwise[i].x,

finalCubeOnePointsClockwise[i].y,

finalCubeTwoPointsClockwise[i].x,

finalCubeTwoPointsClockwise[i].y
72
);

g.drawString("Rotation in Clockwise", 560, 280);

public static void main(String[] args) {

new Rotation3D();

Output:

73
20. WAP in Java to demonstrate boundary fill algorithm.
import javax.swing.JFrame;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.awt.image.WritableRaster;

public class BoundaryFill extends JFrame {

public static int width = 500;

public static int height = 500;

public BoundaryFill() {

setTitle("BoundaryFill");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(800, 600);

setVisible(true);

public void boundaryFill(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.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);

boundaryFill(x + 1, y, fill, boundary, raster);

boundaryFill(x - 1, y, fill, boundary, raster);

boundaryFill(x, y + 1, fill, boundary, raster);

boundaryFill(x, y - 1, fill, boundary, raster);

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

74
return pix1[0] == pix2[0] && pix1[1] == pix2[1] && pix1[2] == pix2[2] && pix1[3] == pix2[3]; }

@Override

public void paint(Graphics g) {

super.paint(g);

Graphics2D g2d = (Graphics2D) g;

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

Graphics2D big2d = bi.createGraphics();

big2d.setColor(Color.BLACK);

big2d.drawRect(250, 250, 100, 100);

WritableRaster raster = bi.getRaster();

boundaryFill(251, 251, Color.red, Color.BLACK, raster);

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

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(() -> new BoundaryFill()); }

Output:

21. WAP to demonstrate Flood fill algorithm.


75
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) { javax.swing.SwingUtilities.invokeLater(() -> {

JFrame frame = new JFrame("FloodFill algorithm"); 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.green);

g2.setBackground(Color.red);

g2.drawRect(100, 100, 100, 100);

addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

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

repaint();

});
76
}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(image, 0, 0, this);

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

if (image.getRGB(seedX, seedY) == rgb) {

image.setRGB(seedX, seedY, Color.yellow.getRGB());

if (seedX > 0 && seedX < image.getWidth() - 1 && seedY > 0 && seedY < image.getHeight() - 1)
{ 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:

22. WAP in JAVA program to demonstrate 2D lines


import javax.swing.*;

77
import java.awt.*;

public class Lines extends JFrame {

Lines() {

this.setTitle(" Line Types");

this.setSize(600, 800);

this.setVisible(true);

@Override

public void paint(Graphics g) {

super.paint(g);

Graphics2D g2D = (Graphics2D) g;

g2D.setStroke(new BasicStroke(4f));

g2D.drawLine(40, 100, 40, 300);

g2D.drawString("Normal Line", 20, 100);

g2D.setStroke(new BasicStroke(10f));

g2D.drawLine(120, 100, 120, 300);

g2D.drawString("Thick Line", 110, 90);

g2D.setStroke(new BasicStroke(1f));

g2D.drawLine(190, 100, 190, 300);

g2D.drawString("Thin Line", 190, 90);

float[] dash = {2f, 0f, 2f};

BasicStroke bs1 = new BasicStroke(4f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1.0f, dash, 2f);

g2D.setStroke(bs1);

g2D.drawLine(280, 100, 280, 300);

g2D.drawString("Dotted Line", 280, 90);

BasicStroke bs2 = new BasicStroke(10f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1.0f, dash, 2f);

g2D.setStroke(bs2);

g2D.drawLine(40, 350, 400, 350);

g2D.drawString("Thick Dotted Line", 200, 380);

BasicStroke bs3 = new BasicStroke(1f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND, 1.0f, dash, 2f);


78
g2D.setStroke(bs3);

g2D.drawLine(40, 420, 400, 420);

g2D.drawString("Dashed Line", 220, 440);

public static void main(String[] args) {

new Lines();

Output:

23. WAP in JAVA program to demonstrate text in java.


import javax.swing.*;

import java.awt.*;
79
public class Texts extends JPanel {

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

Font font = new Font("Times New Roman", Font.BOLD, 32); g2d.setFont(font);

g2d.setColor(Color.BLUE);

String text = " This is CG assignment" ;

int x = 50;

int y = 50;

g2d.drawString(text, x, y);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

JFrame frame = new JFrame("Text Display"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Texts textsPanel = new Texts();

textsPanel.setPreferredSize(new Dimension(400, 300)); frame.getContentPane().add(textsPanel);

frame.pack();

frame.setVisible(true);

});

}}

Output:

80

You might also like