Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 148,821 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,118 people online right now. 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: 11,860

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.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month