berikut adalah tampilan beserta Source Codenya.
Ellips midpoint
garis brasenham
garis DDA
kotak DDA
di atas merupakan tampilan dari algoritma DDA, Brasenham, Midpoint.
package algoritma;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
public class algoritma_1 extends javax.swing.JFrame {
private Color pixel[][] = new Color[2000][2000];
/** Creates new form algoritma */
public algoritma_1() {
initComponents();
}
public void drawline_DDA(Graphics g) {
super.paintComponents(g);
int langkah;
int xInc, yInc;
int x0 = 120,
y0 = 230;
int x1 = 270, y1 = 230;
int x = x0, y = y0;
int dx = x1 - x0,
dy = y1 - y0;
if (Math.abs(dx) > Math.abs(dy)) {
langkah = Math.abs(dx);
} else {
langkah = Math.abs(dy);
}
xInc = dx / langkah;
yInc = dy / langkah;
for (x = x0; x < x1; x++) {
x += xInc;
y += yInc;
putPixel(g, Math.round(x), Math.round(y), Color.black, 6);
}
}
void drawLine_bresenham(Graphics g) {
super.paintComponents(g);
int xa = 120, xb = 270, ya = 230, yb = 230, p, x, y, xend;
int dx = Math.abs(xb - xa);
int dy = Math.abs(yb - ya);
p = (2 * dy) - dx;
if (xa > xb) {
x = xb;
y = yb;
xend = xa;
} else {
x = xa;
y = ya;
xend = xb;
}
putPixel(g, Math.round(x), Math.round(y), Color.black, 1);
for (x = xa; x < xend; x++) {
if (p < 0) {
p = p + (2 * dy);
} else {
y = y + 1;
p = p + (2 * (dy - dx));
}
putPixel(g, Math.round(x), Math.round(y), Color.black, 1);
}
}
public void algoritma_midpoint(Graphics g) {
super.paintComponents(g);
int r = 50;
int x = 0, y = r, p = 1 - r, i = 1;
plotCirclePoints(g, x, y, Color.BLACK, 1);
while (x < y) {
x++;
if (p < 0) {
p += 2 * x + 1;
} else {
p += 2 * (x - y) + 1;
y--;
}
plotCirclePoints(g, x, y, Color.BLACK, 1);
i++;
}
}
private void putPixel(Graphics g, int x, int y, Color color, int size) {
try {
Graphics g2 = (Graphics2D) g;
g2.setColor(color.black);
g2.fillRect(x, y, size, size);
int pixX = Math.abs(x);
int pixY = Math.abs(y);
pixel[pixX][pixY] = color;
} catch (IndexOutOfBoundsException ex) {
} catch (Exception ex2) {
}
}
private void plotCirclePoints(Graphics g, int x, int y, Color color, int size) {
int xCenter = 200, yCenter = 250;
putPixel(g, xCenter + x, yCenter + y, color, size);
putPixel(g, xCenter - x, yCenter + y, color, size);
putPixel(g, xCenter + x, yCenter - y, color, size);
putPixel(g, xCenter - x, yCenter - y, color, size);
putPixel(g, xCenter + y, yCenter + x, color, size);
putPixel(g, xCenter - y, yCenter + x, color, size);
putPixel(g, xCenter + y, yCenter - x, color, size);
putPixel(g, xCenter - y, yCenter - x, color, size);
}
public void drawEllipseMidpoint(Graphics g) {
int rx = 50, ry = 90;
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;
plotEllipsePoints(g, x, y, Color.BLACK, 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;
}
plotEllipsePoints(g, x, y, Color.BLACK, 1);
i++;
}
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;
}
plotEllipsePoints(g, x, y, Color.BLACK, 1);
i++;
}
}
private void plotEllipsePoints(Graphics g, int x, int y, Color color, int size) {
int xCenter = 200, yCenter = 230;
putPixel(g, xCenter + x, yCenter + y, color, size);
putPixel(g, xCenter - x, yCenter + y, color, size);
putPixel(g, xCenter + x, yCenter - y, color, size);
putPixel(g, xCenter - x, yCenter - y, color, size);
}
public void kotak_dda(Graphics g) {
//variable
int a0, b0, a1, b1, dx, dy, step, tx, ty;
int x, y, x_tambah, y_tambah;
//inisial translasi
tx = 80;
ty = 70;
//koordinat titik
a0 = 70;
b0 = 70;
a1 = 200;
b1 = 200;
//translasi
int x0 = a0 + tx;
int x1 = a1 + tx;
int y0 = b0 + ty;
int y1 = b1 + ty;
dx = x1 - x0;
dy = y1 - y0;
x = x0;
y = y0;
//faktor pembagi
if (dx > dy) {
step = dx;
} else {
step = dy;
}
x_tambah = dx / step;
y_tambah = dy / step;
//menggambar kotak
for (int k = 0; k < step; k++) {
x += x_tambah;
y += y_tambah;
putPixel(g, Math.round(x), Math.round(y0), Color.black, 1);
putPixel(g, Math.round(x), Math.round(y1), Color.black, 1);
putPixel(g, Math.round(x0), Math.round(y), Color.black, 1);
putPixel(g, Math.round(x1), Math.round(y), Color.black, 1);
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel3 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jButton3 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
jPanel3.setBackground(new java.awt.Color(153, 51, 0));
jPanel2.setBackground(new java.awt.Color(102, 204, 0));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 382, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 332, Short.MAX_VALUE)
);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Macam - Macam Algoritma");
jPanel1.setBackground(new java.awt.Color(204, 102, 0));
jButton3.setText("lingkaran midpoint");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton2.setText("garis bresenham");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton5.setText("kotak_dda");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton4.setText("ellips midpoint");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton1.setText("garis DDA");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(19, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(95, 95, 95)
.addComponent(jLabel1)))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
drawline_DDA(getGraphics());
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
drawLine_bresenham(getGraphics());
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
algoritma_midpoint(getGraphics());
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
drawEllipseMidpoint(getGraphics());
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
kotak_dda(getGraphics());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new algoritma_1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
// End of variables declaration
}
Tidak ada komentar:
Posting Komentar