Jumat, 13 Juli 2012

program graphic java grafkom

silahkan langsung di copas saja..:D
males ngmong banyak2 uy.. hehehe


import java.awt.*;

class GraphicsProgramxx extends Canvas {
public void SetPixel(Graphics Img, int x, int y) {
Img.drawRect(x, y, 1, 1);
}

public void GraphicsProgramxx() {
setSize(200, 200);
setBackground(Color.white);
}




public void garis(Graphics Img, int xstart, int ystart, int xend, int yend) {
int delta_x = xend - xstart;
int delta_y = yend - ystart;
int steps, k;
float xIncrement, yIncrement, x = xstart, y = ystart;

if (Math.abs(delta_x) > Math.abs(delta_y)) steps = Math.abs(delta_x);
else steps = Math.abs(delta_y);

xIncrement = (float) delta_x / (float) steps;
yIncrement = (float) delta_y / (float) steps;
this.SetPixel(Img, Math.round(x), Math.round(y));

for (k = 0; k < steps; k++) {
x += xIncrement;
y += yIncrement;
this.SetPixel(Img, Math.round(x), Math.round(y));
}
}

public void EllipseSetPixel(Graphics Img, int x, int y, int xc, int yc) {
// pencerminan titik
this.SetPixel(Img,  x+xc,  y+yc);
this.SetPixel(Img,  x+xc, -y+yc);
this.SetPixel(Img, -x+xc,  y+yc);
this.SetPixel(Img, -x+xc, -y+yc);
}


public void EllipseMidPoint(Graphics Img, int xc, int yc, int Rx, int Ry) {
// region 1
int x = 0, y = Ry;
double P = Math.pow(Ry, 2) - (Math.pow(Rx, 2) * Ry) + ((1/4) * Math.pow(Rx, 2));
double Px = 0, Py = 2 * (Math.pow(Rx, 2) * y);
this.EllipseSetPixel(Img, x, y, xc, yc);
while (Px < Py) {
x++;
Px += 2 * Math.pow(Ry, 2);
if (P >= 0) {
y--;
Py -= 2 * Math.pow(Rx, 2);
P += Math.pow(Ry, 2) + Px - Py;
} else {
P += Math.pow(Ry, 2) + Px;
}
this.EllipseSetPixel(Img, x, y, xc, yc);
}

// region 2
this.EllipseSetPixel(Img, x, y, xc, yc); // Plot(X,Y)
P = Math.pow(Ry, 2) * Math.pow((x+(1/2)), 2) + Math.pow(Rx, 2) * (y-1) - Math.pow(Rx, 2) * Math.pow(Ry, 2);

while (y > 0) {
y--;
Py -= 2 * Math.pow(Rx, 2);
if (P <= 0) {
x++;
Px += 2 * Math.pow(Ry, 2);
P += Math.pow(Rx, 2) + Px - Py;
} else {
P += Math.pow(Rx, 2) - Py;
}
this.EllipseSetPixel(Img, x, y, xc, yc);
}

}

public void CircleSetPixel(Graphics Img, int x, int y, int xc, int yc) {
// pencerminan titik
this.SetPixel(Img,  x + xc,  y + yc);   // oktan 1
this.SetPixel(Img, -x + xc,  y + yc); // oktan 2
this.SetPixel(Img,  x + xc, -y + yc); // oktan 3
this.SetPixel(Img, -x + xc, -y + yc); // oktan 4
this.SetPixel(Img,  y + xc,  x + yc); // oktan 5
this.SetPixel(Img, -y + xc,  x + yc); // oktan 6
this.SetPixel(Img,  y + xc, -x + yc); // oktan 7
this.SetPixel(Img, -y + xc, -x + yc); // oktan 8
}


public void CircleMidPoint(Graphics Img, int xc, int yc, int r) {
int x = 0; int y = r;
this.CircleSetPixel(Img, x, y, xc, yc);
int p = 1-r;
do {
if (p < 0) {
x += 1;
p += (2*x) + 1;
} else {
x += 1; y -= 1;

p += (2*x) - (2*y) + 1;
}
this.CircleSetPixel(Img, x, y, xc, yc);
} while (x < y);
}

public static void main(String[] args) {
GraphicsProgram GP = new GraphicsProgram();

Frame aFrame = new Frame();
aFrame.setSize(420, 355);
aFrame.add(GP);
aFrame.setVisible(true);
}

public void paint(Graphics Img) {

// koordinat garis-garis
int[][] garis = {
{0, 40, 80, 40},
{12, 12, 70, 65},
{40, 0, 40, 80},
{70, 12, 12, 65},
};

int x, y, iGaris = 0, xGaris = 0, yGaris = 0;

for (x=40; x < 400; x+=80) {
for (y=40; y < 400; y+=80) {

// menggambaar lingkaran
this.CircleMidPoint(Img, y, x, 40);

// menggambar garis
this.garis(Img, garis[iGaris][0] + xGaris, garis[iGaris][1] + yGaris,
 garis[iGaris][2] + xGaris, garis[iGaris][3] + yGaris);

// menggambar elipse
this.EllipseMidPoint(Img, x, y, 40, 10);

if (iGaris < 3) iGaris++;
else iGaris = 0;

if (xGaris < 310) xGaris += 80;
else xGaris = 0;
}
if (yGaris < 280) yGaris += 80;
else yGaris = 0;

}
}
}

hasilnya :


















semoga bermanfaat gan..:D

0 komentar:

Posting Komentar