membuat suatu gambar sederhana dari program java
disini akan dicoba untuk membuat grafika atau gambar dari sebuah tas
namun tas yang di buat ini sangatlah simpel
berikut programnya:
ü Source
code main program
package
grafikakomputer;
import
java.io.DataInputStream;
import
javax.swing.*;
public class
GrafikaKomputer extends JFrame{
DigitalDifferensialAnalizer garis = new
DigitalDifferensialAnalizer();
CircleMidpoint lingkaran = new
CircleMidpoint();
ElipsMidPoint elips = new ElipsMidPoint();
public GrafikaKomputer(){
setTitle("...");
add("Center", new
MainCanvas());
setSize(800,700);
// KOORDINAT TITIK
garis.drawLineDda(300,200,600,200);
//lurus atas dalam
garis.drawLineDda(300,200,300,400);
//garis bawah dalam kiri
garis.drawLineDda(300,400,600,400);
//lurus bawah dalam
garis.drawLineDda(600,200,600,400);
//lurus bawah dalam
garis.drawLineDda(320,190,580,190);
//garis atas
garis.drawLineDda(300,200,320,190);
//miring kiri
garis.drawLineDda(600,200,580,190);
//miring kanan
garis.drawLineDda(410,170,490,170); //atas
pegangan 1
garis.drawLineDda(410,170,410,195);
//garis pgngan 1
garis.drawLineDda(490,170,490,195);
//garis pegangan 2
garis.drawLineDda(400,160,500,160); //atas
pegangan 2
garis.drawLineDda(400,160,400,195);
//garis pgngn 3
garis.drawLineDda(500,160,500,195);
//garis pgangan 4
garis.drawLineDda(300,200,450,70);
//miring kiri 1
garis.drawLineDda(600,200,450,70); //miring
kanan 1
garis.drawLineDda(320,190,450,70);
//miring kiri 2
garis.drawLineDda(580,190,450,70);
//miring kanan 3
garis.drawLineDda(550,250,550,370);
//garis saku
elips.drawElips(565,255,15,5); //slerekan
setResizable(false);
setVisible(true);
}
public static void main (String[] args) {
new GrafikaKomputer();
}
}
ü Source
code dda
package
grafikakomputer;
public class
DigitalDifferensialAnalizer extends MainCanvas
{
public void drawLineDda(int x1, int y1,
int x2, int y2){
float dx, dy, a, b,x,y, xin,
yin,langkah;
dx = x2-x1;
dy= y2-y1;
a = Math.round(Math.abs(dx));
b= Math.round(Math.abs(dy));
x=x1;
y = y1;
if (a>b)
langkah= a;
else
langkah=b;
xin = dx/langkah;
yin = dy/langkah;
px.setPixel(Math.round(x),Math.round(y),
warna);
for(int i =
0;i<langkah;i++)
{
x = x+ xin;
y = y+yin;
px.setPixel(Math.round(x),Math.round(y),
warna);
}
} }
ü Source
code elips mid point
package
grafikakomputer;
public class
ElipsMidPoint extends MainCanvas{
ElipsMidPoint(){
}
private void elipsPlotPoints(int
xCenter,int yCenter,int x,int y){
px.setPixel(xCenter+x,yCenter+y,warna);
px.setPixel(xCenter-x,yCenter+y,warna);
px.setPixel(xCenter+x,yCenter-y,warna);
px.setPixel(xCenter-x,yCenter-y,warna);
}
public void drawElips(int xCenter,int
yCenter,int Rx, int Ry){
int
Rx2, Ry2, x,y,twoRx2, twoRy2, py, px;
float
p;
Rx2
= Rx*Rx;
Ry2
= Ry*Ry;
x=0;
y=Ry;
twoRx2=2*Rx2;
twoRy2=2*Ry2;
px=0;
py=twoRx2*y;
elipsPlotPoints(xCenter,yCenter,x,y);
p=Math.round(Ry2-(Rx2*Ry)+(0.25*Rx2));
while(px<py){
x=x+1;
px=px+twoRy2;
if(p<0){
p=p+(Ry2+px);
}
else{
y=y-1;
py=py-twoRx2;
p=p+(Ry2+px-py);
}
elipsPlotPoints(xCenter,
yCenter,x,y);
}
while (y>0){
y=y-1;
py=py-twoRx2;
if(p>0){
p=p+(Rx2-py);
}
else{
x=x+1;
px=px+twoRy2;
p=p+Ry2+px-py;
}
elipsPlotPoints(xCenter,
yCenter,x,y);
}
}
}