Author: BIM Notes
Java Program In Computer graphics
1. Write a java program to draw a line using DDA Algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.applet.Applet; import java.awt.Graphics; public class DDA_extends Applet { public void paint(Graphics g) { double dx,dy,steps,x,y,j; double xc,yc; double x1=200, y1= 500, x2= 600, y2= 200; 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(x, y, width, height); g.fillOval((int)x, (int)y, 5, 5); for(j=1; j<=steps; j++) { x = x+xc; y= y+yc; g.fillOval((int)x, (int)y, 5, 5); System.out.println((int)y); } } } |
2.Write a java program to draw a line using Brasenhem Algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package computer_Graphics; import java.applet.*; import java.awt.*; public class Bresenhem extends Applet { double x1=200,y1=100,x2=300,y2=500; double dx,dy,steps,x,y,j; double xc,yc; public void paint(Graphics g) { int ch=6; dx=Math.abs(x2-x1); dy=Math.abs(y2-y1); x=x1; y=y1; double p=(2*dy)-(dx); int i=1; one: for(j=10;ch==6;j++) { g.fillOval((int)x,(int)y,5,5); while(p>=0) { y=y+1; p=p-(2*dx); } x=x+1; p=p+(2*dy); i++; if(i<=dx) ch=6; else break one; } } } |
3. Write a java program to draw a line using mid-point Algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package computer_Graphics; import java.awt.*; import java.applet.*; public class MidPointCircle extends Applet { public void paint(Graphics g) { int xc=120; int yc=120; int rad=100; int X=0; int Y=rad; int p = 5/4-rad; while(X<Y) { if(p<0) { X++; p=p+2*X+2+1; } else { X++; Y--; p = p+2*X+2+1-2*Y+2; } g.fillOval(xc+X,yc+Y,2,2); g.fillOval(xc+Y,yc-X,2,2); g.fillOval(xc-Y,yc+X,2,2); g.fillOval(xc-X,yc+Y,2,2); g.fillOval(xc+X,yc-Y,2,2); g.fillOval(xc+Y,yc+X,2,2); g.fillOval(xc-Y,yc-X,2,2); g.fillOval(xc-X,yc-Y,2,2); } } } |
4.Write a java program to perform translation on a line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package computer_Graphics; import java.awt.Graphics; import javax.swing.JFrame; class Translation extends JFrame{ int x1 = 50; int y1 = 50; int x2 = 150; int y2 = 150; int tx = 200; int ty = 200; int px1,px2,py1,py2; public Translation() { setTitle("Line Translation"); setSize(500,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); px1 = x1+tx; px2 = x2+tx; py1 = y1+ty; py2 = y2+ty; setVisible(true); } public void paint(Graphics g) { g.drawLine(x1, y1, x2, y2); g.drawLine(px1, py1, px2, py2); } public static void main(String args[]) { new Translation(); } } |
5.Write a java program to […]
Continue Reading