Senin, 09 Juli 2012

Program Java UTS Bikin Gambar Gelas Animasi Delay

/*
 * 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 Gelas extends Frame {

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

   Gelas()
   {  super("Gambar Gelass");
      addWindowListener(new WindowAdapter()
       {public void windowClosing(WindowEvent e){System.exit(0);}});
      setSize(290, 390);
      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;
   }

   //int iX(float x){return Math.round(centerX + x/pixelSize);}
   //int iY(float y){return Math.round(centerY - y/pixelSize);}

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

  
   // Bikin Garis
   void garis(Graphics g, int xP, int yP, int xQ, int yQ)
   {  int x = xP, y = yP, D = 0, HX = xQ - xP, HY = yQ - yP, c, M, xInc = 1, yInc = 1;
      if (HX < 0){
          xInc = -1; HX = -HX;}
      if (HY < 0){
          yInc = -1; HY = -HY;}
      if (HY <= HX)
      {  c = 2 * HX;
         M = 2 * HY;
         for (;;)
         {  putPixel(g, x, y);
            if (x == xQ) break;
            x += xInc;
            D += M;
            if (D > HX){y += yInc; D= c;}
            try{
              Thread.sleep(10);  
            }catch (InterruptedException ie){
              System.out.println(ie.getMessage());
            }
         }
      }
      else
      {  c = 2 * HY; M = 2 * HX;
         for (;;)
         {  putPixel(g, x, y);
            if (y == yQ) break;
            y += yInc;
            D += M;
            if (D > HY){x += xInc; D -= c;}
            try{
              Thread.sleep(10);  
            }catch (InterruptedException ie){
              System.out.println(ie.getMessage());
            }
         }
      }
   }
 
  // 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(10);  
            }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();
     // showGrid(g);
      g.setColor(Color.black);
      
    //elips
       lingkaran(g, 135, 120,78,"top");     
       lingkaran(g, 135, 13,78,"bottom");
       lingkaran(g, 135, 160,50,"bottom");
      
   
    // garis gelas 
    garis(g, 170, 195, 190,65);
        garis(g, 100, 195, 80,65);
   
    //kotakan  
    garis(g, 65, 140, 20,250);
          garis(g, 245, 140, 210,250);
          garis(g, 245, 140, 179,140);
    garis(g, 65, 140, 90,140);
    garis(g, 20, 250, 210, 250);
      
       garis(g, 20, 250, 20, 290 );
       garis(g, 20, 290, 210 ,290);
       garis(g, 210, 250, 210,290);
      
       garis(g, 245, 140, 245, 170);
       garis(g, 210, 290, 245,170);
      
    //lingkaran
      lingkaran(g, 135, 140,32,"all");     
      g.setColor(Color.red);
        
      g.drawString("Gelass", 115, 143 );
    

   }

}

Tidak ada komentar:

Posting Komentar