Вы находитесь на странице: 1из 14

Program No.

1
Aim: Write a program to move an object using the concept of 2-D
translation transformation.
Input:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Surface extends JPanel {


private void doDrawing(final Graphics g) {
final Graphics2D g2d = (Graphics2D) g.create();
g2d.setPaint(new Color(150, 150, 150));
g2d.fillRect(20, 20, 80, 50);
g2d.translate(150, 50);
g2d.fillRect(20, 20, 80, 50);
g2d.dispose();
}

@Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}

public class TranslationEx extends JFrame {


public TranslationEx() {
initUI();
}

private void initUI() {


add(new Surface());
setTitle("Translation");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
final TranslationEx ex = new TranslationEx();
ex.setVisible(true);
}
});
}
}

Output:
Program No. 2
Aim: Write a program to move an object using the concept of 2-D rotation
transformation.
Input:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Surface extends JPanel {


private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setPaint(new Color(150, 150, 150));
g2d.fillRect(20, 20, 80, 50);
g2d.translate(180, -50);
g2d.rotate(Math.PI / 4);
g2d.fillRect(80, 80, 80, 50);
g2d.dispose();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}

public class RotationEx extends JFrame {


public RotationEx() {
initUI();
}

private void initUI() {


setTitle("Rotation");
add(new Surface());
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
RotationEx ex = new RotationEx();
ex.setVisible(true);
}
});
}
}

Output:
Program No. 3
Aim: Write a program to move an object using the concept of 2-D scaling
transformation.
Input:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Surface extends JPanel {


private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(new Color(150, 150, 150));
g2d.fillRect(20, 20, 80, 50);
AffineTransform tx1 = new AffineTransform();
tx1.translate(110, 22);
tx1.scale(0.5, 0.5);
g2d.setTransform(tx1);
g2d.fillRect(0, 0, 80, 50);
AffineTransform tx2 = new AffineTransform();
tx2.translate(170, 20);
tx2.scale(1.5, 1.5);
g2d.setTransform(tx2);
g2d.fillRect(0, 0, 80, 50);
g2d.dispose();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}

public class ScalingEx extends JFrame {


public ScalingEx() {
initUI();
}
private void initUI() {
add(new Surface());
setTitle("Scaling");
setSize(330, 160);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {


EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ScalingEx ex = new ScalingEx();
ex.setVisible(true);
}
});
}
}

Output:
Program No. 4
Aim: Write a program to move an object using the concept of 2-D shearing
transformation.
Input:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Surface extends JPanel {


private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
AffineTransform tx1 = new AffineTransform();
tx1.translate(50, 90);
g2d.setTransform(tx1);
g2d.setPaint(Color.green);
g2d.drawRect(0, 0, 160, 50);
AffineTransform tx2 = new AffineTransform();
tx2.translate(50, 90);
tx2.shear(0, 1);
g2d.setTransform(tx2);
g2d.setPaint(Color.blue);
g2d.draw(new Rectangle(0, 0, 80, 50));
AffineTransform tx3 = new AffineTransform();
tx3.translate(130, 10);
tx3.shear(0, 1);
g2d.setTransform(tx3);
g2d.setPaint(Color.red);
g2d.drawRect(0, 0, 80, 50);
g2d.dispose();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}

public class ShearingEx extends JFrame {


public ShearingEx() {
initUI();
}

private void initUI() {


add(new Surface());
setTitle("Shearing");
setSize(330, 270);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {


EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ShearingEx ex = new ShearingEx();
ex.setVisible(true);
}
});
}
}

Output:
Program No. 5
Aim: Write a program to implement DDA line drawing algorithm.
Input:

import java.awt.*;
import java.applet.*;
public class DDA extends Applet {
@Override
public void paint(Graphics g) {
double dx, dy, steps, x, y, k;
double xc, yc;
double x1 = 50, y1 = 200, x2 = 250, y2 = 10;
dx = x2 - x1;
dy = y2 - y1;
if (Math.abs(dx) > Math.abs(dy))
steps = Math.abs(dx);
else
steps = Math.abs(dy);
xc = (dx / steps);
yc = (dy / steps);
x = x1;
y = y1;
g.fillOval(200, 500, 5, 5);
for (k = 1; k <= steps; k++) {
x = x + xc;
y = y + yc;
g.fillOval((int) x, (int) y, 5, 5);
}
}
}
Output:
Program No. 6
Aim: Write a program to implement Bresenhams line drawing algorithm.
Input:

import java.applet.*;
import java.awt.*;
public class Bresenham extends Applet {
@Override
public void paint(Graphics g) {
int x, y, k;
double dx, dy, p;
int x1 = 50, y1 = 50, x2 = 250, y2 = 250;
dx = Math.abs(x2 - x1);
dy = Math.abs(y2 - y1);
x = x1;
y = y1;
p = 2 * dy - dx;
g.fillOval(200, 300, 5, 5);
for (k = 0; k < dx; k++) {
if (p < 0) {
g.fillOval(x++, y, 5, 5);
p = p + (2 * dy);
} else {
g.fillOval(x++, y++, 5, 5);
p = p + (2 * (dy - dx));
}
}
}
}
Output:
Program No. 7
Aim: Write a program to implement Bresenhams circle drawing
algorithm.
Input:

import java.awt.*;
import java.applet.*;
public class Brescir1 extends Applet
{
@Override
public void paint(Graphics g)
{
int xc,yc,r;
xc=125;
yc=125;
r=60;
int x=0;
int y=r;
int p=3-2*r;
do
{
if(p<0)
p=p+4*x+6;
else
{
p=p+4*(x-y)+10;
y=y-1;
}
x=x+1;
g.drawLine(xc+x,yc+y,xc+x,yc+y);
g.drawLine(x+xc,yc-y,xc+x,yc-y);
g.drawLine(xc-x,yc+y,xc-x,yc+y);
g.drawLine(xc-x,yc-y,xc-x,yc-y);
g.drawLine(xc+y,yc+x,xc+y,yc+x);
g.drawLine(xc+y,yc-x,xc+y,yc-x);
g.drawLine(xc-y,yc+x,xc-y,yc+x);
g.drawLine(xc-y,yc-x,xc-y,yc-x);
}
while(x<y);
}
}

Output:

Вам также может понравиться