Results 1 to 9 of 9
  1. #1
    lost_soul is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default How to use Polygon class?

    Could someone show me how to use a Polygon class to draw shapes or try to walk me through it. How can I set it up to draw these coordinates and fill them in?

    shape1 with:
    xcoordinates: {139, 170, 203, 203}
    ycoordinates: {139, 122, 122, 139}

    shape2 with:
    xcoordinates: {210, 245, 245, 210}
    ycoordinates: {122, 122, 139, 139}


    Java Code:
    import javax.swing.JPanel;
    import java.awt.*;
    import java.lang.Object; 
    
    
    public class Shapes extends JPanel
    {
      Polygon ()
    
    Polygon (int [] xpoints, int[] ypoints, int npoints)
    
    Polygon shape = new Polygon
    shape.addPoint(int[] xpoints, int[] ypoints);
    
    Polygon shape2 = new Polygon
    shape2.addPoint(int[] xpoints, int[] ypoints);
    
    
    
    }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Seriously, google is your friend when you encounter a class that you don't fully understand. Either find the API, or google an example. To make your job a bit easier, here's the API for Polygon class Polygon API.

  3. #3
    lost_soul is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    Thank you for the link.
    Ok, I tried to set it up like this.
    What are these errors telling me?

    Java Code:
    import javax.swing.JPanel;
    import java.awt.*;
    
    
    
    public class shape extends JPanel
      {
    
      int[]xShape = new int[4];
      int[]yShape = new int[4];
      int nShape;  // count of points
       
      // Make a shape
      xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;
      yShape[0]=139; yShape[1]=122; yShape[2]=122; yShape[3]=139; 
      nShape = 4;
      
      Polygon Shape = new Polygon(xShape, yShape, nShape);  // a shape
      }
    I get these errors:

    hape.java:19: ']' expected
    xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;
    ^
    Shape.java:19: ']' expected
    xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;
    ^
    Shape.java:19: ']' expected
    xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;
    ^
    Shape.java:19: ']' expected
    xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;
    ^
    Shape.java:20: ']' expected
    yShape[0]=80; yShape[1]=80; yShape[2]=130; yShape[3]=170;
    ^
    Shape.java:20: ']' expected
    yShape[0]=80; yShape[1]=80; yShape[2]=130; yShape[3]=170;
    ^
    Shape.java:20: ']' expected
    yShape[0]=80; yShape[1]=80; yShape[2]=130; yShape[3]=170;
    ^
    Shape.java:20: ']' expected
    yShape[0]=80; yShape[1]=80; yShape[2]=130; yShape[3]=170;
    ^
    shape.java:21: <identifier> expected
    nShape = 18;
    ^
    Last edited by lost_soul; 04-24-2010 at 10:46 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't have a compiler present, but it looks like you're trying to make a whole bunch of method calls outside of a method/constructor/etc... You can't just place these calls out naked in the class.

    i.e, This is no good:

    Java Code:
    public class Foo {
    
         System.out.println("hello, my name is Fubarable");
    
    }

    But this is just fine:
    Java Code:
    public class Foo {
    
      public Foo() {
         System.out.println("hello, my name is Fubarable");
      }
    
      public static void main(String[] args) {
         Foo myFoo = new Foo();
      }
    
    }

  5. #5
    lost_soul is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    Ok, thank you.
    I've tried it like this now.

    These are the errors I'm getting. The example didn't use a return type so I'm not sure what it means by return type in the error?


    Shape.java:13: invalid method declaration; return type required
    public Polygon()
    ^
    Shape.java:27: class, interface, or enum expected
    }
    ^
    2 errors



    Java Code:
    import javax.swing.JPanel;
    import java.awt.*;
    
    
    
    public class Shape extends JPanel
      {
       
        public Polygon()
        {
         int[]xShape = new int[4];
         int[]yShape = new int[4];
         int nShape;  // count of points
       
         // Make a shape
         xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;
         yShape[0]=139; yShape[1]=122; yShape[2]=122; yShape[3]=139; 
         nShape = 4;
        }
    
         public static void main(String[] args) 
        {
         Polygon myPolygon = new Polygon(xShape, yShape, nShape);
        }
      }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Now you're confusing me and the compiler. Is your class named Polygon (as it seems to be for the constructor and when you use it in main) or Shape (as you have declared it in the class signature)?

    My suggestion: don't use either term since both names are part of the java Graphics libraries, but also make sure that the name you use for the class is the same as the file name as the constructor name and as the name you use in the main method. And keep studying the basics.

  7. #7
    lost_soul is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    Sorry, I'm confused also. I thought Polygon was part of the java.awt library so I'm trying to figure out how to call it rather then defining a method with it's own name.

    I changed the class name to match the method name. If that's correct way to go.

    These are the errors it's showing me now:

    Shape.java:21: cannot find symbol
    symbol : variable xShape
    location: class Shape
    Polygon myShape = new Shape(xShape, yShape, nShape);
    ^
    Shape.java:21: cannot find symbol
    symbol : variable yShape
    location: class Shape
    Polygon myShape = new Shape(xShape, yShape, nShape);
    ^
    Shape.java:21: cannot find symbol
    symbol : variable nShape
    location: class Shape
    Polygon myShape = new Shape(xShape, yShape, nShape);
    ^
    Shape.java:21: internal error; cannot instantiate Shape.<init> at Shape to ()
    Polygon myShape = new Shape(xShape, yShape, nShape);
    ^
    Shape.java:28: cannot find symbol
    symbol : variable myShape
    location: class Shape
    g.fillPolygon(myShape); // fills shape above.
    ^
    5 errors




    Java Code:
    import javax.swing.JPanel; 
    import java.awt.*; 
    
    public class Shape extends JPanel 
    { 
        public Shape() 
    	 { 
    	    int[]xShape = new int[4];
    		 int[]yShape = new int[4]; 
    		 int nShape;  // count of points 
    		 
    		 
    		 // Make a shape 
    		 xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203; 
    		 yShape[0]=139; yShape[1]=122; yShape[2]=122; yShape[3]=139; 
    		 nShape = 4;
    	 }	 
    		 
    	 public static void main(String[] args) 
    	 { 
    	    Polygon myShape = new Shape(xShape, yShape, nShape); 
    	 } 
    
    	 
    	 public void paintComponent(Graphics g) 
    	 {
           super.paintComponent(g);    // paint background
           g.fillPolygon(myShape);     // fills shape above.
        } 
     
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You've got some very pernicious errors that have nothing to do with Swing coding and everything to do with understanding the basics of Java. For example:

    Java Code:
        Polygon myShape = new Shape(xShape, yShape, nShape);
    So you appear to be creating a Polypgon variable, myShape and trying to construct it with a Shape constructor. So again, what is this? A Polypgon or a Shape? You have to make up your mind and choose one or the other. If it's a Shape object, you're calling a constructor that is not present in the class.


    Here:
    Java Code:
      public static void main(String[] args) {
        Polygon myShape = new Shape(xShape, yShape, nShape);
      }
    
      public void paintComponent(Graphics g) {
        super.paintComponent(g); // paint background
        g.fillPolygon(myShape); // fills shape above.
      }
    
    }
    You appear to be trying to use a variable, myShape that is declared and used in the main method, a static method, in the class itself, something that's impossible to do and not desired even if it were possible. The myShape variable exists only in the main method and is only visible in the main method.

    You may benefit from tutoring to overcome some misunderstandings of the basics of Java. I strongly urge you to get together with your instructor or tutor and soon.

    Best of luck!
    Last edited by Fubarable; 04-25-2010 at 12:30 AM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If I were going to do this I'd create a class called UsePolygon that extends JPanel. As I mentioned previously, I'd not use the name Shape since that's already a class in the Java libraries.

    I'd give the class two Polygon objects, polygon1 and polygon2 that are initialized with the arrays that you have posted in your OP and that used the Polygon constructor that I've found in the API: Polygon (Java Platform SE 6)

    I'd have this class override paintComponent, as you are doing, and in this method, I'd create a Graphics2D object that is assigned the Graphics object that is passed into the paintComponent method's Graphics parameter. You'll have to cast the object into a Graphics2D object. I'd then have this method draw the two polygons using the Graphics2D draw method.

    I'd then display this JPanel in a JFrame.

    Again, best of luck.

Similar Threads

  1. Drag polygon
    By cassysumandak in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 06:30 PM
  2. How to copy a polygon
    By cassysumandak in forum New To Java
    Replies: 6
    Last Post: 10-07-2009, 12:37 AM
  3. Replies: 2
    Last Post: 04-01-2009, 08:37 AM
  4. polygon-shaped JComponent
    By zenMarko in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 06:06 PM
  5. How to Fill a Polygon in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •