NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

paintComponent() Graphics2D versus panel Graphics2D

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
gmseed



Joined: 01 Dec 2008
Posts: 38

PostPosted: Thu Nov 18, 2010 2:31 pm    Post subject: paintComponent() Graphics2D versus panel Graphics2D Reply with quote

Hi

If I place the panel below onto a frame using the argument Graphics2D object then I see the centred circle.

However, if I use the Graphics2D object of the panel [via this.getGraphics()] then I do not see the circle.

I'm trying to draw several objects on a panel accurately and found that the argument Graphics2D object refers to the overall frame graphics object and performing any affine transform operations are with respect to the frame and not the panel. Thus, if I wanted to use the frame Graphics2D I would to calculate offsets to add to panel object transforms. With the frame having a menubar, toolbar, status bar etc, calculating such offsets is error prone.

Clearly, it is much simpler to use the Graphics2D object of the panel itself. However, it's refusing to make the panel circle visible?

Does anyone know a solution to this problem?

Graham

Code:


public class TestJPanel extends JPanel
{
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D)g;

        //Graphics2D graphics = g2;
        Graphics2D graphics = (Graphics2D)this.getGraphics();

        Rectangle   panelBounds   = this.getBounds();
        int         panelTopLeftX = panelBounds.x;
        int         panelTopLeftY = panelBounds.y;
        int         panelWidth    = this.getWidth();
        int         panelHeight   = this.getHeight();

        // circle
        double circleRadius = 250.0;
        double circleDiameter = 2.0 * circleRadius;
        java.awt.geom.Ellipse2D.Double jcircle = new java.awt.geom.Ellipse2D.Double(0,0,circleDiameter,circleDiameter);

        // save original transform
        AffineTransform origTransform = graphics.getTransform();

        AffineTransform tx = AffineTransform.getTranslateInstance(panelWidth/2-circleRadius, panelHeight/2-circleRadius); // alternative static method of creating transform

        graphics.setTransform(tx);

        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,      RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics.setColor(Color.BLACK);
        graphics.draw(jcircle);
        graphics.fill(jcircle);

        // restore original transform
        graphics.setTransform( origTransform );
    }

} // class TestJPanel
Back to top
gmseed



Joined: 01 Dec 2008
Posts: 38

PostPosted: Thu Nov 18, 2010 3:57 pm    Post subject: Reply with quote

Hi

Gotya!! The affine transform of the argument Graphics2D has to be concatenated with the panel affine transform - see below.

The tutorials never tell you that!

Graham

Code:


    private void paintCircle_Test(Graphics2D g2)
    {
        // panel properties
        Rectangle   panelBounds     = this.getBounds();
        int         panelTopLeftX   = panelBounds.x;
        int         panelTopLeftY   = panelBounds.y;
        int         panelWidth      = panelBounds.width;
        int         panelHeight     = panelBounds.height;

        // circle
        double circleRadius = 250.0;
        double circleDiameter = 2.0 * circleRadius;
        java.awt.geom.Ellipse2D.Double jcircle = new java.awt.geom.Ellipse2D.Double(0,0,circleDiameter,circleDiameter);

        // save original transform
        AffineTransform originalTransform = g2.getTransform();

        // total affine - initialising with g2 affine
        AffineTransform affineTotal = new AffineTransform(originalTransform);

        // panel affine
        AffineTransform affinePanel = AffineTransform.getTranslateInstance(panelWidth/2-circleRadius, panelHeight/2-circleRadius);

        // concat
        affineTotal.concatenate(affinePanel);

        g2.setTransform(affineTotal);

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,      RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setColor(Color.BLACK);
        g2.draw(jcircle);
        g2.fill(jcircle);

        // restore original transform
        g2.setTransform(originalTransform);
    }
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo