KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > awt > Polygon

java.awt
Class Polygon

java.lang.Object
  extended by java.awt.Polygon
All Implemented Interfaces:
Shape, Serializable
See Also:
Top Examples, Source Code, WIND_EVEN_ODD

public void addPoint(int x,
                     int y)
See Also:
contains(java.awt.Point), getBounds()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected Rectangle bounds
See Also:
getBounds(), getBoundingBox()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean contains(double x,
                        double y)
See Also:
Shape
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean contains(double x,
                        double y,
                        double w,
                        double h)
See Also:
Shape.intersects(double, double, double, double), Area
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean contains(int x,
                        int y)
See Also:
contains(double, double)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean contains(Point2D p)
See Also:
contains(double, double), Shape
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean contains(Rectangle2D r)
See Also:
contains(double, double, double, double), Shape
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean contains(Point p)
See Also:
contains(double, double)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public Rectangle getBoundingBox()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Rectangle getBounds()
See Also:
Shape.getBounds2D()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Rectangle2D getBounds2D()
See Also:
Shape.getBounds()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public PathIterator getPathIterator(AffineTransform at)
See Also:
Shape
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public PathIterator getPathIterator(AffineTransform at,
                                    double flatness)
See Also:
Shape
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public boolean inside(int x,
                                 int y)
See Also:
contains(double, double)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean intersects(double x,
                          double y,
                          double w,
                          double h)
See Also:
Area, Shape
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean intersects(Rectangle2D r)
See Also:
Shape.intersects(double, double, double, double)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void invalidate()
See Also:
getBounds()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int npoints
See Also:
addPoint(int, int), ypoints, xpoints
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Polygon()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Polygon(int[] xpoints,
               int[] ypoints,
               int npoints)
See Also:
NullPointerException, IndexOutOfBoundsException, NegativeArraySizeException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void reset()
See Also:
invalidate()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void translate(int deltaX,
                      int deltaY)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1269]Drawing Applet
