本文介绍了将Shape的中心与JPanel的中心对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图将java2d形状的中心与JPanel的中心对齐,但没有成功.我能够使用getBounds方法对图像和许多2D形状(如平行四边形)执行此操作,但对于菱形却无法做到,尽管它们都遵循相同的模式.彻底地,当我从实际项目中准备SSCCE时,我无法正确地对齐它们.我已经编写了一个drawShape方法来在中心上绘制形状.我不明白我要去哪里错了.这是SSCCE:

I have been trying to align my java2d shape's center to the JPanel's center with no success. I was able to do it for an image and many 2D shapes like parallelogram using getBounds method but not for rhombus though all of them follow the same pattern. Drastically, when I prepared an SSCCE out of the actual project I could align none of them correctly.I've written a drawShape method for drawing shapes on center. I didn't understand where I'm going wrong.This is 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 help would be appreciatedI just removed the confusing lines out of the code...

推荐答案

您需要考虑Shape的有界x/y位置:

You need to account for the bounded x/y location of the Shape:

Rectangle bounds=shape.getBounds(); // changed this
...
//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 this

这篇关于将Shape的中心与JPanel的中心对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:52