Package: ScaleController
ScaleController
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ScaleController(EditableImageRenderer) |
|
|
|
|
|
||||||||||||||||||||
fitToView() |
|
|
|
|
|
||||||||||||||||||||
fitToView(double) |
|
|
|
|
|
||||||||||||||||||||
getImageRenderer() |
|
|
|
|
|
||||||||||||||||||||
getScale() |
|
|
|
|
|
||||||||||||||||||||
getZoomFactor() |
|
|
|
|
|
||||||||||||||||||||
setScale(double) |
|
|
|
|
|
||||||||||||||||||||
setScale(double, Point) |
|
|
|
|
|
||||||||||||||||||||
setZoomFactor(double) |
|
|
|
|
|
||||||||||||||||||||
showActualPixels() |
|
|
|
|
|
||||||||||||||||||||
zoomIn() |
|
|
|
|
|
||||||||||||||||||||
zoomOut() |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.image.render;
28:
29: import java.awt.Point;
30:
31: /***********************************************************************************************************************
32: *
33: * The <code>ScaleController</code> makes it possible to control the scale of
34: * the image in an <code>EditableImageRenderer</code>. In order to be used,
35: * you just have to create a new instance passing the
36: * <code>EditableImageRenderer</code> as a parameter:
37: *
38: * <pre>
39: * EditableImageRenderer renderer = ...;
40: * ScaleController scaleController = new ScaleController(renderer);
41: * ...
42: * scaleController.setScale(2.5);
43: * </pre>
44: *
45: * @author Fabrizio Giudici
46: *
47: **********************************************************************************************************************/
48: public class ScaleController
49: {
50: /**
51: * The attached renderer.
52: */
53: protected final EditableImageRenderer imageRenderer;
54:
55: /**
56: * The zoom factor used by zoomIn() and zoomOut().
57: */
58: private double zoomFactor = 1.5;
59:
60: /*******************************************************************************************************************
61: *
62: * Creates a new instance of this class, attached to the given renderer.
63: *
64: * @param imageRenderer the image renderer
65: *
66: ******************************************************************************************************************/
67: public ScaleController (final EditableImageRenderer imageRenderer)
68: {
69:• if (imageRenderer == null)
70: {
71: throw new IllegalArgumentException("imageRenderer is mandatory");
72: }
73:
74: this.imageRenderer = imageRenderer;
75: }
76:
77: /*******************************************************************************************************************
78: *
79: * Sets the scale.
80: *
81: * @param scale the new scale
82: *
83: ******************************************************************************************************************/
84: public final void setScale (final double scale)
85: {
86: setScale(scale, null);
87: }
88:
89: /*******************************************************************************************************************
90: *
91: * Sets the scale using a pivot point. A null pivot means that the pivot
92: * will be placed on the center of the image.
93: *
94: * @param scale the new scale
95: * @param pivot the pivot point (null means the center of the image)
96: *
97: ******************************************************************************************************************/
98: public void setScale (final double scale, final Point pivot)
99: {
100: imageRenderer.setScale(scale, pivot);
101: }
102:
103: /*******************************************************************************************************************
104: *
105: * Returns the current scale.
106: *
107: * @return the current scale
108: *
109: ******************************************************************************************************************/
110: public double getScale()
111: {
112: return imageRenderer.getScale();
113: }
114:
115: /*******************************************************************************************************************
116: *
117: * Sets the zoom factor that is used by <code>zoomIn()</code> and
118: * <code>zoomOut</code>.
119: *
120: * @param zoomFactor the new zoom factor
121: *
122: ******************************************************************************************************************/
123: public void setZoomFactor (final double zoomFactor)
124: {
125: this.zoomFactor = zoomFactor;
126: }
127:
128: /*******************************************************************************************************************
129: *
130: * Returns the current zoom factor.
131: *
132: * @return the currnet zoom factor
133: *
134: ******************************************************************************************************************/
135: public double getZoomFactor()
136: {
137: return zoomFactor;
138: }
139:
140: /*******************************************************************************************************************
141: *
142: * Fits the image into the current view: the image is resized so it's not
143: * clipped, and it's centered on the screen.
144: *
145: ******************************************************************************************************************/
146: public void fitToView()
147: {
148: fitToView(1.0);
149: }
150:
151: public void fitToView (final double factor)
152: {
153: imageRenderer.setRepaintEnabled(false);
154: imageRenderer.centerImage();
155: setScale(imageRenderer.getFitScale() * factor);
156: imageRenderer.setRepaintEnabled(true);
157: imageRenderer.repaint();
158: }
159:
160: /*******************************************************************************************************************
161: *
162: * Zooms out.
163: *
164: ******************************************************************************************************************/
165: public void zoomOut()
166: {
167: setScale(imageRenderer.getScale() / zoomFactor);
168: }
169:
170: /*******************************************************************************************************************
171: *
172: * Zooms in.
173: *
174: ******************************************************************************************************************/
175: public void zoomIn()
176: {
177: setScale(imageRenderer.getScale() * zoomFactor);
178: }
179:
180: /*******************************************************************************************************************
181: *
182: * Sets the scale to 1:1.
183: *
184: ******************************************************************************************************************/
185: public void showActualPixels()
186: {
187: setScale(1.0);
188: }
189:
190: /*******************************************************************************************************************
191: *
192: * @return the image renderer
193: *
194: ******************************************************************************************************************/
195: protected EditableImageRenderer getImageRenderer()
196: {
197: return imageRenderer;
198: }
199: }