1

我跳回了一堆旧代码,我的 Java 非常粗糙。请善待。

问题:我有一个在画布上绘图的应用程序。屏幕对象的放置效果很好。甚至附加到其他对象的文本。但是,当我在画布上放置一个文本对象时,画布的比例减半。我已经折腾了几个月,似乎找不到解决办法。任何意见将是有益的。

下面是使用其他绘图方法在 Visualise2D 类中在屏幕上绘制文本的代码。所有其他对象都使用相同的比例等。这只发生在我升级到 Java 15 之后,我使用的最后一个 java 是 java 8,它运行良好。

    //TEXT  
    public void paintText(Graphics2D t2D, Color color,Text t, Font font, double bearing, Rectangle2D bounds, double scale, boolean selected, boolean isRotationTool, double enhance) {

        //Draws text where ever the user clicks
        FontMetrics fm = t2D.getFontMetrics();

        t2D.setFont(default_FONT);
        
        AffineTransform at = new AffineTransform();

        int x = (int) ((t.getX() - bounds.getX())*(scale));
        int y = (int) ((bounds.getHeight() + bounds.getY() - t.getY()) *(scale));

        at.setToRotation(Math.toRadians(bearing+270), x,y);
        FontRenderContext frc = t2D.getFontRenderContext();
        TextLayout layout = new TextLayout(t.getText(), t2D.getFont(), frc);
        
        t2D.setTransform(at);
        
        if (!(selected)) {
            t2D.setColor(color);
        }
        else 
        {
            //pixel size of the circle
            float size = 20;//(float) (fm.stringWidth(t.getText())*0.5);
            t2D.setColor(p_selectedObjectsColour);
            t2D.setStroke(LINE_100);
            
            //Highlight and origin indicator when selected - START
            t2D.setColor(p_selectedObjectsColour);
            t2D.draw(new Ellipse2D.Double((((t.getX() - bounds.getX())*scale) - size), (((bounds.getHeight() + bounds.getY() - t.getY())*scale) - size), (size*2), (size*2))); 
            
            if(isRotationTool){
                t2D.drawString("  : \uu27f3 "+dec1P.format(bearing)+"\u00b0",(float) (x + (fm.stringWidth(t.getText())*1.05)),y);
            }
            
            t2D.setColor(p_selectedObjectsColour);
            
            t2D.draw(new Rectangle2D.Double(
                    (t.getX() - bounds.getX())* scale,
                    ((bounds.getHeight() + bounds.getY() - t.getY())*scale)-fm.getStringBounds(t.toString(), t2D).getHeight(),
                    t.getBounds().getWidth(),
                    t.getBounds().getHeight()
                    ));
            t2D.drawLine((int) (((t.getX() - bounds.getX())) * scale),
                    (int)(((bounds.getHeight() + bounds.getY())-(t.getY()))*scale), 
                    (int)(((t.getX())- bounds.getX())*scale)+fm.stringWidth(t.getText()),
                    (int)(((bounds.getHeight() + bounds.getY())-(t.getY()))*scale));
        }
        t2D.setColor(color);
        //t2D.drawString(t.getText(), x, y);
        layout.draw(t2D, x, y);

        at.setToRotation(0, x, y);

        t2D.setTransform(at);
        //This error is to remind you that the Affine transform is not working and the text is in the collection still after it is moved. 

    }

下面是描述该问题的两张图片。
图 1 是正常比例的正常视图 图 2 是添加文本后的更改比例。

如果文本被删除,比例将返回到第一个图像。

正常比例:

https://i.stack.imgur.com/FjfPe.jpg

添加了文本比例更改:

https://i.stack.imgur.com/Dh1RK.jpg

4

0 回答 0