10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.java2d;
27
28 import java.awt.AWTError;
29 import java.awt.Color;
30 import java.awt.Font;
31 import java.awt.Graphics2D;
32 import java.awt.GraphicsConfiguration;
33 import java.awt.GraphicsDevice;
34 import java.awt.GraphicsEnvironment;
35 import java.awt.Insets;
36 import java.awt.Point;
37 import java.awt.Rectangle;
38 import java.awt.Toolkit;
39 import java.awt.geom.AffineTransform;
40 import java.awt.image.BufferedImage;
41 import java.awt.peer.ComponentPeer;
42 import java.security.AccessController;
43 import java.util.Locale;
44 import java.util.TreeMap;
45
46 import sun.awt.DisplayChangedListener;
47 import sun.awt.SunDisplayChanger;
48 import sun.font.FontManager;
49 import sun.font.FontManagerFactory;
332 * @param x the x coordinate of the given point
333 * @param y the y coordinate of the given point
334 * @return the graphics configuration
335 */
336 public static GraphicsConfiguration getGraphicsConfigurationAtPoint(
337 GraphicsConfiguration current, double x, double y) {
338 if (current.getBounds().contains(x, y)) {
339 return current;
340 }
341 GraphicsEnvironment env = getLocalGraphicsEnvironment();
342 for (GraphicsDevice device : env.getScreenDevices()) {
343 GraphicsConfiguration config = device.getDefaultConfiguration();
344 if (config.getBounds().contains(x, y)) {
345 return config;
346 }
347 }
348 return current;
349 }
350
351 /**
352 * Converts coordinates from the user's space to the device space using
353 * appropriate device transformation.
354 *
355 * @param x coordinate in the user space
356 * @param y coordinate in the user space
357 * @return the point which uses device space(pixels)
358 */
359 public static Point convertToDeviceSpace(double x, double y) {
360 GraphicsConfiguration gc = getLocalGraphicsEnvironment()
361 .getDefaultScreenDevice().getDefaultConfiguration();
362 gc = getGraphicsConfigurationAtPoint(gc, x, y);
363
364 AffineTransform tx = gc.getDefaultTransform();
365 x = Region.clipRound(x * tx.getScaleX());
366 y = Region.clipRound(y * tx.getScaleY());
367 return new Point((int) x, (int) y);
368 }
369
370 /**
371 * Converts bounds from the user's space to the device space using
372 * appropriate device transformation.
373 *
374 * @param bounds the rectangle in the user space
375 * @return the rectangle which uses device space(pixels)
376 */
377 public static Rectangle convertToDeviceSpace(Rectangle bounds) {
378 GraphicsConfiguration gc = getLocalGraphicsEnvironment()
379 .getDefaultScreenDevice().getDefaultConfiguration();
380 gc = getGraphicsConfigurationAtPoint(gc, bounds.x, bounds.y);
381 return convertToDeviceSpace(gc, bounds);
382 }
383
384 /**
385 * Converts bounds from the user's space to the device space using
386 * appropriate device transformation of the passed graphics configuration.
387 *
388 * @param bounds the rectangle in the user space
389 * @return the rectangle which uses device space(pixels)
390 */
391 public static Rectangle convertToDeviceSpace(GraphicsConfiguration gc,
392 Rectangle bounds) {
393 AffineTransform tx = gc.getDefaultTransform();
394 return new Rectangle(
395 Region.clipRound(bounds.x * tx.getScaleX()),
396 Region.clipRound(bounds.y * tx.getScaleY()),
397 Region.clipRound(bounds.width * tx.getScaleX()),
398 Region.clipRound(bounds.height * tx.getScaleY())
399 );
400 }
401 }
|
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.java2d;
27
28 import java.awt.AWTError;
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.Font;
32 import java.awt.Graphics2D;
33 import java.awt.GraphicsConfiguration;
34 import java.awt.GraphicsDevice;
35 import java.awt.GraphicsEnvironment;
36 import java.awt.Insets;
37 import java.awt.Point;
38 import java.awt.Rectangle;
39 import java.awt.Toolkit;
40 import java.awt.geom.AffineTransform;
41 import java.awt.image.BufferedImage;
42 import java.awt.peer.ComponentPeer;
43 import java.security.AccessController;
44 import java.util.Locale;
45 import java.util.TreeMap;
46
47 import sun.awt.DisplayChangedListener;
48 import sun.awt.SunDisplayChanger;
49 import sun.font.FontManager;
50 import sun.font.FontManagerFactory;
333 * @param x the x coordinate of the given point
334 * @param y the y coordinate of the given point
335 * @return the graphics configuration
336 */
337 public static GraphicsConfiguration getGraphicsConfigurationAtPoint(
338 GraphicsConfiguration current, double x, double y) {
339 if (current.getBounds().contains(x, y)) {
340 return current;
341 }
342 GraphicsEnvironment env = getLocalGraphicsEnvironment();
343 for (GraphicsDevice device : env.getScreenDevices()) {
344 GraphicsConfiguration config = device.getDefaultConfiguration();
345 if (config.getBounds().contains(x, y)) {
346 return config;
347 }
348 }
349 return current;
350 }
351
352 /**
353 * Returns the bounds of the graphics configuration in device space.
354 *
355 * @param config the graphics configuration which bounds are requested
356 * @return the bounds of the area covered by this
357 * {@code GraphicsConfiguration} in device space(pixels).
358 */
359 public static Rectangle getGCDeviceBounds(GraphicsConfiguration config) {
360 AffineTransform tx = config.getDefaultTransform();
361 Rectangle bounds = config.getBounds();
362 bounds.width *= tx.getScaleX();
363 bounds.height *= tx.getScaleY();
364 return bounds;
365 }
366
367 /**
368 * Converts the size(w,h) from the device space to the user's space using
369 * passed graphics configuration.
370 *
371 * @param gc the graphics configuration to be used for transformation
372 * @param w the width in the device space
373 * @param h the height in the device space
374 * @return the size in the user's space
375 */
376 public static Dimension toUserSpace(GraphicsConfiguration gc,
377 int w, int h) {
378 AffineTransform tx = gc.getDefaultTransform();
379 return new Dimension(
380 Region.clipRound(w / tx.getScaleX()),
381 Region.clipRound(h / tx.getScaleY())
382 );
383 }
384
385 /**
386 * Converts absolute coordinates from the user's space to the device space
387 * using appropriate device transformation.
388 *
389 * @param x absolute coordinate in the user's space
390 * @param y absolute coordinate in the user's space
391 * @return the point which uses device space(pixels)
392 */
393 public static Point toDeviceSpaceAbs(int x, int y) {
394 GraphicsConfiguration gc = getLocalGraphicsEnvironment()
395 .getDefaultScreenDevice().getDefaultConfiguration();
396 gc = getGraphicsConfigurationAtPoint(gc, x, y);
397 return toDeviceSpaceAbs(gc, x, y, 0, 0).getLocation();
398 }
399
400 /**
401 * Converts the rectangle from the user's space to the device space using
402 * appropriate device transformation.
403 *
404 * @param rect the rectangle in the user's space
405 * @return the rectangle which uses device space(pixels)
406 */
407 public static Rectangle toDeviceSpaceAbs(Rectangle rect) {
408 GraphicsConfiguration gc = getLocalGraphicsEnvironment()
409 .getDefaultScreenDevice().getDefaultConfiguration();
410 gc = getGraphicsConfigurationAtPoint(gc, rect.x, rect.y);
411 return toDeviceSpaceAbs(gc, rect.x, rect.y, rect.width, rect.height);
412 }
413
414 /**
415 * Converts absolute coordinates(x,y) and the size(w,h) from the user's
416 * space to the device space using passed graphics configuration.
417 *
418 * @param gc the graphics configuration to be used for transformation
419 * @param x absolute coordinate in the user's space
420 * @param y absolute coordinate in the user's space
421 * @param w the width in the user's space
422 * @param h the height in the user's space
423 * @return the rectangle which uses device space(pixels)
424 */
425 public static Rectangle toDeviceSpaceAbs(GraphicsConfiguration gc,
426 int x, int y, int w, int h) {
427 AffineTransform tx = gc.getDefaultTransform();
428 Rectangle screen = gc.getBounds();
429 return new Rectangle(
430 screen.x + Region.clipRound((x - screen.x) * tx.getScaleX()),
431 screen.y + Region.clipRound((y - screen.y) * tx.getScaleY()),
432 Region.clipRound(w * tx.getScaleX()),
433 Region.clipRound(h * tx.getScaleY())
434 );
435 }
436
437 /**
438 * Converts absolute coordinates from the user's space to the device space
439 * using appropriate device transformation.
440 *
441 * @param x absolute coordinate in the user's space
442 * @param y absolute coordinate in the user's space
443 * @return the point which uses device space(pixels)
444 */
445 public static Point toDeviceSpace(int x, int y) {
446 GraphicsConfiguration gc = getLocalGraphicsEnvironment()
447 .getDefaultScreenDevice().getDefaultConfiguration();
448 gc = getGraphicsConfigurationAtPoint(gc, x, y);
449 return toDeviceSpace(gc, x, y, 0, 0).getLocation();
450 }
451
452 /**
453 * Converts coordinates(x,y) and the size(w,h) from the user's
454 * space to the device space using passed graphics configuration.
455 *
456 * @param gc the graphics configuration to be used for transformation
457 * @param x coordinate in the user's space
458 * @param y coordinate in the user's space
459 * @param w the width in the user's space
460 * @param h the height in the user's space
461 * @return the rectangle which uses device space(pixels)
462 */
463 public static Rectangle toDeviceSpace(GraphicsConfiguration gc,
464 int x, int y, int w, int h) {
465 AffineTransform tx = gc.getDefaultTransform();
466 return new Rectangle(
467 Region.clipRound(x * tx.getScaleX()),
468 Region.clipRound(y * tx.getScaleY()),
469 Region.clipRound(w * tx.getScaleX()),
470 Region.clipRound(h * tx.getScaleY())
471 );
472 }
473 }
|