Home > Back-end >  Difference between getscaleinstance and scale? (swing java)
Difference between getscaleinstance and scale? (swing java)

Time:12-21

I saw a code that used getScaleInstance(x,x) to declare the scale. On the other hand, I also saw that some people used scale to declare the scale. What is the difference?

For example:

AffineTransform test= new AffineTransform();
test.scale(1.0,1.0) //1
test=AffineTransform.getScaleInstance(1.0,1.0) //2

What is the difference between 1 and 2?

CodePudding user response:

I have checked the docs for you and have found that:

scale() - is a transformation of the matrix (inside AffineTransform) which is equal to following code:

AffineTransform test = new AffineTransform();
AffineTransform scale = AffineTransform.getScaleInstance(1.0, 1.0);
test.concatenate(scale);

As you can see from code above, firstly, we create a scale by calling AffineTransform.getScaleInstance and, secondly, we apply this scale to the "test".

So, scale() is a more concise way to do scalling. However, you should notice that scale() accepts two double params which descibe scale factors. Therefore, if you somehow calculate the scale as a AffineTransform, you should use concatenate() instead of scale().

Summaryzing, these codes do the same thing, they are equal:

AffineTransform test = new AffineTransform();
test.scale(1.0, 1.0);

and

AffineTransform test = new AffineTransform();
test.concatenate(AffineTransform.getScaleInstance(1.0, 1.0));

The first approach is unquestionable more concise than the second one.

  • Related