Java 15 - Adding Customisable Text to my canvas produces scaling/position issue

63 views Asked by At

I am jumping back into an old bunch of code and my Java is very rough. Please be kind.

Problem: I have an application that draws on the canvas. The placement of the screen objects works well. Even Text attached to other objects. However when I place a Text object on the canvas the scale of the canvas halves. I have fiddled off and on for months and can't seem to find the resolution. Any advice would be helpful.

Below is the code to draw the text on screen it is in a class Visualise2D with the other drawing method. All other objects use the same scale etc. This only occurred since I upgraded to Java 15, last java I used was java 8 and it worked fine.

    //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. 

    }

Below are two images that describe the issue.
Image 1 is the Normal View at Normal Scale Image 2 is the Alter after Text addition Scale.

If the text is deleted the Scale returns to the first image.

Normal Scale:

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

Added Text Scale Changes:

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

0

There are 0 answers