【问题标题】:2D clip area to shape2D 剪辑区域进行整形
【发布时间】:2013-10-27 08:12:44
【问题描述】:

我对 java 中的图形很陌生,我正在尝试创建一个可以剪辑到另一个形状底部的形状。这是我想要实现的一个示例:

形状底部的白线是圆形边缘内的那种剪裁。 我目前这样做的方式是这样的:

g2.setColor(gray);
Shape shape = getShape(); //round rectangle
g2.fill(shape);
Rectangle rect = new Rectangle(shape.getBounds().x, shape.getBounds().y, width, height - 3);
Area area = new Area(shape);
area.subtract(new Area(rect));
g2.setColor(white);
g2.fill(area);

我仍在尝试使用剪辑方法,但我似乎无法做到正确。这种当前方法是否可行(性能方面,因为组件经常重绘)还是有更有效的方法?

【问题讨论】:

  • 发布您的SSCCE 来证明问题。
  • 我怀疑更有效的方法是先在白色上涂上黄色,然后在上面涂上黄色,而不是做面积减法,你仍然在做同样数量的绘画,只是少了一次(可能很贵)与区域操作。
  • 提高效率的唯一方法是将结果缓冲到 BufferedImage 上并简单地绘制,只根据需要更改缓冲区...

标签: java awt shape java-2d


【解决方案1】:

我认为您关于使用剪辑方法的最初想法是正确的方法。这对我有用:

static void drawShapes(Graphics2D g, int width, int height,
    Shape clipShape) {

    g.setPaint(Color.BLACK);
    g.fillRect(0, 0, width, height);

    g.clip(clipShape);

    int centerX = width / 2;
    g.setPaint(new GradientPaint(
        centerX, 0, Color.WHITE,
        centerX, height, new Color(255, 204, 0)));

    g.fillRect(0, 0, width, height);

    g.setPaint(Color.WHITE);
    int whiteRectHeight = height * 4 / 5;
    g.fillRect(0, whiteRectHeight,
        width, height - whiteRectHeight);
}

【讨论】:

    【解决方案2】:

    当前的方法是否可行(性能方面,因为组件经常重绘)..

    减去形状是我的做法。对象可以是几个实例,也可以是(可能)单个实例,根据需要是transformed

    1. 一个text demo.,使用缩放和淡入淡出。
    2. 这是one with simple lines(..和点,..它是动画的)。

    当然,如果图像是纯叠加的,请使用BufferedImage 作为画布并以JLabel/ImageIcon 组合显示它。就像这两个例子一样。

    【讨论】:

      猜你喜欢
      • 2012-10-13
      • 2011-08-04
      • 2019-05-27
      • 2017-08-08
      • 2012-08-06
      • 2014-08-02
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      相关资源
      最近更新 更多