// FLASHMEDIUM / Developer Home / Code / JAVA
  > > JAVA - Utilizing the drawPolygon( ) method from the Graphics class to draw a city skyline within an applet.
 

Download Source Code:
Skyline.java (7k)

Launch Applet >>
Skyline Class
 
                
               /*
------------------------------------------------------------ Program: Skyline
               Purpose:  This applet will use the drawPolygon() method from
               the Graphics class to draw a city skyline within the
               applet. The applet will contain a button which will
               change the city's background to night or day when
               it is pressed. A counter will be used to make the 
               distinction of night and day by assigning all the even
               values to the day and all the odd values to the night.
               (fm)
               
               Date:                  4/28/02
               -----------------------------------------------------------
               */
                 
                 import java.applet.*;
                 import java.awt.*;
                 import java.awt.event.*;
                 public class Skyline extends Applet implements ActionListener
                 {
                    int xPoints[] = {0,80,80,120,120,140,140,160,160,180,180,
                    200,200,220,220,280,280,320,320,340,340,
                    380,380,400,400,420,420,460,460,480,480,540,
                    540,620,620,640,640,0,0};
                    int yPoints[]  = {240,240,340,340,400,400,240,220,360,360,
                    400,400,380,380,280,280,360,360,300,300,
                    260,280,380,380,400,400,140,140,340,340,
                    280,280,220,220,260,260,480,480,240};

                    int counter = 2; // day/night counter

                    Polygon city = new Polygon(xPoints, yPoints, xPoints.length);
                 
                    Color blueGray = new Color(33,66,99);
                   //Applet button component

                    Button daynite = new Button("Day/Night");

                   public void init()
                   {
                       add(daynite);
                       daynite.addActionListener(this);
                   }

                   public void paint(Graphics gr)
                   {
                      if (counter% 2 == 0) 
                 
                   {
 
                      setBackground(Color.white);    // Sets applet Background to black
                      gr.setColor(blueGray);             // Sets the city skyline stroke to blueGary
                     gr.drawPolygon(city);               //  Draws out the entire skyline using the
                    
                      // coordinates in the two arrays above.
                     gr.fillPolygon(city);
                 
                   
                     // The following drawLine() methods will give the skyline 
                     // graphic a 3-D prespective
                    gr.drawLine(0,220,100,220);
                    gr.drawLine(80,240,100,220);
                    gr.drawLine(100,220,100,320);
                    gr.drawLine(80,340,100,320);
                    gr.drawLine(100,320,140,320);
                    gr.drawLine(120,340,140,320);
                    gr.drawLine(120,400,140,380);
                   gr.drawLine(160,220,180,200);
                   gr.drawLine(180,200,180,340); 
                   gr.drawLine(180,340,160,360);
                   gr.drawLine(180,340,200,340);
                   gr.drawLine(200,340,180,360);
                   gr.drawLine(200,340,200,380);
                   gr.drawLine(180,400,220,360);
                   gr.drawLine(220,280,240,260);
                   gr.drawLine(240,260,300,260);
                   gr.drawLine(300,260,280,260);
                   gr.drawLine(300,260,300,340);
                   gr.drawLine(280,280,300,260);
                   gr.drawLine(300,340,280,360);
                   gr.drawLine(300,340,320,340);
                   gr.drawLine(320,300,340,280);
                   gr.drawLine(340,260,360,240);
                   gr.drawLine(360,240,400,260);
                   gr.drawLine(400,260,380,280);
                   gr.drawLine(400,260,380,280); 
                   gr.drawLine(400,260,400,360);
                   gr.drawLine(400,360,380,380);
                   gr.drawLine(400,380,420,360);
                   gr.drawLine(400,400,420,380);
                   gr.drawLine(400,360,420,360);
                 
                   gr.drawLine(420,140,440,120);
                   gr.drawLine(440,120,480,120);
                   gr.drawLine(480,120,480,280);
                   gr.drawLine(460,140,480,120);
                   gr.drawLine(460,340,480,320);
                 
                   gr.drawLine(480,280,500,260);
                   gr.drawLine(500,260,540,260);
                   gr.drawLine(540,220,560,200);
                   gr.drawLine(560,200,640,200);
                   gr.drawLine(640,200,620,220);
                   gr.drawLine(620,260,640,240);
                   gr.drawRect(560,240,40,40);
                   gr.drawLine(560,280,600,240);
                 }
                  else
                 
                 {
                    setBackground(Color.black);         // Sets applet Background to black
                    gr.setColor(blueGray);                  // Sets the city skyline stroke to blueGary
                    gr.drawPolygon(city);                   //  Draws out the entire skyline using the
                                                                      // coordinates in the two arrays above.
 
                 // The following drawLine() methods will give the skyline 
                 // graphic a 3-D perspective
                 gr.drawLine(0,220,100,220);
                 gr.drawLine(80,240,100,220);
                 gr.drawLine(100,220,100,320);
                 gr.drawLine(80,340,100,320);
                 gr.drawLine(100,320,140,320);
                 gr.drawLine(120,340,140,320);
                 gr.drawLine(120,400,140,380);
                  gr.drawLine(160,220,180,200);
                 gr.drawLine(180,200,180,340); 
                 gr.drawLine(180,340,160,360);
                 gr.drawLine(180,340,200,340);
                 gr.drawLine(200,340,180,360);
                 gr.drawLine(200,340,200,380);
                 gr.drawLine(180,400,220,360);
                 gr.drawLine(220,280,240,260);
                 gr.drawLine(240,260,300,260);
                 gr.drawLine(300,260,280,260);
                 gr.drawLine(300,260,300,340);
                 gr.drawLine(280,280,300,260);
                 gr.drawLine(300,340,280,360);
                 gr.drawLine(300,340,320,340);
                  gr.drawLine(320,300,340,280);
                 gr.drawLine(340,260,360,240);
                 gr.drawLine(360,240,400,260);
                 gr.drawLine(400,260,380,280);
                 gr.drawLine(400,260,380,280); 
                 gr.drawLine(400,260,400,360);
                 gr.drawLine(400,360,380,380);
                 gr.drawLine(400,380,420,360);
                 gr.drawLine(400,400,420,380);
                 gr.drawLine(400,360,420,360);
                 
                 gr.drawLine(420,140,440,120);
                 gr.drawLine(440,120,480,120);
                 gr.drawLine(480,120,480,280);
                 gr.drawLine(460,140,480,120);
                 gr.drawLine(460,340,480,320);
                 
                 gr.drawLine(480,280,500,260);
                 gr.drawLine(500,260,540,260);
                 gr.drawLine(540,220,560,200);
                 gr.drawLine(560,200,640,200);
                 gr.drawLine(640,200,620,220);
                 gr.drawLine(620,260,640,240);
                 gr.drawRect(560,240,40,40);
                 gr.drawLine(560,280,600,240);

      
                } //end if 
             }  //end paint
                  
             public void actionPerformed(ActionEvent e)
             {
                 ++ counter;
                 repaint();
             } 

          }  // end class               
       }//end class

 

HTML Code to run the applet above.
( * I placed an asterisk in html tags to avoid rendering )

 

<*HTML>
<*APPLET CODE ="Skyline.class" WIDTH = 640 HEIGHT = 480>
<*/APPLET>
<*/HTML>


 
- - -
 
  //cmts  
(fm) Home | Dev Home | Help | Site Map
Tutorials | Sound FX | Sound Loops | 3-D | Code | Images | Submit