java - Aligning Shape's Center to JPanel's Center -


i have been trying align java2d shape's center jpanel's center no success. able image , many 2d shapes parallelogram using getbounds method not rhombus though of them follow same pattern. drastically, when prepared sscce out of actual project align none of them correctly. i've written drawshape method drawing shapes on center. didn't understand i'm going wrong. sscce:

            import java.awt.*;             import java.awt.geom.*;             import java.util.*;             import javax.swing.*;             public class testpanel extends jpanel{                     point a,b,c,d;                     shape trapezium,parallelogram;                     random random=new random();                     public testpanel(){                         a=new point();                         b=new point();                         c=new point();                         d=new point();                         rhombusfactory(a,b,c,d);                         trapezium=getquadrilateral(a,b,c,d);                       }                     private void rhombusfactory(point a,point b,point c,point d)                     {       int width=random.nextint(200-100)+100;                             int height=random.nextint(150-50)+50;                             a.x=0;                             a.y=0;                             b.x=a.x+width/2;                             b.y=a.y+height/2;                             c.x=a.x+width;                             c.y=a.y;                             d.x=a.x+width/2;                             d.y=a.y-height/2;                     }                     private void parallelogramfactory(point a,point b,point c,point d){                         int l1=random.nextint(200-100)+100;                         int l2=random.nextint(150-70)+70;                         int offset=(random.nextint(2)==0?-1:1)*(random.nextint(50-20)+20);                         a.x=0;                         a.y=0;                         b.x=a.x+l1;                         b.y=a.y;                         d.x=a.x+offset;                         d.y=a.y+l2;                         c.x=d.x+l1;                         c.y=d.y;                 }                      private shape getquadrilateral(point a,point b,point c,point d){                          generalpath gp=new generalpath();                          gp.moveto(a.x,a.y);                          gp.lineto(b.x,b.y);                          gp.lineto(c.x,c.y);                          gp.lineto(d.x,d.y);                          gp.closepath();                          return gp;                     }                     private void drawshape(graphics2d g,shape shape){                         affinetransform oldt=g.gettransform();                         rectangle2d bounds=shape.getbounds2d();                         double height=bounds.getheight();                         double width=bounds.getwidth();                         g.setrenderinghint(renderinghints.key_antialiasing,renderinghints.value_antialias_on);                         g.setstroke(new basicstroke(2.0f, basicstroke.cap_butt, basicstroke.join_bevel));                         g.translate(this.getwidth()/2,this.getheight()/2);                         g.translate(-width/2,-height/2);                         g.draw(shape.getbounds2d());                         g.draw(shape);                         g.settransform(oldt);                 }                     public void paintcomponent(graphics g2){                             super.paintcomponent(g2);                             graphics2d g=(graphics2d)g2;                             drawshape(g,trapezium);                             //drawshape(g,parallelogram);                     }                     public static void main(string args[]){                             jframe jf=new jframe();                             testpanel tp=new testpanel();                             jf.setlayout(new borderlayout());                             jf.add(tp,borderlayout.center);                             jf.setsize(500,500);                             jf.setvisible(true);                     }              } 

any appreciated edit: removed confusing lines out of code...

you need account bounded x/y location of shape:

rectangle bounds=shape.getbounds(); // changed ... //g.translate(this.getwidth()/2,this.getheight()/2); //g.translate(-width/2,-height/2); g.translate((this.getwidth() - width) / 2,(this.getheight() - height) / 2); g.translate(-bounds.x, -bounds.y); // added 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -