Hi ,
Can anyone point me to fillPolygon source code ,
In Graphics.java it is declared as abstract class .

  • Nataraja G

Recommended Answers

All 12 Replies

Interesting question. A quick Google doesnt reveal any subclasses for Graphics2d - maybe they are private?

You could do something that gives you a Graphics2d object (eg overide paintComponent) then test the runtime variable to see exactly what class it really is. With the actual class name it may be easier to track down the source.

Please post the answer here if you do find it!

@JamesCherrill wrote - Interesting question. A quick Google doesnt reveal any subclasses for Graphics2d - maybe they are private?,

---> not is available only for its parent - Graphics,

@JamesCherrill wrote - Please post the answer here if you do find it!

---> yes, no problem with, e.g. :-)

        @Override // paint Icon (JComboBox, JCheckBox, sort icon for RowSorter....)
        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.setColor(color);
            g.translate(x, y);
            if (selected) {
                g.fillPolygon(poly1);
            } else {
                g.drawPolygon(poly2);
            }
            g.translate(-x, -y);
        }

or

     protected void paintComponent(Graphics g) { //draw a Car
        super.paintComponent(g);
        g.setColor(Color.GREEN);
        //g.fillOval(0, 0, 25, 25);
        panelWidth = getWidth(); // better by override getPreferredSize
        panelHeight = getHeight(); //then getWidth() and getHeight() returns proper value 
        g.setColor(color);
        //polygon points
        int t_x[] = {x + 10, x + 20, x + 30, x + 40};
        int t_y[] = {y + 10, y, y, y + 10};
        g.fillPolygon(t_x, t_y, t_x.length);
        g.fillRect(x, y + 10, 50, 10);
        //g.fillArc(x + 10, y + 20, 10, 10, 0, 360);
        //g.fillArc(x + 30, y + 20, 10, 10, 0, 360);
    }

Thise are examples of code that uses fillPolygon, but the OP was asking where to find the source code for fillPolygon. It's defined in Graphics, but its abstract. Graphics2D doesn't have an implemention either, so somewhere there is one or mode non-abstract subclasses that have a concrete implementation.

ps
I just tried printing the class of the Graphics parameter in a simple Swing application and its
sun.java2d.SunGraphics2D
so that's where the spource code will be!

my view

  • original Java by Sun was about enthusiasm, tons of API has lack in descriptions (authors of description were inspired by MSDN:-)

  • almost, maybe all methods used for Graphics/Graphics2D, available in paint/paintChildren/paintBorder/paintIcon/paintComponent/paintImmediatelly are abstract, part of them is protected (thats one of reasons to declare the paintComponent protected too), just to avoids against an accesses, narrow the penetration from/to uninialized class, void, methods, variables

  • those methods couldn't be accessed from outside, only inside (e.g.) paintComponent, otherwise (dirty casting from outside is possible) result should be exception from RepaintManager,

  • I think thit is top of the safest implementations in compare with AWT methods draw()

  • paintImmediatelly must be call out of EDT (only if isEventDispatchThread returs false) otherwise RepaintManager can lock current JVM instance, forever

(Most likely) the actual implementation is inside the JVM and is platform specific.
http://openjdk.java.net/groups/2d/ is the implementation project of the openjdk variant of Java2D.

Hello , 
    Thanks for your help , I was not able to find the code .
used anyother workaround which is not efficient.  

background of this problem : I'm drawing multiple Rect/Polygons on JPanel and few Rect/Poly are filled with TexturePatterns wherein brackground is black and patterns(45/135 lines) are drawn in some colour 

when I'm filling with this TexturePattern , I want the backgound image to be transparent i.e whereever there is black pixel filling should not happen , this will give transparent effect and easiest way is to override fillRect/Polygon code 

In my workaround instead of fillRect/Polygon I've used drawRect/Polygon and used drawLine to created needed patterns !!


- Nataraja G

Why didn't you say that in the first place?
Just setOpaque(false) on your JPanel, and instead of black use any color with an alpha value of 0 (which means fully transparent)

Hi James ,
I'm quite new to Java/Swing so I didn't know all these features !

"instead of black use any color with an alpha value of 0 "
I'm not sure what this means , tried googling .
below is the code snippet , can you kindly enlighten me

Rectangle r = new Rectangle(0, 0, texture_size, texture_size);
for ( Color itr_color : color_list) {
 BufferedImage bi = new BufferedImage(texture_size, texture_size, BufferedImage.TYPE_INT_RGB);
 big = bi.createGraphics();
 big.setColor(Color.black);
 big.fillRect(0, 0, texture_size, texture_size);
 big.setColor(itr_color);
 big.drawLine(texture_size, 0, 0, texture_size);
 big.drawLine(texture_size, 0, texture_size, texture_size);
 texturePaints.push( new TexturePaint(bi, r));
}

// while drawing rectangles I'm using texturePaints like this. 

 graphics2D_Obj.setPaint(texturePaint[cnt]);
 graphics2D_Obj.fillRect(dx, dy, dw, dh);

Thanks
Nataraja G

Change the BufferedImage to TYPE_INT_ARGB (RGB plus alpha channel). The alpha value (0-255) specifies how opaque the color is.
Instead of black, use a totally transparent color, eg new Color(0,0,0,0) (the fourth parameter is the alpha value)
and finally make the JPanel transparent (not opaque) using setOpaque(false)

Thanks James ,
I'll give it a try .

  • Nataraja G

Hello ,
It worked as expected , thanks James .
I'll look into offset issues .

  • Nataraja G

Hello ,
Just an update,filling rectangles with ( fillRect + ARGB TexturePaint )
has more runtime than ( drawRect + 45 degree lines )

  • Nataraja G
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.