By vedprakashinfobase { dot } co { dot } in on 2005/01/21 01:01:13  Rate
//developed by Ved Prakash Sharma 
  
  
 import java.applet.Applet; 
 import java.awt.image.*; 
 import java.awt.event.*; 
 import java.security.*; 
 import javax.swing.*; 
 import java.awt.*; 
 import java.net.*; 
 import java.util.*; 
 import java.io.*; 
  
  
 public class ButtonApplet11 extends JPanel implements ActionListener,MouseMotionListener,MouseListener,Serializable 
  {  
       //button variable 
       private JPanel button_panel; 
       private JButton circle_btn,rect_btn,polygon_btn,move_btn,rotate_btn,image_btn; 
  
  
      //variable for polygon 
       Polygon polygon; 
       final int MAXLINE=100; 
       Point startx [  ] =new Point [ MAXLINE ] ; 
       Point ends [  ] =new Point [ MAXLINE ] ; 
       Point start; 
       Point anchor; 
       Point currentpoint; 
       int currentline=0; 
       boolean flagpoly=true,inside; 
       Point poly [  ] =new Point [ MAXLINE ] ; 
       int axisX [  ] =new int [ MAXLINE ] ,axisY [  ] =new int [ MAXLINE ] ; 
       int ind=0,flagpl=0; 
      //end polygon variable 
  
  
 //circle variable 
       Point sCrPoint = new Point ( -1, -1 ) ; 
       Point hwCrPoint = new Point ( -1, -1 ) ; 
       Vector points = new Vector (  ) ; 
  
  
 //rectangle variable 
       Rectangle rect; 
       final int NULL = 0; 
       final int MOVE = 1; 
       final int RESHAPE = 2; 
       final int MOVELINE = 3; 
       final int TOP = 1; 
       final int RIGHT = 10; 
       final int BOTTOM = 100; 
       final int LEFT = 1000; 
       int operation; 
       Point pointSource; 
       Point lineSourceUL; 
       Point lineSourceLR; 
       int lineSource; 
  
  
       public static String name; 
       public Image image,img; 
  
  
       /** Position in ball of mouse press to make dragging look better. */ 
       private int _dragFromX = 0; 
       private int _dragFromY = 0; 
  
  
       private boolean _canDrag1 = false, _canDrag2 = false, _canDrag3 = false, _canDrag4 = false; 
  
  
       private int flag=0,imgFlag=0; 
       private int moveFlag1=0,moveFlag2=0,moveFlag3=0,moveFlag4=0; 
       private int rotateFlag1=0,rotateFlag2=0,rotateFlag3=0,rotateFlag4=0; 
       private int resizeFlag1=0,resizeFlag2=0,resizeFlag3=0,resizeFlag4=0; 
  
  
       static String msg="",msg1=""; 
  
  
       static String command; 
  
  
       public int top=0,left=0,width=0,height=0; 
       public int pX=0,pY=0,rX=0,rY=0,prX=0,prY=0; 
       public int sX,startX,sY,startY,eX,endX,eY,endY; 
       public int ctop=0,cleft=0,cwidth=0,cheight=0; 
  
  
       public int firstX,firstY,lastX,lastY; 
  
  
  
       public int mouseX,mouseY,moveX,moveY,X,Y; 
  
  
       public void method ( MyBean mb )  
        {  
             this.name=mb.getName (  ) ; 
        }  
  
  
       ButtonApplet11 (  )  
        {  
             Dimension d = getSize (  ) ; 
             setSize ( d.width,d.height ) ; 
  
  
             setBackground ( Color.lightGray ) ; 
             setForeground ( Color.blue ) ; 
  
  
             circle_btn=new JButton ( "Circle" ) ; 
             add ( circle_btn ) ; 
             circle_btn.addActionListener ( this ) ; 
  
  
             polygon_btn=new JButton ( "Polygon" ) ; 
             add ( polygon_btn ) ; 
             polygon_btn.addActionListener ( this ) ; 
  
  
             rect_btn=new JButton ( "Rectangle" ) ; 
             add ( rect_btn ) ; 
             rect_btn.addActionListener ( this ) ; 
  
  
             move_btn=new JButton ( "Move" ) ; 
             add ( move_btn ) ; 
             move_btn.addActionListener ( this ) ; 
  
  
             rotate_btn=new JButton ( "Rotate" ) ; 
             add ( rotate_btn ) ; 
             rotate_btn.addActionListener ( this ) ; 
  
  
             image_btn=new JButton ( "Image" ) ; 
             add ( image_btn ) ; 
             image_btn.addActionListener ( this ) ; 
  
  
             addMouseMotionListener ( this ) ; 
             addMouseListener ( this ) ; 
  
  
        }  
       public void paintComponent ( Graphics g )  
        {  
             super.paintComponent ( g ) ; // Required for background. 
             g.drawString ( "Mouse at  ( "+X+","+Y+" )   : "+msg,1,45 ) ; 
             if ( flag==1 )  
              {  
                   rect.display ( g ) ; 
              }  
             if ( flag==2 )  
              {  
                   if ( moveFlag2==1 )  
                    {  
                         g.drawArc ( ctop,cleft,cwidth,cheight,0,360 ) ; 
                         flag=2; 
                    }  
                   else 
                    {  
                         g.drawArc ( ctop,cleft,cwidth,cheight,0,360 ) ; 
                    }  
              }  
             if ( flag==3 && flagpoly )  
              {  
                   for ( int i=0;i < currentline;i++ )  
                    {  
                         g.drawLine ( startx [ i ] .x,startx [ i ] .y,ends [ i ] .x,ends [ i ] .y ) ; 
                    }  
                   if ( currentpoint!=null )  
                    {  
                          g.drawLine ( anchor.x,anchor.y,currentpoint.x,currentpoint.y ) ; 
                    }  
              }  
             if ( flagpl==1 && !flagpoly )  
              {  
                   g.drawPolygon ( polygon ) ; 
              }  
        } //end paintComponent 
  
  
  
       public void actionPerformed ( ActionEvent ae )  
        {  
             command = ae.getActionCommand (  ) ; 
             System.out.print ( "Command:-"+command ) ; 
  
  
             if ( command.equals ( "Rectangle" )  )  
              {  
                   setCursor ( Cursor.getPredefinedCursor ( Cursor.DEFAULT_CURSOR )  ) ; 
                   flag=1; 
                   msg="Click any where for draw Rectangle"; 
                   if ( imgFlag==1 )  
                         rect = new Rectangle ( new Point ( 20, 200 ) , new Point ( 200, 100 ) ,image ) ; 
                   else 
                         rect = new Rectangle ( new Point ( 20, 200 ) , new Point ( 200, 100 ) ,img ) ; 
              }  
             if ( command.equals ( "Circle" )  )  
              {  
                   setCursor ( Cursor.getPredefinedCursor ( Cursor.DEFAULT_CURSOR )  ) ; 
                   msg="Click any where for draw Circle"; 
                   flag=2; 
                   moveFlag2=0; 
                   moveFlag1=0; 
                   moveFlag2=0; 
                   moveFlag3=0; 
                   moveFlag4=0; 
                   rotateFlag1=0; 
                   rotateFlag2=0; 
                   rotateFlag3=0; 
                   rotateFlag4=0; 
              }  
  
  
             if ( command.equals ( "Polygon" )  )  
              {  
                   setCursor ( Cursor.getPredefinedCursor ( Cursor.DEFAULT_CURSOR )  ) ; 
                   msg="Click any where for draw Polygon after double click polygon complited"; 
                   flag=3; 
                   flagpl=0; 
                   ind=0; 
                   start=null; 
                   anchor=null; 
                   currentpoint=null; 
                   currentline=0; 
                   flagpoly=true; 
                   inside=false; 
  
  
                   moveFlag1=0; 
                   moveFlag2=0; 
                   moveFlag3=0; 
                   moveFlag4=0; 
                   rotateFlag1=0; 
                   rotateFlag2=0; 
                   rotateFlag3=0; 
                   rotateFlag4=0; 
              }  
             if ( command.equals ( "Move" )  )  
              {  
                   setCursor ( Cursor.getPredefinedCursor ( Cursor.HAND_CURSOR )  ) ; 
                   if ( flag==1 )  
                         moveFlag1=1; 
                   if ( flag==2 )  
                         moveFlag2=1; 
                   if ( flag==3 )  
                         moveFlag3=1; 
  
  
                   msg="Move the Shape"; 
                   rotateFlag1=0; 
                   rotateFlag2=0; 
                   rotateFlag3=0; 
                   rotateFlag4=0; 
              }  
             if ( command.equals ( "Rotate" )  )  
              {  
                   if ( flag==1 )  
                    {  
                         rect.rotate (  ) ; 
                         repaint (  ) ; 
                         rotateFlag1=1; 
                    }  
                   if ( flag==2 )  
                         rotateFlag2=1; 
                   if ( flag==3 )  
                         rotateFlag3=1; 
  
  
                   msg="Rotate the Shape"; 
                   moveFlag1=0; 
                   moveFlag2=0; 
                   moveFlag3=0; 
                   moveFlag4=0; 
              }  
             if ( command.equals ( "Image" )  )  
              {  
                   imgFlag=1; 
  
  
                   msg="Image UpLoad"; 
                   moveFlag1=0; 
                   moveFlag2=0; 
                   moveFlag3=0; 
                   moveFlag4=0; 
                   rotateFlag1=0; 
                   rotateFlag2=0; 
                   rotateFlag3=0; 
                   rotateFlag4=0; 
             }  
        }  
       public void mouseDragged ( MouseEvent e )  
        {  
             e.consume (  ) ; 
              if ( flag==3 && flagpoly && currentline < MAXLINE )  
               {  
                   currentpoint=new Point ( e.getX (  ) ,e.getY (  )  ) ; 
                   this.repaint (  ) ; 
               }  
              if ( inside )  
               {  
                     polygon.translate ( e.getX (  ) , e.getY (  )  ) ; 
               }  
  
  
             if  ( _canDrag2 )  
              {  
                   ctop = e.getX (  )  - _dragFromX; 
                   cleft = e.getY (  )  - _dragFromY; 
  
  
                   ctop = Math.max ( ctop, 0 ) ; 
                   ctop = Math.min ( ctop, getWidth (  )  - cwidth ) ; 
  
  
                   cleft = Math.max ( cleft, 0 ) ; 
                   cleft = Math.min ( cleft, getHeight (  )  - cheight ) ; 
  
  
                   this.repaint (  ) ; 
              }  
             if (  ( flag==2  )  &&  ( moveFlag2==0 )  )  
              {  
                   e.consume (  ) ; 
                   hwCrPoint.x = e.getX (  ) ; 
                   hwCrPoint.y = e.getY (  ) ; 
                   drawCircle (  ) ; 
              }  
             if ( flag==1 )  
              {  
                   Point point = e.getPoint (  ) ; 
                   switch ( operation )  
                    {  
                         default: 
                               break; 
  
  
                         case 1: // '\001' 
                               setCursor ( Cursor.getPredefinedCursor ( Cursor.HAND_CURSOR  )  ) ; 
                               rect.move ( point.x - pointSource.x, point.y - pointSource.y ) ; 
                               pointSource = point; 
                               break; 
  
  
                         case 3: // '\003' 
                               switch ( lineSource )  
                                {  
                                     case 1: // '\001' 
                                           setCursor ( Cursor.getPredefinedCursor ( Cursor.N_RESIZE_CURSOR  )  ) ; 
                                           Point point1 = new Point ( lineSourceUL.x, point.y ) ; 
                                           rect.reshape ( lineSourceUL, point1 ) ; 
                                           lineSourceUL = point1; 
                                           break; 
  
  
                                     case 10: // '\n' 
                                           setCursor ( Cursor.getPredefinedCursor ( Cursor.E_RESIZE_CURSOR   )  ) ; 
                                           Point point2 = new Point ( point.x, lineSourceLR.y ) ; 
                                           rect.reshape ( lineSourceLR, point2 ) ; 
                                           lineSourceLR = point2; 
                                           break; 
  
  
                                     case 100: // 'd' 
                                           setCursor ( Cursor.getPredefinedCursor ( Cursor.N_RESIZE_CURSOR  )  ) ; 
                                           Point point3 = new Point ( lineSourceLR.x, point.y ) ; 
                                           rect.reshape ( lineSourceLR, point3 ) ; 
                                           lineSourceLR = point3; 
                                           break; 
  
  
                                     case 1000: 
                                           setCursor ( Cursor.getPredefinedCursor ( Cursor.E_RESIZE_CURSOR   )  ) ; 
                                           Point point4 = new Point ( point.x, lineSourceUL.y ) ; 
                                           rect.reshape ( lineSourceUL, point4 ) ; 
                                           lineSourceUL = point4; 
                                           break; 
                                }  
                               break; 
  
  
                         case 2: // '\002' 
                               rect.reshape ( pointSource, point ) ; 
                               pointSource = point; 
                               break; 
                    }  
                   this.repaint (  ) ; 
              }  
        }  
  
  
       public void mouseMoved ( MouseEvent me )  
        {  
             X=me.getX (  ) ; 
             Y=me.getY (  ) ; 
             this.repaint (  ) ; 
        }  
       public void mouseClicked ( MouseEvent e )  
        {  
             if ( flagpl==1 )  
              {  
                   if  ( polygon.contains ( e.getX (  ) , e.getY (  )  )  )  
                    {  
                          setCursor ( Cursor.getPredefinedCursor ( Cursor.HAND_CURSOR  )  ) ; 
                          inside = true; 
                          repaint (  ) ; 
                    }  
              }  
  
  
        }  
       public void mouseEntered ( MouseEvent me )  
        {  
        }  
       public void mousePressed ( MouseEvent e )  
        {  
             e.consume (  ) ; 
             moveX=e.getX (  ) ; 
             moveY=e.getY (  ) ; 
             prX=moveX; 
             prY=moveY; 
             if ( moveFlag2==1 )  
              {  
                   int x = e.getX (  ) ; 
                   int y = e.getY (  ) ; 
  
  
                   if  ( x  > = ctop && x  < =  ( ctop + cwidth )  && y  > = cleft && y  < =  ( cleft + cheight )  )  
                    {  
                         _canDrag2 = true; 
                         _dragFromX = x - ctop; 
                         _dragFromY = y - cleft; 
                    }  
                   else 
                    {  
                         _canDrag2 = false; 
                    }  
              }  
  
  
             if (  ( flag==2 )  &&  ( moveFlag2==0 )  )  
              {  
                   e.consume (  ) ; 
                   sCrPoint.x = e.getX (  ) ; 
                   sCrPoint.y = e.getY (  ) ; 
              }  
             if ( flag==1 )  
              {  
                   Point point = e.getPoint (  ) ; 
                   pointSource = nearCorner ( point ) ; 
                   if ( pointSource != null )  
                    {  
                         operation = 2; 
                         return; 
                    }  
                   lineSource = nearLine ( point ) ; 
                   if ( lineSource != 0 )  
                    {  
                         lineSourceUL = rect.getUL (  ) ; 
                         lineSourceLR = rect.getLR (  ) ; 
                         operation = 3; 
                         return; 
                    }  
                   Point point1 = rect.getUL (  ) ; 
                   Point point2 = rect.getLR (  ) ; 
                   if ( point.x  >  point1.x && point.x  <  point2.x && point.y  >  point1.y && point.y  <  point2.y )  
                    {  
                         operation = 1; 
                         pointSource = point; 
                    }  
              }  
             if ( flag==3 && flagpoly && currentline < MAXLINE )  
              {  
                   if ( currentline==0 )  
                    {  
                             anchor=new Point ( e.getX (  ) ,e.getY (  )  ) ; 
                             start=new Point ( e.getX (  ) ,e.getY (  )  ) ; 
                    }  
              }  
  
  
  
        }  
       public void mouseReleased ( MouseEvent e )  
        {  
             operation = 0; 
             if (  ( flag==2 && moveFlag2==0 )   )  
              {  
                   e.consume (  ) ; 
                   hwCrPoint.x = e.getX (  ) ; 
                   hwCrPoint.y = e.getY (  ) ; 
                   drawCircle (  ) ; 
              }  
             if ( flagpoly && flag==3 && currentline < MAXLINE )  
              {  
                  addline ( e.getX (  ) ,e.getY (  )  ) ; 
                  anchor=new Point ( e.getX (  ) ,e.getY (  )  ) ; 
                  if ( e.getClickCount (  )  == 2 )  
                   {  
                         addline ( start.x,start.y ) ; 
                         for ( int i=0;i < ind;i++ )  
                          {  
                               axisX [ i ] =poly [ i ] .x; 
                               axisY [ i ] =poly [ i ] .y; 
                               flagpl=1; 
                          }  
                         flagpoly=false; 
                         if ( flagpl==1 )  
                          {  
                               System.out.print ( " "+ind ) ; 
                               polygon = new Polygon ( axisX, axisY, ind ) ; 
                          }  
  
  
                   }  
              }  
        }  
       public void mouseExited ( MouseEvent me )  
        {  
             if ( moveFlag1==1 )  
                   _canDrag1 = false; 
             if ( moveFlag2==1 )  
                   _canDrag2 = false; 
             if ( moveFlag3==1 )  
                   _canDrag3= false; 
        }  
      public void addline ( int x,int y )  
        {  
             startx [ currentline ] =anchor; 
             ends [ currentline ] =new Point ( x,y ) ; 
  
  
 // System.out.print ( " "+ind ) ; 
  
  
             poly [ ind++ ] =startx [ currentline ] ; 
             poly [ ind++ ] =ends [ currentline ] ; 
  
  
             currentline++; 
             currentpoint=null; 
             anchor=null; 
             repaint (  ) ; 
        }  
  
  
       public void setCircleBoundry ( int top_int,int left_int,int width_int,int height_int )  
        {  
  
  
             this.ctop=top_int; 
             this.cleft=left_int; 
             this.cwidth=width_int; 
             this.cheight=width_int; 
             this.repaint (  ) ; 
        }  
  
  
       public void drawCircle (  )  
        {  
             if ( moveFlag2==0 )  
              {  
                   if  (  ( hwCrPoint.x  <  sCrPoint.x )  &  ( hwCrPoint.y  <  sCrPoint.y )  )  
                         setCircleBoundry ( hwCrPoint.x, hwCrPoint.y, sCrPoint.x- hwCrPoint.x, sCrPoint.y - hwCrPoint.y ) ; 
                   else if  ( hwCrPoint.x  <  sCrPoint.x )  
                         setCircleBoundry ( hwCrPoint.x, sCrPoint.y, sCrPoint.x-hwCrPoint.x, hwCrPoint.y-sCrPoint.y ) ; 
                   else if  ( hwCrPoint.y  <  sCrPoint.y )  
                         setCircleBoundry ( sCrPoint.x, hwCrPoint.y, hwCrPoint.x-sCrPoint.x, sCrPoint.y-hwCrPoint.y ) ; 
                   else 
                         setCircleBoundry ( sCrPoint.x, sCrPoint.y, hwCrPoint.x-sCrPoint.x, hwCrPoint.y-sCrPoint.y ) ; 
              }  
        }  
       private int nearLine ( Point point )  
        {  
             Point point1 = rect.getUL (  ) ; 
             Point point2 = rect.getLR (  ) ; 
             int i = 0; 
             if ( Math.abs ( point1.x - point.x )   <  10 )  
                   i += 1000; 
             if ( Math.abs ( point1.y - point.y )   <  10 )  
                   i++; 
             if ( Math.abs ( point2.y - point.y )   <  10 )  
                   i += 100; 
             if ( Math.abs ( point2.x - point.x )   <  10 )  
                   i += 10; 
             return i; 
        }  
  
  
       private Point nearCorner ( Point point )  
        {  
             int i = nearLine ( point ) ; 
             switch ( i )  
              {  
                   case 1001: 
                         return rect.getUL (  ) ; 
  
  
                   case 11: // '\013' 
                         return rect.getUR (  ) ; 
  
  
                   case 1100: 
                         return rect.getLL (  ) ; 
  
  
                   case 110: // 'n' 
                         return rect.getLR (  ) ; 
              }  
             return null; 
        }  
  } 


public int[] xpoints
See Also:
addPoint(int, int), npoints
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int[] ypoints
See Also:
addPoint(int, int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags