Senin, 09 Juli 2012

Program Bikin Java Lingkaran Animasi

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


import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
 *
 * @author Rizky Hw
 */
public class Lingkaran extends Frame {

    public static void main(String[] args){new Lingkaran();
    }

   Lingkaran()
   {  super("Gambar Lingkarans");
      addWindowListener(new WindowAdapter()
       {public void windowClosing(WindowEvent e){System.exit(0);}});
      setSize(600, 400);
      add("Center", new Cvlingkaran());
      show();
   }
}

// Bikin Lingkaran
class Cvlingkaran extends Canvas
{  float rWidth = 10.0F, rHeight = 7.5F, pixelSize;
   int centerX, centerY, dGrid = 10, maxX, maxY;

   void initgr()
   {  Dimension d;
      d = getSize();
      maxX = d.width - 1;
      maxY = d.height - 1;
      pixelSize = Math.max(rWidth/maxX, rHeight/maxY);
      centerX = maxX/2; centerY = maxY/2;
   }

   void putPixel(Graphics g, int x, int y)
   {  int x1 = x * dGrid, y1 = y * dGrid, h = dGrid/2;
      g.drawString(".",x , y);
   }

  
  
 
  // Void Lingkaran
 void lingkaran(Graphics g, int xC, int yC, int r, String pos)
   {  int x = 0, y = r, u = 1, v = 2 * r - 1, E = 0;
      while (x < y){ 
        if(!pos.equals("top") || pos.equals("all")){
            putPixel(g, xC + x, yC + y); // NNE
        }
        if(pos.equals("all")){
            putPixel(g, xC + y, yC - x); // ESE
        }
        if(pos.equals("top") || pos.equals("all")){
            putPixel(g, xC - x, yC - y); // SSW
        }
        if(pos.equals("all")){
            putPixel(g, xC - y, yC + x); // WNW
        }
         x++; E += u; u += 2;
         if (v < 2 * E){y--; E -= v; v -= 2;}
         if (x > y) break;
        if(pos.equals("all")){
            putPixel(g, xC + y, yC + x); // ENE
        }
        if(pos.equals("top") || pos.equals("all")){
            putPixel(g, xC + x, yC - y); // SSE
        }
        if(pos.equals("all")){
            putPixel(g, xC - y, yC - x); // WSW
        }
        if(!pos.equals("top") || pos.equals("all")){
            putPixel(g, xC - x, yC + y); // NNW
        }
       
            try{
              Thread.sleep(50);  
            }catch (InterruptedException ie){
              System.out.println(ie.getMessage());
            }
      }
   }
  
  
  /* void showGrid(Graphics g)
   {  for (int x=dGrid; x<=maxX; x+=dGrid)
         for (int y=dGrid; y<=maxY; y+=dGrid)
            g.drawLine(x, y, x, y);
   }*/

  
   //Void Inputan titik point
   public void paint(Graphics g)
   {
       initgr();
      g.setColor(Color.black);
      lingkaran(g, 300, 170,80,"all");          

   }

}

Tidak ada komentar:

Posting Komentar