我一直试图将我的java2d形状的中心与JPanel的中心对齐但没有成功。我能够使用getBounds方法对图像和许多2D形状(如平行四边形)进行此操作,但对于菱形则不能,但所有这些都遵循相同的模式。很大程度上,当我从实际项目中准备一个SSCCE时,我可以正确地对齐它们。 我写了一个drawShape方法在中心绘制形状。我不明白我哪里出错了。 这是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);
}
}
任何帮助将不胜感激 修改 我刚从代码中删除了令人困惑的行......
答案 0 :(得分:7)
您需要考虑Shape的有界x / y位置:
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
答案 1 :(得分:1)
修改强>:
在方法rhombusFactory()
中,更改:
a.y=0;
到此:
a.y=height/2;
短暂扩展:
在drawShape()
方法中,将图形上下文的原点放在假定形状边界的左上角为(Point:[(getWidth-width)/ 2,(getHeight() - height)/ 2的位置])。在rhombusFactory()
方法中,您将点a
设置为(0,0),从而将形状的左上角移动到(0,-height / 2),从而有效地使其不是垂直居中。
答案 2 :(得分:1)
你的翻译错了。您应该首先将绘图空间的原点转换为面板的中心,然后将绘图的中心转换为原点。你做了后者,但第一个错误。
更改
g.translate(this.getWidth(),this.getHeight());
g.translate(-width/2,-height/2);
到
g.translate(this.getWidth()/2,this.getHeight()/2);
g.translate(-width/2,-height/2);
或者只是
g.translate((this.getWidth() - width) / 2,(this.getHeight() - height) / 2);