Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 107,400 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,162 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Drawing a shape

Shows you how to draw a square. Gives you basic idea of drawing a shape for an applet.

Submitted By: BlackJesus
Actions:
Rating:
Views: 9,164

Language: Java

Last Modified: February 16, 2005

Snippet


  1. import javax.swing.*;               //Import these
  2. import java.awt.*;
  3.  
  4. public class DrawShapes extends JApplet  //Public class make sure it extends JApplet
  5. {
  6.      int xPoints[] = {10,40,40,10,10}//Sets x coordinates to draw to/from
  7.      int yPoints[] = {10,10,40,40,10}//Sets y coordinates to draw to/from
  8.      
  9.      public void init()  //Initialize
  10.      {
  11.           setBackground(Color.black)//Make window background black
  12.           setSize(500,500);            //Set window size
  13.          
  14.      }
  15.      
  16.      public void paint(Graphics Obj)  //paint function (essential for graphics)
  17.      {
  18.           Obj.setColor(Color.green)//Sets the color to green
  19.           Obj.fillPolygon(xPoints,yPoints,xPoints.length)//Uses the x,y coordinates declared earlier to draw a filled square
  20.           for(int i = 0; i < xPoints.length;i++)  //for loop to add to x values to move it across screen
  21.                xPoints[i] += 80;
  22.           Obj.drawPolyline(xPoints,yPoints,xPoints.length); //draws an empty square at the new x values and same y values
  23.      }
  24. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month