Search Here

Java program for creating a night view


/* Creating a night view using applet */

import java.awt.*;
import java.applet.*;

/*
 <applet code=night_view height=200 width=300>
 </applet>
*/

public class night_view extends Applet
{  
 // Setting the background color to black
 public void init()
 {
  this.setBackground(Color.BLACK);
 }
 
 public void paint(Graphics g)
 {  
  int appletWidth = 300;
  int appletHeight = 200;
  
  for(int i=0; i<1000; i++)
  {
   int x = (int)(Math.random() * appletWidth);
   int y = (int)(Math.random() * appletHeight);
   
   if(i<1000 )     
    g.setColor(Color.WHITE);
    g.fillOval(x,y,1,1);
  }  
  
  //Drawing the Moon 
  g.setColor(Color.WHITE);
  g.fillOval(50,50,30,30);
  g.setColor(Color.BLACK);
  g.fillOval(45,45,30,30);
  
  //Drawing the ground   
  int column = 0;
  int horizonHeight= 50;
  g.setColor(Color.GREEN);
  while(column < appletWidth) 
  {
   if(column < appletWidth)    
    g.drawRect(column,(appletHeight-horizonHeight),1,appletHeight);
          
   column++;
   horizonHeight = horizonHeight + (int)(Math.random()*3-1.5);
  }  
 }
}

No comments:

Post a Comment