circle and line using java
import java.util.*;
import javax.swing.JFrame;
import java.lang.*;import java.awt.*;
import java.util.Scanner;
class Demo1 extends Canvas
{
int x, y;
public void paint(Graphics g)
{
Circle(g,50,500,200);
Circle(g,8,445,200);
Circle(g,8,555,200);
Circle(g,8,470,180);
Circle(g,8,530,180);
Smile(g,40,500,200);
line(g,500,170,500,220);
line(g,500,250,500,600);
line(g,600,400,500,300);
line(g,400,400,500,300);
line(g,600,600,500,500);
line(g,500,500,400,600);
}
public void line(Graphics g,int x0,int y0,int x1,int y1)
{
int dx,dy;
float Xinc,Yinc,x,y,steps;
dy=y1-y0;
dx=x1-x0;
g.fillOval(x0,y0,4,4);
if(dy>dx)
{
steps=Math.abs(dy);
}
else
{
steps=Math.abs(dx);
}
x=x0;
y=y0;
Yinc=dy/steps;
Xinc=dx/steps;
while(steps!=0)
{
steps--;
x=x+Xinc;
y=y+Yinc;
g.fillOval(Math.round(x),Math.round(y),4,4);
}
}
public void Circle(Graphics g,int r,int xc,int yc)
{
int d=1-r;
x=0;
y=r;
while(y>x)
{
fill(g,x,y,xc,yc);
x=x+1;
if(d<0)
{
d=d+(2*x)+1;
}
else
{
y=y-1;
d=d+(2*x)-(2*y)+1;
}
}
}
public void Smile(Graphics g,int r,int xc,int yc)
{
int d=1-r;
x=0;
y=r;
while(y>x)
{
fill1(g,x,y,xc,yc);
x=x+1;
if(d<0)
{
d=d+(2*x)+1;
}
else
{
y=y-1;
d=d+(2*x)-(2*y)+1;
}
}
}
public void fill(Graphics g,int x,int y,int xc,int yc)
{
g.fillOval(xc+x,yc+y,4,4);
g.fillOval(xc+y,yc+x,4,4);
g.fillOval(xc-y,yc+x,4,4);
g.fillOval(xc-x,yc+y,4,4);
g.fillOval(xc-x,yc-y,4,4);
g.fillOval(xc-y,yc-x,4,4);
g.fillOval(xc+y,yc-x,4,4);
g.fillOval(xc+x,yc-y,4,4);
}
public void fill1(Graphics g,int x,int y,int xc,int yc)
{
g.fillOval(xc+x,yc+y,4,4);
//g.fillOval(xc+y,yc+x,1,1);
//g.fillOval(xc-y,yc+x,1,1);
g.fillOval(xc-x,yc+y,4,4);
//g.fillOval(xc-x,yc-y,1,1);
//g.fillOval(xc-y,yc-x,1,1);
//g.fillOval(xc+y,yc-x,1,1);
//g.fillOval(xc+x,yc-y,1,1);
}
public static void main(String args[])
{
JFrame j=new JFrame();
Demo1 d=new Demo1();
j.add(d);
j.setVisible(true);
j.setSize(1000,1000);
}
}
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile--- javac Demo1.java
Run-----java Demo1
Comments
Post a Comment