CG Lab Report
CG Lab Report
CG Lab Report
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class D1 extends JFrame implements ActionListener { JLabel l1, l2, l3, l4;
Graphics g;
D1() {
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();
1
t4.setBounds(100, 130, 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
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;
float x = x0;
float y = y0;
y += yincrement;
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;
Graphics g;
BLALineAlgorithm() {
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();
4
t4.setBounds(100, 130, 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
int x0 = Integer.parseInt(t1.getText());
int y0 = Integer.parseInt(t2.getText());
int x1 = Integer.parseInt(t3.getText());
int y1 = Integer.parseInt(t4.getText());
p0 = 2 * dy - dx;
if (p0 >= 0) {
yn = yn + sy;
p0 = p0 + 2 * (dy - dx);
} else {
p0 = p0 + 2 * dy;
}}}
else {
p0 = 2 * dx - dy;
if (p0 >=0) {
xn = xn + sx;
yn = yn + sy;
yn = yn + sy;
p0 = p0 + 2 * dx;
}}}}
new BLALineAlgorithm();
Output:
6
3. General Circle Drawing Algorithm
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Graphics g;
GCD() {
l1 = new JLabel("h:");
this.add(l1);
t1 = new JTextField();
this.add(t1);
l2 = new JLabel("k:");
this.add(l2);
t2 = new JTextField();
this.add(t2);
l3 = new JLabel("r:");
this.add(l3);
t3 = new JTextField();
this.add(t3);
7
btn.addActionListener(this);
this.add(btn);
this.setSize(800, 700);
this.setLayout(null);
this.setVisible(true);
g = this.getGraphics();
@Override
int h = Integer.parseInt(t1.getText());
int k = Integer.parseInt(t2.getText());
int r = Integer.parseInt(t3.getText());
int xk = r;
int yk = 0;
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;
new GCD();
8
}
Output:
9
4.Midpoint Circle Algorithm Program
import javax.swing.*;
import java.awt.Graphics;
import java.awt.event.*;
Graphics g;
MidpointCircle() {
l1 = new JLabel("h:");
add(l1);
t1 = new JTextField();
add(t1);
l2 = new JLabel("k:");
add(l2);
t2 = new JTextField();
add(t2);
l3 = new JLabel("r:");
this.add(l3);
t3 = new JTextField();
this.add(t3);
10
btn.addActionListener(this);
this.add(btn);
this.setSize(600, 700);
this.setLayout(null);
this.setVisible(true);
g = this.getGraphics();
@Override
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;
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);
break;
xk = xk + 1;
if (pk >= 0) {
11
yk = yk - 1;
pk = pk + 2 * (xk - yk) + 1;
else {
pk = pk + 2 * xk + 1;
new MidpointCircle();
Output:
12
5.Translation
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Graphics g;
Translation() {
this.setTitle("11211/20 - 2D Translation");
this.setSize(800, 700);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXITONCLOSE);
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();
13
l2.setBounds(10, 50, 80, 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
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());
finalMatrix[i][j] = 0;
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;
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();
16
l2.setBounds(10, 50, 80, 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
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());
finalMatrix[i][j] = 0;
}}}
g = this.getGraphics();
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;
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();
19
l3.setBounds(10, 90, 80, 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();
double[][] rotationVector = {{Math.cos(rad), -Math.sin(rad), 0}, {Math.sin(rad), Math.cos(rad), 0}, {0, 0, 1}};
finalMatrix[i][j] = 0;
double[][] rotationVector = {{Math.cos(rad), -Math.sin(rad), 0}, {Math.sin(rad), Math.cos(rad), 0}, {0, 0, 1}};
finalMatrix[i][j] = 0;
@Override
int x1 = Integer.parseInt(t1.getText());
int y1 = Integer.parseInt(t2.getText());
int x2 = Integer.parseInt(t3.getText());
int y2 = Integer.parseInt(t4.getText());
counterClockWise(orgMatrix, rad);
clockWise(orgMatrix, -rad);
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;
Graphics g;
Refx() {
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();
23
t4.setBounds(100, 130, 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);
tempMatrix[i][j] = 0;
}}}
return tempMatrix;
@Override
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());
reflectedMatrix[i][j] = 0;
}}}
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;
Graphics g;
public Refy() {
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();
26
t4.setBounds(100, 130, 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();
tempMatrix[i][j] = 0;
}}
return tempMatrix;}
@Override
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());
}}}
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;
Graphics g;
RefO() {
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();
29
t2.setBounds(100, 50, 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();}
tempMatrix[i][j] = 0;
}}
return tempMatrix;}
@Override
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[][] orgMatrix = {{x1, x2, x3}, {y1, y2, y3}, {1, 1, 1}};
reflectedMatrix[i][j] = 0;
}}
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;
Graphics g;
Refyequalsx() {
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();
btn.addActionListener(this);
33
l2.setBounds(10, 50, 80, 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();
temp[i][j] = 0;
}}}
return temp;
@Override
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[][] orgMatrix = {{x1, x2, x3}, {y1, y2, y3}, {1, 1, 1}};
finalMatrix[i][j] = 0;
}}
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];
x1 = translatedMatrix[0][0];
y1 = translatedMatrix[1][0];
x2 = translatedMatrix[0][1];
y2 = translatedMatrix[1][1];
x3 = translatedMatrix[0][2];
y3 = translatedMatrix[1][2];
new Refyequalsx();
}}
Output:
36
12.WAP to demonstrate 2D reflection in y=mx+c
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Graphics g;
reflection_Y_mx() {
this.setSize(800, 700);
this.setLayout(null);
t1 = new JTextField();
t5 = new JTextField();
l7 = new JLabel("m:");
37
l8_ = new JLabel("c:");
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();
tempMatrix[i][j] = 0;
} }}
return tempMatrix;
39
@Override
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());
double[][] orgMatrix = {{x1, x2, x3}, {y1, y2, y3}, {1, 1, 1}};
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];
x1 = -(c / m);
40
y2 = c;
double a = m * m - 1;
double b = m * m + 1;
double[][] reflectionVector = {
reflectedMatrix[i][j] = 0;
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];
41
g.drawString("After Reflection", 160, 650);
new reflection_Y_mx();
Output:
42
13. WAP to demonstrate Shearing in X.
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
static int x1, y1, x2, y2, x3, y3, x4, y4;
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;
@Override
super.paintComponent(g);
43
g.drawString("After Shearing", px1 - 20, y1 - 20);
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;
static int x1, y1, x2, y2, x3, y3, x4, y4;
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
super.paintComponent(g);
46
g.drawLine(x4, y4, x1, y1);
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.*;
Graphics g;
Cohen_Sutherland() {
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();
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();
code |= LEFT;
code |= RIGHT;
code |= BOTTOM;
code |= TOP;
return code;
@Override
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());
while (true) {
accept = true;
break;
break;
else {
int code_out;
double x = 0, y = 0;
if (code1 != 0) {
code_out = code1;
else {
52
code_out = code2;
y = y_max;
y = y_min;
x = x_max;
x = x_min;
if (code_out == code1) {
x1 = x;
y1 = y;
else {
x2 = x;
y2 = y;
}
53
int tx = 300;
if (accept) {
else {
System.out.println("Line is rejected.");
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;
@Override
super.paintComponent(g);
int x1 = 50;
int y1 = 50;
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;
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();
57
l2.setBounds(10, 50, 80, 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();
return points;
return points;
@Override
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int tx = Integer.parseInt(t5.getText());
int ty = Integer.parseInt(t6.getText());
g.setColor(Color.BLACK);
g.setColor(Color.RED);
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;
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();
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();
return points;
return points;
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;
return points;
Point[] drawCubeTwoPointsAfterScaling(int x, int y, int length, int shift, int sx, int 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;
return points;
64
}
@Override
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int sx = Integer.parseInt(t5.getText());
int sy = Integer.parseInt(t6.getText());
cubeTwoPoints[i].y);
sy);
g.drawLine(finalCubeOnePoints[i].x, finalCubeOnePoints[i].y,
finalCubeTwoPoints[i].x, finalCubeTwoPoints[i].y);
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;
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();
67
l3.setBounds(10, 90, 80, 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();
return points;
}
68
Point[] getCubeTwoPoints(int x, int y, int length, int shift) {
return points;
int[][] orgMatrix = {
{1, 1, 1, 1}
};
double[][] rotationVector = {
{0, 0, 1}
};
finalMatrix[i][j] = 0;
}
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];
return points;
int[][] orgMatrix = {{newX, newX + length, newX + length, newX},{newY, newY, newY + length, newY +
length},{1, 1, 1, 1}};
finalMatrix[i][j] = 0;
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];
return points;
@Override
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
cubeTwoPoints[i].y);
g.drawLine(finalCubeOnePoints[i].x,
finalCubeOnePoints[i].y,
finalCubeTwoPoints[i].x,
finalCubeTwoPoints[i].y
);
-rad);
g.drawLine(
finalCubeOnePointsClockwise[i].x,
finalCubeOnePointsClockwise[i].y,
finalCubeTwoPointsClockwise[i].x,
finalCubeTwoPointsClockwise[i].y
72
);
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 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[] curColor = raster.getPixel(x, y, new int[] { 255, 255, 255, 255 });
raster.setPixel(x, y, fillColor);
74
return pix1[0] == pix2[0] && pix1[1] == pix2[1] && pix1[2] == pix2[2] && pix1[3] == pix2[3]; }
@Override
super.paint(g);
big2d.setColor(Color.BLACK);
g2d.drawImage(bi, 0, 0, null);
Output:
75
21. WAP to demonstrate Flood fill algorithm.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
frame.add(fill);
frame.pack();
frame.setVisible(true);
});
public FloodFill() {
setMinimumSize(getPreferredSize());
g2 = image.createGraphics();
g2.setColor(Color.green);
g2.setBackground(Color.red);
addMouseListener(new MouseAdapter() {
repaint();
76
}
});
@Override
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
if (seedX > 0 && seedX < image.getWidth() - 1 && seedY > 0 && seedY < image.getHeight() - 1) {
floodFill(seedX - 1, seedY - 1, rgb);
}}}}
Output:
77
22. WAP in JAVA program to demonstrate 2D lines
import javax.swing.*;
import java.awt.*;
Lines() {
this.setSize(600, 800);
this.setVisible(true);
@Override
super.paint(g);
g2D.setStroke(new BasicStroke(4f));
g2D.setStroke(new BasicStroke(10f));
g2D.setStroke(new BasicStroke(1f));
g2D.setStroke(bs1);
g2D.setStroke(bs2);
78
g2D.drawLine(40, 350, 400, 350);
g2D.setStroke(bs3);
new Lines();
Output:
79
23. WAP in JAVA program to demonstrate text in java.
import javax.swing.*;
import java.awt.*;
@Override
super.paintComponent(g);
g2d.setColor(Color.BLUE);
int x = 50;
int y = 50;
g2d.drawString(text, x, y);
SwingUtilities.invokeLater(() -> {
frame.pack();
frame.setVisible(true);
});
}}
Output:
80