Kamis, 21 Juni 2012

tugas 6 (LUKISAN) dengan Java

 Berikut ini adalah membuat gambar lukisan rumah menggunakan Algoritma  DDA, BRESENHEIM, dan Midpoint

SOURCE CODE nya sebagai berikut

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import javax.swing.*;
import javax.swing.JFrame;

public class lukis extends JPanel {
   
     double x_awal, x_akhir, y_awal, y_akhir, Dx, Dy, parameter, langkah, xincre, yincre, p, x2, y2, x_pusat, y_pusat;
    
    GeneralPath gp =  new GeneralPath();
    GeneralPath gp2 = new GeneralPath();
    GeneralPath gp3 = new GeneralPath();
    GeneralPath gp4 = new GeneralPath();
    GeneralPath gp5 = new GeneralPath();
    GeneralPath gp6 = new GeneralPath();
    GeneralPath gp7 = new GeneralPath();
    GeneralPath gp8 = new GeneralPath();
    GeneralPath gp9 = new GeneralPath();
   
    public lukis () {
        setBackground(Color.white);
        setLayout(null);
    }

    public void gambar_garis_dda(int x_awal, int y_awal, int x_akhir, int y_akhir) {
        this.x_awal = x_awal;
        this.x_akhir = x_akhir;
        this.y_awal = y_awal;

        Dx = Math.abs(x_akhir - x_awal);
        Dy = Math.abs(y_akhir - y_awal);

        if (Dx > Dy) {

            langkah = Dx;

        } else {

            langkah = Dy;

        }

        xincre = (x_akhir - x_awal) / langkah;
        yincre = (y_akhir - y_awal) / langkah;


        gp.moveTo(x_awal, y_awal);
        if (x_akhir - x_awal > 0) {

            for (double x = x_awal; x < x_akhir; x += xincre) {

                gp.lineTo(x, y_awal);
                y_awal += yincre;

            }
        } else {

            for (double x = x_awal; x > x_akhir; x += xincre) {

                gp.lineTo(x, y_awal);
                y_awal += yincre;

            }
        }


    }

     public void gambar_garis_Bressenham(int x_awal, int y_awal, int x_akhir, int y_akhir) {

        this.x_awal = x_awal;
        this.x_akhir = x_akhir;
        this.y_awal = y_awal;
        this.y_akhir = y_akhir;

        Dx = Math.abs(x_akhir - x_awal);
        Dy = Math.abs(y_akhir - y_awal);
        parameter = (2 * Dy) - Dx;

        this.repaint();

        gp.moveTo(x_awal, y_awal);
        double xt, yt;

        if (x_akhir - x_awal > 0) {
            xt = 1;

        } else {
            xt = -1;
        }

        if (y_akhir - y_awal > 0) {
            yt = 1;

        } else {
            yt = -1;
        }

        if (Dx > Dy) {
            if (x_akhir - x_awal > 0) {
                while (x_awal < x_akhir) {

                    if (parameter < 0) {

                        parameter += (2 * Dy);

                    } else {

                        y_awal += yt;
                        parameter = parameter + (2 * Dy) - (2 * Dx);

                    }

                    gp.lineTo(x_awal, y_awal);
                    x_awal += xt;
                }
            } else {

                while (x_awal > x_akhir) {

                    if (parameter < 0) {

                        parameter += (2 * Dy);

                    } else {

                        y_awal += yt;
                        parameter = parameter + (2 * Dy) - (2 * Dx);

                    }

                    gp.lineTo(x_awal, y_awal);
                    x_awal += xt;
                }

            }
        } else {
            if (y_akhir - y_awal > 0) {

                while (y_awal < y_akhir) {


                    if (parameter < 0) {

                        parameter += (2 * Dx);

                    } else {

                        x_awal += xt;
                        parameter = parameter + (2 * Dx) - (2 * Dy);

                    }

                    gp.lineTo(x_awal, y_awal);
                    y_awal += yt;
                }
            } else {

                while (y_awal > y_akhir) {


                    if (parameter < 0) {

                        parameter += (2 * Dx);

                    } else {

                        x_awal += xt;
                        parameter = parameter + (2 * Dx) - (2 * Dy);

                    }

                    gp.lineTo(x_awal, y_awal);
                    y_awal += yt;
                }

            }
        }

    }
    
     public void gambar_kotak_Bressenham(int x_awal, int y_awal, int x_akhir, int y_akhir) {
        this.x_awal = x_awal;
        this.x_akhir = x_akhir;
        this.y_awal = y_awal;
        Dx = Math.abs(x_akhir - x_awal);
        Dy = Math.abs(y_akhir - y_awal);

        parameter = (2 * Dy) - Dx;
        this.repaint();

        double g = x_awal, h = y_awal;
        while (x_awal < x_akhir) {
            if (parameter < 0) {

                parameter += (2 * Dy);
            } else {
                y_awal++;
                parameter = parameter + (2 * Dy) - (2 * Dx);
            }

            gp.moveTo(x_awal, y_awal);
            x_awal++;
        }

        gp.lineTo(g, y_awal);
        gp.lineTo(g, h);
        gp.lineTo(x_awal, h);
        gp.lineTo(x_awal, y_awal);

    }

    public void gambar_bulat_midpoint(int x_pusat, int y_pusat, int r) {
        x2 = 0;
        y2 = r;

        this.x_pusat = x_pusat;
        this.y_pusat = y_pusat;
        gp.moveTo(x_pusat + x2, y_pusat + y2);
        gp2.moveTo(x_pusat - x2, y_pusat + y2);
        gp3.moveTo(x_pusat + x2, y_pusat - y2);
        gp4.moveTo(x_pusat - x2, y_pusat - y2);
        gp5.moveTo(x_pusat + y2, y_pusat + x2);
        gp6.moveTo(x_pusat - y2, y_pusat + x2);
        gp7.moveTo(x_pusat + y2, y_pusat - x2);
        gp8.moveTo(x_pusat - y2, y_pusat - x2);


        p = 1 - r;
        while (x2 < y2) {
            gp.lineTo(x_pusat + x2, y_pusat + y2);
            gp2.lineTo(x_pusat - x2, y_pusat + y2);
            gp3.lineTo(x_pusat + x2, y_pusat - y2);
            gp4.lineTo(x_pusat - x2, y_pusat - y2);
            gp5.lineTo(x_pusat + y2, y_pusat + x2);
            gp6.lineTo(x_pusat - y2, y_pusat + x2);
            gp7.lineTo(x_pusat + y2, y_pusat - x2);
            gp8.lineTo(x_pusat - y2, y_pusat - x2);
            x2++;

            if (p < 0) {

                p += (2 * x2) + 6;

            } else {

                y2--;

                p += 2 * (x2 - y2) + 1;

            }


            this.repaint();
        }

    }
   
