Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 07-08-2009
Member
 
Join Date: Aug 2009
Posts: 2
drawPolygon in java Swing?

Hi,

How to use drawPolygon in java Swing?
Please help me with an example.


Regards,
Reply With Quote
  #2  
Old 07-08-2009
Member
 
Join Date: May 2008
Posts: 35
Re: drawPolygon in java Swing?

Drawing Polygons

One approach is to use multiple calls to drawLine() to draw a sequence of straight line segments that outlines the desired shape.

But a faster approach is instead to use a single call to drawPolyline(). This will speed up the drawing since the process needs only one invocation of the method, rather than one for each line segment.

Code:
import javax.swing.*;
import java.awt.*;

/** Example demonstrating drawPolyline().*/
public class Polygon1Applet extends JApplet
{
  public void init ()  {
    Container content_pane = getContentPane ();

    int width = getSize ().width;
    int height= getSize ().height;

    int num_points = 21;

    // Create an instance of DrawingPanel
    Polygon1Panel polygon1_panel =
        new Polygon1Panel (width,height,num_points);

    // Add the DrawingPanel to the contentPane.
    content_pane.add (polygon1_panel);

  }
}

/** Draw a polygon with drawPolyline() on
  * this JPanel subclass. **/
class Polygon1Panel extends JPanel
{
  int fWidth,fHeight;
  int fNumPoints;
  double fFactor;

  Polygon1Panel (int width, int height, int nPoints) {
    fNumPoints = nPoints;
    fWidth = width;
    fHeight= height;
    fFactor = 2.0 * Math.PI / fWidth;
  } // ctor

  public void paintComponent (Graphics g) {
    // First paint background unless you will
    // paint whole area yourself.
    super.paintComponent (g);

    // Create arrays of points for each
    // segment of the polygon
    int [] x = new int[fNumPoints];
    int [] y = new int[fNumPoints];

    // Select horizontal step size
    double x_del=  ((double)fWidth)/ (fNumPoints-1);

    // Find coordinates of the display center
    int x_offset = fWidth/2;
    int y_offset = fHeight/2;

    // Choose amplitude for the sine curve
    int amp =  (int) (y_offset * 0.9);

    // Create a sine curve from a sequence
    // of short line segments
    for  (int i=0; i < fNumPoints; i++) {
      x[i] =  (int) (i * x_del);
      y[i] =  (int) (amp * Math.sin (fFactor * x[i]) )
              + y_offset;
    }

    // Set the line color to red
    g.setColor (Color.red);

    // Draw curve with single call to drawPolyline
    g.drawPolyline (x,y,fNumPoints);

    // Change the line color and draw the x-y axes
    g.setColor (Color.green);
    g.drawLine (0,y_offset,fWidth-1,y_offset);
    g.drawLine (x_offset,0,x_offset,fHeight-1);

  } // paintComponent

} // class Polygon1Panel


The method

drawPolygon(int[] x, int[] y, int nPoints)

will work similarly, except it will connect the last point to the first if they are not the identical.

An interesting overriden version of this method

drawPolygon(Polygon poly)

uses an instance of the Polygon class to hold the points.
Reply With Quote
  #3  
Old 07-08-2009
Member
 
Join Date: May 2008
Posts: 39
Re: drawPolygon in java Swing?

Graphics: drawPolygon(int[] xPoints, int[] yPoints, int nPoints)

Code:
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JPanel {

  public void paint(Graphics g) {
    int xpoints[] = {25, 145, 25, 145, 25};
    int ypoints[] = {25, 25, 145, 145, 25};
    int npoints = 5;
    
    g.drawPolygon(xpoints, ypoints, npoints);
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setVisible(true);
  }
}
Reply With Quote
  #4  
Old 07-08-2009
Member
 
Join Date: May 2008
Posts: 23
Re: drawPolygon in java Swing?

You can also download this sample program.

Draw Polygon for Java

I hope this helps.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "drawPolygon in java Swing?"
Thread Thread Starter Forum Replies Last Post
Applets V/s Swing in JAVA "Dritan" Software Development 3 14-12-2010 09:04 AM
Print in java using Swing Sheenas Software Development 3 18-11-2009 09:50 AM
Java Swing and MySQL shelton141 Software Development 1 22-09-2009 08:50 AM
Message Box example for Java Swing Visala28 Software Development 3 31-07-2009 05:30 PM
Java swing help with repaint() grudge Software Development 4 23-07-2009 03:41 PM


All times are GMT +5.5. The time now is 07:23 PM.