CG Practical File
CG Practical File
1
1.DDA Algorithm
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class DDA extends JFrame implements ActionListener { JLabel l1, l2, l3, l4;
Graphics g;
DDA() {
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();
2
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
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;
x += xincrement;
y += yincrement;
3
}}
}}
Output
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:");
4
t3 = new JTextField();
l4 = new JLabel("Y2:");
t4 = 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);
his.add(btn);
this.setVisible(true);
g = this.getGraphics();
@Override
int y0 = Integer.parseInt(t2.getText());
int x1 = Integer.parseInt(t3.getText());
int y1 = Integer.parseInt(t4.getText());
5
int dx = Math.abs(x1 - x0);
p0 = 2 * dy - dx;
xn = xn + sx;
if (p0 >= 0) {
yn = yn + sy;
p0 = p0 + 2 * dy;
}}
} else {
p0 = 2 * dx - dy;
xn = xn + sx;
yn = yn + sy;
yn = yn + sy;
p0 = p0 + 2 * dx;
6
Output:
import java.awt.*;
import java.awt.event.*;
Graphics g;
GeneralCDrawingAlgorithm() {
l1 = new JLabel("h:");
this.add(l1);
t1 = new JTextField();
this.add(t1);
l2 = new JLabel("k:");
this.add(l2);
t2 = new JTextField();
7
this.add(t2);
l3 = new JLabel("r:");
this.add(l3);
t3 = new JTextField();
this.add(t3);
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);
8
g.fillRect(-xk + h, -yk + k, 2, 2);
xk = xk - 1;
new GeneralCDrawingAlgorithm();
Output:
import javax.swing.*;
import java.awt.Graphics;
import java.awt.event.*;
9
JTextField t1, t2, t3;
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);
btn.addActionListener(this);
this.add(btn);
this.setSize(600, 700);
this.setLayout(null);
this.setVisible(true);
g = this.getGraphics();
10
@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) {
yk = yk - 1;
pk = pk + 2 * (xk - yk) + 1;
else {
pk = pk + 2 * xk + 1;
} }}
new MidpointCircle();
}}
11
Output:
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;
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();
12
l5 = new JLabel("tx:");
t5 = new JTextField();
l6 = new JLabel("ty:");
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);
13
this.add(btn);
this.setVisible(true);
g = this.getGraphics();
@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 tx = Integer.parseInt(t5.getText());
int ty = Integer.parseInt(t6.getText());
finalMatrix[i][j] = 0;
}}}
new Translation();
}}
Output:
14
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();
15
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());
int y1 = Integer.parseInt(t2.getText());
16
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:
17
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;
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();
18
l4.setBounds(10, 130, 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;
}}}
19
}
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();
20
Output:
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();
21
l3 = new JLabel("X2:");
t3 = new JTextField();
l4 = new JLabel("Y2:");
t4 = 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(btn);
22
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());
reflectedMatrix[i][j] = 0;
}}}
23
public static void main(String[] args) {
new Refx();
}}
Output:
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:");
24
t2 = new JTextField();
l3 = new JLabel("X2:");
t3 = new JTextField();
l4 = new JLabel("Y2:");
t4 = 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(btn);
this.setVisible(true);
g = this.getGraphics();
25
for (int i = 0; i < 3; i++) {
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());
}}}
26
}
new Refy();
}}
Output:
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();
27
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);
this.add(l1);
this.add(t1);
this.add(l2);
this.add(t2);
this.add(l3);
this.add(t3);
this.add(l4);
this.add(t4);
28
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}};
29
int[][] translatedMatrix = translate(orgMatrix, 400, 350);
reflectedMatrix[i][j] = 0;
}}
30
public static void main(String[] args) {
new RefO();
}}
Output:
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();
31
l4 = new JLabel("Y2:");
t4 = new JTextField();
l5 = new JLabel("X3:");
t5 = new JTextField();
l6 = new JLabel("Y3:");
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);
32
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}};
33
int[][] reflectionVector = {{0, 1, 0}, {1, 0, 0}, {0, 0, 1}};
finalMatrix[i][j] = 0;
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();
34
}}
Output:
import java.awt.*;
import java.awt.event.*;
Graphics g;
reflection_Y_mx() {
this.setSize(800, 700);
this.setLayout(null);
t1 = new JTextField();
35
l2_ = new JLabel("Y1:");
t5 = new JTextField();
l7 = new JLabel("m:");
36
l6_.setBounds(10, 210, 50, 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(l7);
this.add(t7_);
this.add(l8_);
this.add(t8_);
this.add(btn);
37
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 m = Integer.parseInt(t7_.getText());
int c = Integer.parseInt(t8_.getText());
38
g.drawLine(0, 350, 800, 350);
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);
y2 = c;
double a = m * m - 1;
double b = m * m + 1;
double[][] reflectionVector = {
39
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];
new reflection_Y_mx();
}}
Output:
40
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
41
g.drawLine(px2, y2, px3, y3);
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);
f.setVisible(true);
42
Input:
Output:
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;
43
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
System.out.println("Enter x1:");
int x1 = sc.nextInt();
System.out.println("Enter y1:");
int y1 = sc.nextInt();
44
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);
f.setSize(600, 500);
f.setVisible(true);
Input:
45
Output:
import java.awt.*;
import java.awt.event.*;
Graphics g;
Cohen_Sutherland() {
this.setSize(800, 700);
this.setLayout(null);
l1 = new JLabel("Xmin:");
46
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:");
t6 = new JTextField();
l7 = new JLabel("X2:");
t7 = new JTextField();
l8 = new JLabel("Y2:");
t8 = new JTextField();
47
l8.setBounds(250, 130, 50, 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);
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;
48
else if (x > x_max) // to the right of rectangle
code |= RIGHT;
code |= BOTTOM;
code |= TOP;
return code;
@Override
x_min = Integer.parseInt(t1.getText());
y_min = Integer.parseInt(t2.getText());
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) {
49
accept = true;
break;
break;
else {
int code_out;
double x = 0, y = 0;
if (code1 != 0) {
code_out = code1;
else {
code_out = code2;
y = y_max;
y = y_min;
x = x_max;
50
x = x_min;
if (code_out == code1) {
x1 = x;
y1 = y;
else {
x2 = x;
y2 = y;
int tx = 300;
if (accept) {
else {
System.out.println("Line is rejected.");
}}
new Cohen_Sutherland();
}}
51
Output:
import javax.swing.JFrame;
import javax.swing.JPanel;
@Override
super.paintComponent(g);
int x1 = 50;
int y1 = 50;
52
int yt1 = (int) (y1 * sy);
mango.add(new WindowViewPort());
mango.setVisible(true);
}}
Output:
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;
Graphics g;
53
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();
54
t6.setBounds(320, 50, 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();
return points;
55
Point[] points = new Point[4];
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);
56
g.drawLine(cubeOnePoints[i].x + tx, cubeOnePoints[i].y + ty, cubeTwoPoints[i].x + tx, cubeTwoPoints[i].y
+ ty);
Output:
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);
57
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);
58
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;
59
points[2] = new Point(newX + length, newY + length);
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;
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) {
60
points[1] = new Point((newX + length) * sx, newY * 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;
@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());
61
g.drawString("Before Scaling", 100, 360);
cubeTwoPoints[i].y);
sy);
g.drawLine(finalCubeOnePoints[i].x, finalCubeOnePoints[i].y,
finalCubeTwoPoints[i].x, finalCubeTwoPoints[i].y);
new Scaling3D();
}}
Output:
import java.awt.*;
62
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();
63
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(l5);
this.add(t5);
this.add(btn);
this.setVisible(true);
g = this.getGraphics();
return points;
64
int newY = y + shift;
return points;
int[][] orgMatrix = {
{1, 1, 1, 1}
};
double[][] rotationVector = {
{0, 0, 1}
};
finalMatrix[i][j] = 0;
int x1 = finalMatrix[0][0];
65
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;
66
for (int k = 0; k < 3; k++) {
}}}
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());
67
double rad = Math.toRadians(angle);
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,
68
finalCubeTwoPointsClockwise[i].y
);
new Rotation3D();
}}
Output:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
69
public class BoundaryFill extends JFrame {
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);
return pix1[0] == pix2[0] && pix1[1] == pix2[1] && pix1[2] == pix2[2] && pix1[3] == pix2[3]; }
@Override
super.paint(g);
70
big2d.setColor(Color.BLACK);
g2d.drawImage(bi, 0, 0, null);
Output:
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
71
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();
});
@Override
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
72
if (seedX > 0 && seedX < image.getWidth() - 1 && seedY > 0 && seedY < image.getHeight() - 1) {
floodFill(seedX - 1, seedY - 1, rgb);
Output:
import java.awt.*;
Lines() {
73
this.setTitle(" Line Types");
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);
74
g2D.setStroke(bs3);
new Lines();
Output:
import java.awt.*;
@Override
super.paintComponent(g);
g2d.setColor(Color.BLUE);
75
int x = 50;
int y = 50;
g2d.drawString(text, x, y);
SwingUtilities.invokeLater(() -> {
frame.pack();
frame.setVisible(true);
});
Output:
76