سؤال

I have a Canvas class, which is an extension of JPanel. I need to draw multiple custom shapes on it and be able to resize them. I finished drawing all the elements, I did the logic for selecting those elements and drawing a section rectangle, but I can't resize them. I tried using affine transformation and the scale() method, but when I scale using those methods, it scales all elements on the canvas, not just one. Any idea how could I make it scale only one element, without scaling others?

هل كانت مفيدة؟

المحلول

Assuming your painting your elements within the paintComponent method...

Create a new copy of the Graphics context...

Graphics2D g2d = (Graphics2D)g.create();

Create and apply you affine transformation...

AffineTransform at = anew AffineTransform();
at.translate(...);
at.scale(...);
g2d.setTransform(at);

Paint your element(s) and dispose of the Graphics context you have created...

 //... Paint...
 g2d.dispose();

Repeat as required.

The other method might be to get the current transformation and re-apply it when your done...

AffineTransform currenrAT = g2d.getTransform();
// Apply new transform and paint...
g2d.setTransform(currentAT);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top