1

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

camickr
  • 321,443
  • 19
  • 166
  • 288
BChin B
  • 11
  • 2
  • 1
    Post images of your frame, not your desktop. The desktop is irrelevant to the question and makes the frame part so small that we can hardly see the frame and its text. Post an [mre] demonstrating the problem. – camickr Oct 18 '21 at 14:15
  • 1
    *This only occurred since I upgraded to Java 15, last java I used was java 8 and it worked fine.* - I think later versions of the JDK use the OS scaling factor in the graphics. You need to make sure you add this scaling factor to your AffineTransform. See: https://stackoverflow.com/questions/32216929/scaling-graphics-with-affinetransform/32217377#32217377 – camickr Oct 18 '21 at 14:19
  • @camickr thanks for the responses and thanks for the advice. Sorry for the image sizes. I will attempt to implement the OS scaling factor in the Affine Transform. – BChin B Oct 23 '21 at 06:08
  • @camickr I appreciate the comments you provided. Thanks so much. However I am unable to find the right method to call on the AffineTransform. I thought there might be a toolkit that I could call to get the scaling, like with screen size or width... Would you consider providing any additional advice. Thanks. – BChin B Oct 30 '21 at 06:57
  • @b 1) I already provided a link with the basic logic to set the proper scaling factor for the AffineTransform. You haven't explained why this didn't help. 2) You still haven't posted an [mre]. So you haven't made an extra effort to provide us with information needed to solve the problem. Not sure what more I can do. – camickr Oct 30 '21 at 13:50

0 Answers0