Kamis, 05 Juli 2012

Membuat Objek Ellips Dengan Menggunakan Netbeans
 
Sehubungan dengan diberikannya tugas Pemrograman Komputer Grafis untuk membuat bentuk ellipse, maka kelompok kami menggunakan Java untuk membuat objek berbentuk ellipse.

Source codenya seperti berikut :

  •     Pertama kita membuat class untuk garis.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Tugas2;

import Tugas1.*;
import java.awt.Graphics;

/**
 *
 * @author JC
 */
public class Garis {
  
    public void Garisku (Graphics g, int x0, int y0, int x1, int y1){
        int dx,dy,steps;
        int x_tambah,y_tambah,x,y;
                            
        dx= x1-x0;
        dy= y1-y0;    
               
        if (Math.abs(dx) > Math.abs(dy)){
            steps = Math.abs(dx);
        }
        else{
            steps = Math.abs(dy);  
        }
        x_tambah = dx / steps;
        y_tambah = dy / steps;
                x=x0;
                y=y0;
                       
                g.fillRect(x, y, 1, 1);
        for (int k=10; k< steps ;k++){
            x += x_tambah;
            y += y_tambah;
          
                        g.fillRect(x, y, 1, 1);
        }            
       
    }
   
}


  • Lalu Kemudian membuat class ellips 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Tugas2;

import java.awt.Graphics;

/**
 *
 * @author JC
 */
public class Ellips {
    private int Rx2, Ry2, x, y, twoRx2, twoRy2, px, py, p;
    private int rx, ry;
    private int X, Y;
    Garis baru = new Garis();
   
   
    public void Ellipsku(Graphics g, int X, int Y, int rx, int ry){
        Rx2 =rx*rx;
        Ry2 =ry*ry;
        x =0;
        y =ry;
        twoRx2 =2*Rx2;
        twoRy2 =2*Ry2;
        px =0;
        py =twoRx2*y;
        int x1 = 0;
        int y1 = 0;
        p =(int) Math.round(Ry2-(Rx2*ry)+(0.25*Rx2));
//       
       
        while (px < py){
           
            x = x+1;
            px += twoRy2;
           
            if(p < 0){
//               
              
                p += Ry2 + px;
//              
               
            }else{
                y -= 1;
                py = py-twoRx2;
               
              
                   p += Ry2 + px - py;
               
//               
//               
            }
            baru.Garisku(g, Math.round(X+x), Math.round(Y+y),Math.round(X+x+1),Math.round(Y+y));
            proy4(g, X, Y, x,y);
            proy5(g, X, Y, x,y);
            proy8(g, X, Y, x,y);
           
               
           
           
           
        }
//       
        baru.Garisku(g, Math.round(X+x), Math.round(Y+y),Math.round(X+x+1),Math.round(Y+y));
       
//       
         p=(int) (ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry);
//       
      
        while( y > 0 ){
           
            y = y-1;
            py=py-twoRx2;
////           
           
            if (p < 0){
               
                x=x+1;
                px=px+2*ry*ry;
                p= p+Rx2+px-py;
               
            }else {
                  p=p+rx*rx-py;
               
//               
            }
           
           
            baru.Garisku(g, Math.round(X+x), Math.round(Y+y), Math.round(X+x+1), Math.round(Y+y));
            proy4(g, X, Y, x,y);
            proy5(g, X, Y, x,y);
            proy8(g, X, Y, x,y);
//          
           
        }
    }
    public void proy2(Graphics g, int X, int Y, int x, int y){
        int a;
        a=y;
        y=x;
        x=a;
        baru.Garisku(g, Math.round(x+X), Math.round(Y+y),Math.round(X+x+1), Math.round(Y+y));
    }
    public void proy3(Graphics g, int X, int Y, int x, int y){
        int a;
        a=y;
        y=-x;
        x=a;
        baru.Garisku(g, Math.round(x+X), Math.round(Y+y),Math.round(X+x+1), Math.round(Y+y));
    }
    public void proy4(Graphics g, int X, int Y, int x, int y){ 
        y=-y;
        x=x;
        baru.Garisku(g, Math.round(X+x), Math.round(Y+y),Math.round(X+x+1), Math.round(Y+y));
    }

    public void proy5(Graphics g, int X, int Y, int x, int y){
        x=-x;
        y=-y;
        baru.Garisku(g, Math.round(X+x), Math.round(Y+y),Math.round(X+x+1), Math.round(Y+y));
    }
    public void proy6(Graphics g, int X, int Y, int x, int y){
        int a;
        a=y;
        y=-x;
        x=-a;
        baru.Garisku(g, Math.round(X+x), Math.round(Y+y),Math.round(X+x+1), Math.round(Y+y));
    }

    public void proy7(Graphics g, int X, int Y, int x, int y){
        int a;
        a=y;
        y=x;
        x=-a;
        baru.Garisku(g, Math.round(X+x), Math.round(Y+y),Math.round(X+x+1), Math.round(Y+y));
    }

    public void proy8(Graphics g, int X, int Y, int x, int y){
        x=-x;
        y=y;
        baru.Garisku(g, Math.round(X+x), Math.round(Y+y),Math.round(X+x+1), Math.round(Y+y));
    }
   
}


  • Dan terakhir kita membuat class PanggilEllips untuk memanggil class Ellips dan Garis.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Tugas2;

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

/**
 *
 * @author JC
 */
public class PanggilEllips extends JPanel{

   
    @Override
    public void paintComponent(Graphics g){
       
        Ellips bru = new Ellips();
        Lingkaran br = new Lingkaran();
        g.setColor(Color.BLACK);
        bru.Ellipsku(g, 300, 150, 250, 100);
        br.Lingkaranku(g, 300, 150, 50);
       
       
    }
   
    public static void main(String[] args) {
        JFrame frame = new JFrame("Gambar Ellips");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new PanggilEllips());
        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
       
    }
}

1 komentar: