import java.awt.*;
public class huruf_m extends Canvas{
public void SetPixel(Graphics Img, int x, int y){
Img.drawRect(x,y,1,1);
}
public void garisDDA(Graphics Img, int x1, int y1, int x2, int y2){
int steps;
float xInc, yInc;
int x = x1, y = y1;
float dx = x2-x1, dy = y2-y1;
if (Math.abs(dx)>Math.abs(dy))
steps = (int) Math.abs(dx);
else
steps = (int) Math.abs(dy);
xInc=dx/(float)steps;
yInc=dy/(float)steps;
for(int k=0; k<steps; k++){
x+=xInc;
y+=yInc;
SetPixel(Img, (int) Math.round(x), (int) Math.round (y));
}
}
/*0609U035 - dadan santana*/
private void ellips(Graphics Img, int xc, int yc, int rx, int ry){
int rx2 = rx * rx, ry2 = ry * ry;
int twoRx2 = 2 * rx2, twoRy2 = 2 * ry2;
int p, x = 0, y = ry, px = 0, py = twoRx2 * y, i = 0;
//SetPixel(Img, x+xc, y+yc);
//SetPixel(Img, -x+xc, y+yc);
SetPixel(Img, x+xc, -y+yc);
SetPixel(Img, -x+xc, -y+yc);
/* Region 1 */
p = (int) Math.round(ry2 - (rx2 * ry) + (0.25 * rx2));
while (px < py) {
x++;
px += twoRy2;
if (p < 0)
p += ry2 + px;
else {
y--;
py -= twoRx2;
p += ry2 + px - py;
}
//SetPixel(Img, x+xc, y+yc);
//SetPixel(Img, -x+xc, y+yc);
SetPixel(Img, x+xc, -y+yc);
SetPixel(Img, -x+xc, -y+yc);
i++;
}
/* Region 2 */
p = (int) Math.round(ry2 * (x + 0.5) * (x + 0.5) + rx2 * (y - 1) * (y - 1) - rx2 * ry2);
while (y > 0 ) {
y--;
py -= twoRx2;
if (p > 0)
p += rx2 - py;
else {
x++;
px += twoRy2;
p += rx2 - py + px;
}
//SetPixel(Img, x+xc, y+yc);
//SetPixel(Img, -x+xc, y+yc);
SetPixel(Img, x+xc, -y+yc);
SetPixel(Img, -x+xc, -y+yc);
i++;
}
}
//0609u035 - dadan santana
public huruf_m(){
setSize (100,100);
setBackground(Color.white);
}
public static void main (String[] argS){
huruf_m HY = new huruf_m();
Frame aFrame=new Frame();
aFrame.setSize(200,200);
aFrame.add(HY);
aFrame.setVisible(true);
}
@Override
public void paint(Graphics Img){
ellips(Img, 48, 40, 16, 20);
ellips(Img, 80, 40, 16, 20);
garisDDA(Img, 32, 40, 32, 85);
garisDDA(Img, 64, 40, 64, 80);
garisDDA(Img, 96, 40, 96, 85);
ellips(Img, 50, 130, 16, 20);
garisDDA(Img, 34, 110, 34, 156);
garisDDA(Img, 66, 123, 66, 156);
}
}
hasilnya :
semoga bermanfaat gan..:D
0 komentar:
Posting Komentar