代码之家  ›  专栏  ›  技术社区  ›  Boolean

JAVA2D:翻译轴

  •  1
  • Boolean  · 技术社区  · 14 年前

    我正在使用java2d开发一个应用程序,我注意到奇怪的是,原点在左上角,正x向右,正y向下增加。

    是否有方法将原点左下移动?

    谢谢您。

    5 回复  |  直到 6 年前
        1
  •  4
  •   luke    14 年前

    你需要做一个比例尺和一个翻译。

    在你 paintComponent 方法可以这样做:

    public void paintComponent(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(0, -height);
        g2d.scale(1.0, -1.0);
        //draw your component with the new coordinates
    
        //you may want to reset the transforms at the end to prevent
        //other controls from making incorrect assumptions
        g2d.scale(1.0, -1.0);
        g2d.translate(0, height);
    }
    

    我的秋千有点生锈,但这应该能完成任务。

        2
  •  2
  •   pradeekrathnayaka    11 年前

    我们可以用以下方法轻松解决问题,

    public void paintComponent(Graphics g)
    {
    Graphics2D g2d = (Graphics2D) g;
    // Flip the sign of the coordinate system
    g2d.translate(0.0, getHeight());
    g2d.scale(1.0, -1.0);
    ......
    }
    
        3
  •  0
  •   Chuk Lee    14 年前

    你试过了吗? Graphics2D.translate() ?

        4
  •  0
  •   Jeff    14 年前

    你会想习惯的。正如Luke提到的,从技术上讲,您可以对图形实例应用转换,但这最终会对性能产生负面影响。

    只做一个平移就可以将0,0的位置移动到左下角,但是沿正轴的移动仍然会在X方向上向右,在Y方向上向下,所以你唯一能完成的就是在屏幕外绘制所有东西。你需要做一个旋转来完成你的要求,这将增加弧度计算的开销到图形实例的转换矩阵。这不是一个好的折衷办法。

        5
  •  0
  •   malat    6 年前

    只是为了以后参考,我必须将调用的顺序切换到我的代码中的 scale translate 也许这会对将来的某个人有所帮助:

    测试 public void BottomLeftOriginTest()引发IOException{ int宽度=256; 内部高度=512; bufferedimage bi=新bufferedimage(宽度、高度、bufferedimage.type_int_bgr); graphics2d ig=bi.creategraphics(); //保存“旧”转换 affinetransform old=ig.getTransform(); //原点在左上方: //使用反转的Y变换更新图形对象 如果(真)/*订购正常*/ ig.比例(1.0,-1.0); ig.translate(0,-bi.getheight()); }否则{ ig.translate(0,-bi.getheight()); ig.比例(1.0,-1.0); } int xpoints[]=新int[]0,宽度,宽度 int ypoints[]=新int[]0,高度,0 int npoints=xpoints.length; ig.setcolor(颜色.蓝色); ig.fillRect(0,0,bi.getWidth(),bi.getHeight()); ig.setcolor(颜色,红色); ig.圆角多边形(xpoints、ypoints、npoints); //还原旧转换 ig.settransform(旧版); //将结果导出到文件 imageio.write(bi,“png”,新文件(“origin.png”)); } < /代码>

    LP未来的某人:

    @Test
    public void bottomLeftOriginTest() throws IOException {
        int width = 256;
        int height = 512;
    
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);    
        Graphics2D ig = bi.createGraphics();
        // save the "old" transform
        AffineTransform old = ig.getTransform();
    
        // origin is top left:
        // update graphics object with the inverted y-transform
        if (true) { /* order ok */
            ig.scale(1.0, -1.0);
            ig.translate(0, -bi.getHeight());
        } else {
            ig.translate(0, -bi.getHeight());
            ig.scale(1.0, -1.0);
        }
    
        int xPoints[] = new int[] { 0, width, width };
        int yPoints[] = new int[] { 0, height, 0 };
        int nPoints = xPoints.length;
        ig.setColor(Color.BLUE);
        ig.fillRect(0, 0, bi.getWidth(), bi.getHeight());
    
        ig.setColor(Color.RED);
        ig.fillPolygon(xPoints, yPoints, nPoints);
    
        // restore the old transform
        ig.setTransform(old);
    
        // Export the result to a file
        ImageIO.write(bi, "PNG", new File("origin.png"));
    }
    

    bottom left origin