Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Graphics: drawPolygon vs fillPolygon

Thread view: 
Christopher Benson-Manica - 03 Apr 2007 15:44 GMT
I've read the documentation, and I'm still having trouble
understanding exactly why fillPolygon() doesn't (always) produce a
filled-in version of drawPolygon().  Specifically, I have the
following class:

public class UpIcon implements Icon {
 private final Polygon polygon = new Polygon(
     new int[]{2,8,5},
     new int[]{5,5,2},
     3
 );

 public void paintIcon( Component c, Graphics g, int x, int y ) {
   g.drawPolygon( polygon ); // Gives me the shape I want, not filled
                             // in of course.
   g.fillPolygon( polygon ); // Seems to fill the *interior* of the
                             // shape I get with drawPolygon(), i.e.
                             // I get a filled polygon that's too
                             // small.
 }

 public int getIconWidth() {
   return 11;
 }

 public int getIconHeight() {
   return 11;
 }
}

Is there something wrong with what I'm doing here, or am I merely
missing some subtlety about how Graphics implements drawPolygon and
fillPolygon?  (When the array of yPoints is {5,5,8}, I get a DownIcon
that works the way I want, so I'm presuming the answer is the latter.)

Signature

C. Benson Manica           | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com      | don't, I need to know.  Flames welcome.

Knute Johnson - 03 Apr 2007 17:33 GMT
> I've read the documentation, and I'm still having trouble
> understanding exactly why fillPolygon() doesn't (always) produce a
[quoted text clipped - 30 lines]
> fillPolygon?  (When the array of yPoints is {5,5,8}, I get a DownIcon
> that works the way I want, so I'm presuming the answer is the latter.)

It works fine for me.  Show us a complete, compilable example that
demonstrates your problem.

Signature

Knute Johnson
email s/nospam/knute/

Christopher Benson-Manica - 03 Apr 2007 19:21 GMT
> It works fine for me.  Show us a complete, compilable example that
> demonstrates your problem.

You will note that the drawn polygon and the filled polygon are not
the same size:

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

public class Foo extends JFrame {
   private final Polygon polygon = new Polygon(
           new int[]{2,8,5},
           new int[]{5,5,2},
           3
   );

   private Foo() {
       super("Test");
       setLayout(new GridLayout(2,1));
       final JLabel A = new JLabel("A");
       getContentPane().add(A);
       A.setIcon(new Icon() {
           public void paintIcon(Component c, Graphics g, int x, int y) {
               g.drawPolygon(polygon);
           }

           public int getIconWidth() {
               return 11;
           }

           public int getIconHeight() {
               return 11;
           }
       });
       final JLabel B = new JLabel("B");
       getContentPane().add(B);
       B.setIcon(new Icon() {
           public void paintIcon(Component c, Graphics g, int x, int y) {
               g.fillPolygon(polygon);
           }

           public int getIconWidth() {
               return 11;
           }

           public int getIconHeight() {
               return 11;
           }
       });
   }

   public static void main(String[] args) throws Exception {
       final Foo foo = new Foo();
       foo.pack();
       foo.setVisible(true);
   }
}
Signature

C. Benson Manica           | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com      | don't, I need to know.  Flames welcome.

Daniel Pitts - 03 Apr 2007 20:01 GMT
On Apr 3, 11:21 am, Christopher Benson-Manica
<a...@ukato.freeshell.org> wrote:
> > It works fine for me.  Show us a complete, compilable example that
> > demonstrates your problem.
[quoted text clipped - 56 lines]
> C. Benson Manica           | I *should* know what I'm talking about - if I
> cbmanica(at)gmail.com      | don't, I need to know.  Flames welcome.

The first draws the outline, the second fills the inside.

Think of it as drawPoly will draw the edges, where fillPoly will fill
everything between the edges.

You might be better off using Shape classes and Graphics2D to get the
effect you want.

Hope this helps.
Christopher Benson-Manica - 03 Apr 2007 20:11 GMT
> On Apr 3, 11:21 am, Christopher Benson-Manica

> The first draws the outline, the second fills the inside.

If that were always true, I could understand that.  However...

> >     private final Polygon polygon = new Polygon(
> >             new int[]{2,8,5},
> >             new int[]{5,5,2},
               new int[]{5,5,8}, // Flip upside down
> >             3
> >     );

...gives an outline that is the same size as the filled version.  So
presumably this has something to do with how polygons are drawn versus
how they are filled, and the documentation I have seen doesn't suggest
that there is a difference (even though there clearly is).

> You might be better off using Shape classes and Graphics2D to get the
> effect you want.

That may well be true, and I will look into those.  Thanks.

Signature

C. Benson Manica           | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com      | don't, I need to know.  Flames welcome.

Chris Uppal - 03 Apr 2007 21:59 GMT
> ...gives an outline that is the same size as the filled version.  So
> presumably this has something to do with how polygons are drawn versus
> how they are filled, and the documentation I have seen doesn't suggest
> that there is a difference (even though there clearly is).

The package comment for java.awt.Graphics should shed some light.

   -- chris
Chris Uppal - 03 Apr 2007 22:05 GMT
I wrote:

> The package comment for java.awt.Graphics should shed some light.

Bah!  Let me try that again...

The /class/ /JavaDoc/ for [etc...]

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.