Graphics: drawPolygon vs fillPolygon

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

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.)
 
K

Knute Johnson

Christopher said:
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.)

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

Christopher Benson-Manica

Knute Johnson said:
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);
}
}
 
D

Daniel Pitts

Knute Johnson said:
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);
}}

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.
 
C

Christopher Benson-Manica

Daniel Pitts said:
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.
 
C

Chris Uppal

Christopher said:
...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
 
C

Chris Uppal

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

Bah! Let me try that again...

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

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top