Forum Stats

  • 3,840,127 Users
  • 2,262,570 Discussions
  • 7,901,154 Comments

Discussions

Problem with Drawing polygon

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Oct 4, 2015 12:25PM in New To Java

Hi,

I am trying to draw a polygon but its not connecting properly:

import java.applet.Applet;

import java.awt.*;

import javax.swing.*;

public class polygon extends Applet{

    public  void paint(Graphics g) {

              example(g);

            

    }

public void example(Graphics g) {

                 int[ ] x = new int[]{100,200,300,400};

                 int[ ] y = new int[]{400,300,300,100};

    g.drawPolygon (x, y, x.length);   

}

polygon.png

Some body please guide me.

Zulfi.

Best Answer

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 4, 2015 4:46AM Answer ✓

    Hi,

    I am able to solve this problem by changing the code:

    int[ ] x = new int[]{100,200,300,400};

    int[ ] y = new int[]{400,300,300,100};

    to

    int[ ] x = new int[]{100,200,300,100};

    int[ ] y = new int[]{400,300,300,400};

    Zulfi.

Answers

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 4, 2015 4:46AM Answer ✓

    Hi,

    I am able to solve this problem by changing the code:

    int[ ] x = new int[]{100,200,300,400};

    int[ ] y = new int[]{400,300,300,100};

    to

    int[ ] x = new int[]{100,200,300,100};

    int[ ] y = new int[]{400,300,300,400};

    Zulfi.

  • Unknown
    edited Oct 4, 2015 12:25PM
    I am trying to draw a polygon but its not connecting properly:

    Yes - by DEFINITION it IS 'connecting properly'.

    The drawPolygon does nothing more than 'connect the dots'. So if you want a polygon you need to provide 'dots' that will connect properly. See the Java API

    Graphics (Java Platform SE 7 )

    public abstract void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)

    Draws a closed polygon defined by arrays of x and y coordinates. Each pair of (x, y) coordinates defines a point.

    This method draws the polygon defined by nPoint line segments, where the first nPoint - 1 line segments are line segments from (xPoints[i - 1], yPoints[i - 1]) to (xPoints[i], yPoints[i]), for 1 ≤ inPoints. The figure is automatically closed by drawing a line connecting the final point to the first point, if those points are different.

    Make sure you read that LAST sentence.

    public void example(Graphics g) {
                     int[ ] x = new int[]{100,200,300,400};
                     int[ ] y = new int[]{400,300,300,100};
        g.drawPolygon (x, y, x.length);   
    }
    
    

    That fourth point is on the diagonal that connects points 1 and 2. If you plot them on a spreadsheet you can see that.

    This was your 'solution'

    int[ ] x = new int[]{100,200,300,100};
    int[ ] y = new int[]{400,300,300,400};
    
    

    Although that will work that fourth, and last, point just repeats the first point and is not needed.,

    The BETTER solution is to just remove that last point completely. REREAD the last sentence in the doc quote above. The method will AUTOMATICALLY connect the last point to the first point.

    The reason I say 'better' is because the drawPolygon methods are written so that you EXPLICITLY provide the number of points to use. There is a reason for that - it allows you to use ANY number of consecutive array elements you desire even if the array has more elements.

    Try passing a length of THREE to your original code and you will see it gives you the SAME result as YOUR solution.

    I suggest you read, and try, the many examples in The Java Tutorials trails - starting with 'Getting Started with Graphics'

    https://docs.oracle.com/javase/tutorial/2d/basic2d/

    There are MANY examples that show how to draw and fill.

This discussion has been closed.