     public void gambar_elips_midpoint(int x_pusat, int y_pusat, double Rx, double Ry) {

        double Rx2 = Rx * Rx;
        double twoRx2 = 2 * Rx2;

        x2 = 0;
        double px = 0;
        double Ry2 = Ry * Ry;
        double twoRy2 = 2 * Ry2;
        y2 = Ry;

        double py = twoRx2 * y2;
        this.x_pusat = x_pusat;
        this.y_pusat = y_pusat;

        this.p = (Ry2 - (Rx2 * Ry) + (0.25 * Rx2));

        gp.moveTo(x_pusat + x2, y_pusat + y2);
        gp2.moveTo(x_pusat - x2, y_pusat + y2);
        gp3.moveTo(x_pusat + x2, y_pusat - y2);
        gp4.moveTo(x_pusat - x2, y_pusat - y2);

        while (px < py) {
            x2++;
            px += twoRy2;
            if (p < 0) {
                p += Ry2 + px;

            } else {
                y2--;
                py -= twoRx2;
                p += Ry2 + px - py;

            }

            gp.lineTo(x_pusat + x2, y_pusat + y2);
            gp2.lineTo(x_pusat - x2, y_pusat + y2);
            gp3.lineTo(x_pusat + x2, y_pusat - y2);
            gp4.lineTo(x_pusat - x2, y_pusat - y2);
        }

        this.p = Ry2 * (x2 + 0.5) * (x2 + 0.5) + Rx2 * (y2 - 1) * (y2 - 1) - Rx2 * Ry2;

        while (y2 > 0) {

            y2--;

            py -= twoRx2;

            if (p > 0) {

                p += Rx2 - py;

            } else {

                x2++;
                px += twoRy2;
                p += Ry2 + px - py;

            }

            gp.lineTo(x_pusat + x2, y_pusat + y2);
            gp2.lineTo(x_pusat - x2, y_pusat + y2);
            gp3.lineTo(x_pusat + x2, y_pusat - y2);
            gp4.lineTo(x_pusat - x2, y_pusat - y2);

        }

   
this.repaint();

     }
   
   
    public void paintComponent(Graphics gambar1) {
        super.paintComponents(gambar1);
        Graphics2D g2 = (Graphics2D) gambar1;

        GradientPaint backs = new GradientPaint(300, 90, new Color(51, 255, 255), 300, 350, new Color(0, 51, 255));
        g2.setPaint(backs);
        g2.fillRect(0, 0, 900, 900);
   
   
    // mewarnai badan rumah
        g2.setColor(Color.gray);
        g2.fillRect(200, 200, 450, 300);
       
       
    // genteng
        g2.setColor(new Color(204, 51, 0));
        gp9.moveTo(200, 200);
        gp9.lineTo(425, 35);
        gp9.lineTo(650, 200);
       
        gp9.closePath();
        g2.draw(gp9);
        g2.fill(gp9);
   
   
      // mewarnai  jendela 1
        g2.setColor(new Color(255, 255, 255));
        g2.fillRect(230, 220, 108, 55);
   
   
    // mewarnai  jendela 2
        g2.setColor(new Color(255, 255, 255));
        g2.fillRect(520, 220, 108, 55);
   
   
    // mewarnai pintu
        g2.setColor(Color.yellow);
        g2.fillRect(380, 340, 100, 160);
        
       
        // mewarnai bundar atas pintu
        g2.setColor(Color.orange);
        g2.fillOval(380, 300, 100, 100);
       
        
    // mewarnai  pintu
        g2.setColor(Color.yellow);
        g2.fillRect(380, 340, 100, 65);
       
       // mewarnai engsel pintu
        g2.setColor(new Color(0, 0, 0));
        g2.fillOval(380, 380, 15, 30);
       
     g2.setColor(Color.black);
        BasicStroke tebal = new BasicStroke(3.0f);
        g2.setStroke(tebal);
        g2.draw(gp);
        g2.draw(gp2);
        g2.draw(gp3);
        g2.draw(gp4);
        g2.draw(gp5);
        g2.draw(gp6);
        g2.draw(gp7);
        g2.draw(gp8);
       
       
      

      
    }
   
   
    public static void main(String[] args) {
        JFrame kom = new JFrame();
        kom.setTitle("umah gue");
        kom.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        lukis asa = new lukis();

        kom.getContentPane().add(asa);
        kom.setSize(800, 600);
        kom.setVisible(true);
        kom.setLocationRelativeTo(null);


   
   
   //membuat badan rumah
       
         asa.gambar_kotak_Bressenham(200, 200, 650, -100);
       
     // jendela 1
         asa.gambar_kotak_Bressenham(230, 220, 285, 7);
   
    // jendela 2
       
         asa.gambar_kotak_Bressenham(520, 220, 600, 165);
        
   // engsel pintu
    asa.gambar_elips_midpoint(400, 400, 10, 20);
   }
    }


OUTPUT  nya

0 komentar:

Posting Komentar