Tugas 4 : Membuat Grafik Primitive Dengan Algoritma (DDA,Bresenhem, Midpoint)


never mind

Sebelum kita lebih jauh membahas tentang Grafik primitive , apa sih DDA, Brasenhem dan mid point itu ? adalah suatu metode pembentukan grafik dengan algoritma tertentu ,bisa dilihat lebih lanjut di sini .,

 

implementasi Algoritma DDA dalam Garis :

[spoiler title=”Garis DDA”]

package DDA;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class garis extends JPanel {

    private int x0, y0, x1, y1;

    public garis() {
    }

    public garis(int x0, int y0, int x1, int y1) {
        setX0(x0);
        setY0(y0);
        setX1(x1);
        setY1(y1);
    }

    public void paintLine(int x0, int y0, int x1, int y1) {
        JFrame frame = new JFrame("Garis DDA");
        frame.add(new garis(x0, y0, x1, y1));
        frame.setSize(250, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
//		g2d.setColor(Color.blue);
        int dx, dy, steps;
        int xInc, yIncrement, x, y;
        dx = x1 - x0;
        dy = y1 - y0;
        if (Math.abs(dx) > Math.abs(dy)) {
            steps = dx;
        } else {
            steps = dy;
        }
        xInc = dx / steps;
        yIncrement = dy / steps;
        x = x0;
        y = y0;
        g2d.drawLine(Math.round(x), Math.round(y), Math.round(x), Math.round(y));
        for (int i = 0; i < steps; i++) {
            x += xInc;
            y += yIncrement;
            g2d.drawLine(Math.round(x), Math.round(y), Math.round(x), Math.round(y));
        }

    }

    public int getX0() {
        return x0;
    }

    public void setX0(int x0) {
        this.x0 = x0;
    }

    public int getY0() {
        return y0;
    }

    public void setY0(int y0) {
        this.y0 = y0;
    }

    public int getX1() {
        return x1;
    }

    public void setX1(int x1) {
        this.x1 = x1;
    }

    public int getY1() {
        return y1;
    }

    public void setY1(int y1) {
        this.y1 = y1;
    }

    public static void main(String[] s) {
        int x0 = Integer.parseInt(JOptionPane.showInputDialog("X0"));
        int y0 = Integer.parseInt(JOptionPane.showInputDialog("Y0"));
        int x1 = Integer.parseInt(JOptionPane.showInputDialog("X1"));
        int y1 = Integer.parseInt(JOptionPane.showInputDialog("Y1"));
        garis ln = new garis(x0, y0, x1, y1);
        ln.paintLine(x0, y0, x1, y1);

    }
}

[/spoiler]

Implementasi Algoritma Brasenhem Pada garis :

[spoiler title=”Garis Brasenhem”]

package Brass;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class garis_bras extends JPanel {
    private int x0,y0,x1,y1 ;
//    xa =10;ya=10;xb=200;yb=10;

    public garis_bras() {
//        repaint();
    }

    public garis_bras(int x0, int y0, int x1, int y1) {
        setX0(x0);
        setY0(y0);
        setX1(x1);
        setY1(y1);
    }

    public void paintLine (int x0, int y0, int x1, int y1){
        JFrame frame = new JFrame("Bressenham");
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new  garis_bras( x0,y0,x1,y1));
        frame.setSize(250, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

	/**
	 *
	 */
//	private static final long serialVersionUID = 1L;

    @Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.setColor(Color.blue);
		int dx,dy,x,y, xEnd, p;
		dx = Math.abs(getX0() - getX1());
		dy = Math.abs(getY0() - getY1());
		p = 2 * dy - dx;
		if ( getX0() > getX1() ){
			x = getX1();
			y = getY1();
			xEnd = getX0();
		}
		else{
			x = getX0();
			y = getY0();
			xEnd = getX1();
		}

		g2d.drawLine(Math.round(x), Math.round(y),Math.round(x),Math.round(y));
		while ( x < xEnd){
			x += 1 ;

			if (p < 0){
				p += 2 * dy;
			}
			else{
				y +=1;
				p += 2* (dy - dx);
			}

			g2d.drawLine(Math.round(x), Math.round(y),Math.round(x),Math.round(y));
		}

	}

    public int getX0() {
        return x0;
    }

    public void setX0(int x0) {
        this.x0 = x0;
    }

    public int getY0() {
        return y0;
    }

    public void setY0(int y0) {
        this.y0 = y0;
    }

    public int getX1() {
        return x1;
    }

    public void setX1(int x1) {
        this.x1 = x1;
    }

    public int getY1() {
        return y1;
    }

    public void setY1(int y1) {
        this.y1 = y1;
    }
    public static void main(String[] s) {

       garis_bras ln = new garis_bras();
        ln.paintLine(100, 100, 200,100);
    }
}

[/spoiler]

Implementasi Midpoint pada Elips dan Lingkaran :

[spoiler title=”Elips Mid point”]

package midpoint;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class elips_mid extends JPanel{
    private int Rx, Ry, X1, Y1;

    public elips_mid() {
    }

    public elips_mid(int X1, int Y1, int Rx, int Ry) {
        setRx(Rx);
        setRy(Ry);
        setX1(X1);
        setY1(Y1);

    }

    public void paintEllipse (int X1, int Y1, int Rx, int Ry){
        JFrame frame = new JFrame("Ellipse");
        frame.add(new elips_mid(X1, Y1, Rx, Ry));
        frame.setSize(250, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        int xCentre = 0, yCentre = 0, p, px, py, x = 0, y = 0, Ry2, Rx2, twoRx2, twoRy2;

        g2d.drawLine(xCentre+x, yCentre+y, xCentre+x, yCentre+y);
        g2d.drawLine(xCentre-x, yCentre+y, xCentre-x, yCentre+y);
        g2d.drawLine(xCentre+x, yCentre-y, xCentre+x, yCentre-y);
        g2d.drawLine(xCentre-x, yCentre-y, xCentre-x, yCentre-y);

        Ry2 = Ry * Ry;
	Rx2 = Rx * Rx;
	twoRy2 = 2 * Ry2;
	twoRx2 = 2 * Rx2;

        x = 0;
	y = Ry;
	p = (int) Math.round(Ry2 - Rx2 * Ry + (0.25 * Rx2));
	px = 0;
	py = twoRx2 * y;

        //radius 1
        while(px= 0){
                y = y - 1;
                py = py - twoRx2;
            }
            g2d.drawLine(x+X1, y+Y1, x+X1, y+Y1);
            g2d.drawLine(x*(-1)+X1, y+Y1, x*(-1)+X1, y+Y1);
            g2d.drawLine(x*(-1)+X1, y*(-1)+Y1, x*(-1)+X1, y*(-1)+Y1);
            g2d.drawLine(x+X1, y*(-1)+Y1, x+X1, y*(-1)+Y1);

            if (p < 0){
               p = p + Ry2 + px;
             }else p = p + Ry2 + px - py;
         }
         //radius 2
         p = (int) Math.round(Ry2*(x + 0.5)*(x + 0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2);
            while (y > 0){
            y = y - 1;
            py = py - twoRx2;

            if (p  0){
                p = p + Rx2 - py;
            }else p = p + Rx2 - py + px;

        }

    }

    public int getRx() {
        return Rx;
    }

    public void setRx(int Rx) {
        this.Rx = Rx;
    }

    public int getRy() {
        return Ry;
    }

    public void setRy(int Ry) {
        this.Ry = Ry;
    }

    public int getX1() {
        return X1;
    }

    public void setX1(int X1) {
        this.X1 = X1;
    }

    public int getY1() {
        return Y1;
    }

    public void setY1(int Y1) {
        this.Y1 = Y1;
    }
    public static void main(String[] s) {
        int radx = Integer.parseInt(JOptionPane.showInputDialog("Radius titik x"));
        int rady = Integer.parseInt(JOptionPane.showInputDialog("Radius titik x"));
        int x = Integer.parseInt(JOptionPane.showInputDialog("X"));
        int y = Integer.parseInt(JOptionPane.showInputDialog("Y"));
        elips_mid ln = new elips_mid(radx, rady, x, y);
        ln.paintEllipse(radx, rady, x, y);

    }
}

[/spoiler]

[spoiler title=”Lingkaran Midpoint”]


package midpoint;

import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ling_mid extends JPanel{

    private int xCenter;
    private int yCenter;
    private int radius;

    public ling_mid() {
    }

    public ling_mid(int xCenter, int yCenter, int radius) {
        setXCenter(xCenter);
        setYCenter(yCenter);
        setRadius(radius);
    }

    public void paintCircle (int xCenter, int yCenter, int rad){
        JFrame frame = new JFrame("Circle Mid Point ");
        frame.add(new ling_mid(xCenter, yCenter, rad));
        frame.setSize(250, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    @Override
	public void paintComponent(Graphics g){

//		xCenter=50;yCenter=50;radius=40;
		int p,x,y;
		p=x=y=0;

		y = radius;

		g.drawLine( xCenter + x, yCenter + y, xCenter + x, yCenter + y);
		g.drawLine( xCenter - x, yCenter + y, xCenter - x, yCenter + y);
		g.drawLine( xCenter + x, yCenter - y, xCenter + x, yCenter - y);
		g.drawLine( xCenter - x, yCenter - y, xCenter - x, yCenter - y);
		g.drawLine( xCenter + y, yCenter + x, xCenter + y, yCenter + x);
		g.drawLine( xCenter - y, yCenter + x, xCenter - y, yCenter + x);
		g.drawLine( xCenter + y, yCenter - x, xCenter + y, yCenter - x);
		g.drawLine( xCenter - y, yCenter - x, xCenter - y, yCenter - x);

		p = 1 - radius;
		  while (x < y){
			if (p < 0){
			   x = x+1;
			}
			else{
			     x = x+1;
			     y = y-1;
			}

			if (p < 0){
				  p = p+2*x+1;
			}
			else{
			   p = p+2*(x-y)+1;
			}

			g.drawLine(xCenter + x, yCenter + y,xCenter + x, yCenter + y);
			g.drawLine(xCenter - x, yCenter + y,xCenter - x, yCenter + y);
			g.drawLine(xCenter + x, yCenter - y,xCenter + x, yCenter - y);
			g.drawLine(xCenter - x, yCenter - y, xCenter - x, yCenter - y);
			g.drawLine(xCenter + y, yCenter + x, xCenter + y, yCenter + x);
			g.drawLine(xCenter - y, yCenter + x,xCenter - y, yCenter + x);
			g.drawLine(xCenter + y, yCenter - x,xCenter + y, yCenter - x);
			g.drawLine(xCenter - y, yCenter - x,xCenter - y, yCenter - x);
		  }
	}

    public int getXCenter() {
        return xCenter;
    }

    public void setXCenter(int xCenter) {
        this.xCenter = xCenter;
    }

    public int getYCenter() {
        return yCenter;
    }

    public void setYCenter(int yCenter) {
        this.yCenter = yCenter;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }
    public static void main(String[] s) {
        int x = Integer.parseInt(JOptionPane.showInputDialog("X0"));
        int y = Integer.parseInt(JOptionPane.showInputDialog("Y0"));
        int rad = Integer.parseInt(JOptionPane.showInputDialog("Y1"));
        ling_mid ln = new ling_mid(x, y, rad);
        ln.paintCircle(x, y, rad);
    }
}

[/spoiler]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s