Professional Documents
Culture Documents
CGfile
CGfile
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.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.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);
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;
11
new MidpointCircle();
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();
12
l4 = new JLabel("Y2:");
t4 = new JTextField();
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);
13
this.add(l6);
this.add(t6);
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();
14
Output:
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();
15
l4 = new JLabel("Y2:");
t4 = new JTextField();
l5 = new JLabel("sx:");
t5 = new JTextField();
l6 = new JLabel("sy:");
t6 = new JTextField();
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);
16
this.add(btn);
this.setVisible(true);
@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 sx = Integer.parseInt(t5.getText());
int sy = Integer.parseInt(t6.getText());
int[][] scalingVector = {{sx, 0, 0}, {0, sy, 0}, {0, 0, 1}}; int[][] finalMatrix = new int[3][2];
finalMatrix[i][j] = 0;
g = this.getGraphics();
new Scaling();
17
Output:
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:");
18
t4 = new JTextField();
l5 = new JLabel("Angle:");
t5 = 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(btn);
this.setVisible(true);
g = this.getGraphics();
19
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];
finalMatrix[i][j] = 0;
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];
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());
20
double rad = Math.toRadians(angle);
counterClockWise(orgMatrix, rad);
clockWise(orgMatrix, -rad);
new Rotation();
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;
21
JTextField t1, t2, t3, t4;
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();
btn.addActionListener(this);
this.add(l1);
this.add(t1);
this.add(l2);
this.add(t2);
this.add(l3);
22
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());
int y2 = Integer.parseInt(t4.getText());
23
int[][] reflectedMatrix = new int[3][2];
reflectedMatrix[i][j] = 0;
new Refx();
Output:
24
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();
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();
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());
26
int y2 = Integer.parseInt(t4.getText());
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++) {
}}}
new Refy();
}}
Output:
27
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();
28
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(l6);
this.add(t6);
this.add(btn);
this.setVisible(true);
g = this.getGraphics();
29
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;
30
int y1Translated = translatedMatrix[1][0];
new RefO();
}}
Output:
31
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);
32
t2.setBounds(100, 50, 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();
33
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;
x1 = finalMatrix[0][0];
y1 = finalMatrix[1][0];
34
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:
35
12. 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:");
36
t7_ = new JTextField();
btn.addActionListener(this);
this.add(l1_);
this.add(t1);
this.add(l2_);
37
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);
this.setVisible(true);
g = this.getGraphics();
tempMatrix[i][j] = 0;
38
for (int k = 0; k < 3; k++) {
tempMatrix[i][j] += translationVector[i][k] *
orgMatrix[k][j];
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());
double[][] orgMatrix = {{x1, x2, x3}, {y1, y2, y3}, {1, 1, 1}};
x1 = (int) translatedMatrix[0][0];
y1 = (int) translatedMatrix[1][0];
39
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 = {
reflectedMatrix[i][j] = 0;
reflectedMatrix[i][j] += reflectionVector[i][k] *
translatedMatrix[k][j];
}}}
40
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];
new reflection_Y_mx();
}}
Output:
41
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
42
g.drawLine(px1, y1, px2, y2);
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);
43
}
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;
44
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
System.out.println("Enter x1:");
int x1 = sc.nextInt();
System.out.println("Enter y1:");
45
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);
f.setSize(600, 500);
f.setVisible(true);
Input:
46
Output:
import java.awt.*;
import java.awt.event.*;
Graphics g;
Cohen_Sutherland() {
this.setSize(800, 700);
this.setLayout(null);
47
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:");
t6 = new JTextField();
l7 = new JLabel("X2:");
t7 = new JTextField();
l8 = new JLabel("Y2:");
t8 = new JTextField();
48
t7.setBounds(320, 90, 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);
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();
49
code |= LEFT;
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) {
50
if ((code1 == 0) && (code2 == 0)) {
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;
51
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
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();
}}
52
Output:
import javax.swing.JFrame;
import javax.swing.JPanel;
@Override
super.paintComponent(g);
int x1 = 50;
int y1 = 50;
53
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;
54
public class Translation3D extends JFrame implements ActionListener { JLabel l1, l2, l3, l4, l5, l6;
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();
55
l5.setBounds(250, 10, 50, 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;
56
Point[] getCubeTwoPoints(int x, int y, int length, int shift) {
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);
57
g.drawRect(x + shift + tx, y + shift + ty, length, length);
Output:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Graphics g;
Scaling3D() {
this.setTitle(" 3D Scaling");
58
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();
59
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();
return points;
60
int newY = y + shift;
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;
61
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;
@Override
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int sx = Integer.parseInt(t5.getText());
62
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();
}}
Output:
63
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();
64
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(btn);
this.setVisible(true);
g = this.getGraphics();
65
points[2] = new Point(x + length, y + length);
return points;
return points;
int[][] orgMatrix = {
{1, 1, 1, 1}
};
double[][] rotationVector = {
{0, 0, 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;
int[][] orgMatrix = {{newX, newX + length, newX + length, newX},{newY, newY, newY + length, newY +
length},{1, 1, 1, 1}};
67
double[][] rotationVector = {{Math.cos(rad), -Math.sin(rad), 0},{Math.sin(rad), Math.cos(rad), 0},{0, 0,
1}};
finalMatrix[i][j] = 0;
}}}
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
68
public void actionPerformed(ActionEvent e) {
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);
69
length, shift, -rad);
g.drawLine(
finalCubeOnePointsClockwise[i].x,
finalCubeOnePointsClockwise[i].y,
finalCubeTwoPointsClockwise[i].x,
finalCubeTwoPointsClockwise[i].y
);
new Rotation3D();
}}
Output:
70
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[] fillColor = {
fill.getRed(), fill.getGreen(), fill.getBlue(), fill.getAlpha() };
int[] curColor = raster.getPixel(x, y, new int[] { 255, 255, 255, 255 });
}}
71
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:
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
72
public class FloodFill extends 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();
});
@Override
73
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:
74
21. 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);
75
BasicStroke bs2 = new BasicStroke(10f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1.0f, dash,
2f);
g2D.setStroke(bs2);
g2D.setStroke(bs3);
new Lines();
} }
Output:
import javax.swing.*;
import java.awt.*;
76
public class Texts extends JPanel {
@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:
77