如何缩放区域以使其成为更大的轮廓?

时间:2015-10-22 00:20:38

标签: java scale graphics2d java-2d area

我有一个区域形状,我正在使用graphics2D.setClip(区域)来绘制纹理。我需要将该区域扩展到更大的版本,但是当我这样做时,它有时会混乱。通过我的方式,它为区域提供AffineTransform,然后缩放和翻译它,this发生。有什么想法吗?

编辑: 这是一个可运行的例子。

public class Areas extends JPanel {

Area area;

public static void main(String[] args) {
    new Areas();
}

public Areas() {
    JFrame frame = new JFrame();
    frame.setSize(600, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.add(this);
    frame.setVisible(true);

    area = new Area();

    //this works fine
    area.add(new Area(new Ellipse2D.Double(300-70, 300-70, 100, 100)));

    //when you add more, it messes up
    //area.add(new Area(new Ellipse2D.Double(400-70, 400-70, 100, 100)));
    //I want it to show up as an outline similar to the result of just having one ellipse added
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;

    g2.setColor(Color.black);
    g2.draw(area);

    Area area2 = new Area();
    area2.add(area);

    AffineTransform at = new AffineTransform();

    double scale = 2;
    at.scale(scale, scale);

    double xDiff = area.getBounds().getCenterX()*scale - area.getBounds().getCenterX();
    double yDiff = area.getBounds().getCenterY()*scale - area.getBounds().getCenterY();

    at.translate(-xDiff/2, -yDiff/2);

    area2.transform(at);

    g2.setColor(Color.red);
    g2.draw(area2);

}

}

0 个答案:

没有答案