1 /*
2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
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 java.awt;
27 
28 import java.lang.annotation.Native;
29 
30 /**
31  * The {@code DisplayMode} class encapsulates the bit depth, height,
32  * width, and refresh rate of a {@code GraphicsDevice}. The ability to
33  * change graphics device's display mode is platform- and
34  * configuration-dependent and may not always be available
35  * (see {@link GraphicsDevice#isDisplayChangeSupported}).
36  * <p>
37  * For more information on full-screen exclusive mode API, see the
38  * <a href="https://docs.oracle.com/javase/tutorial/extra/fullscreen/index.html">
39  * Full-Screen Exclusive Mode API Tutorial</a>.
40  *
41  * @see GraphicsDevice
42  * @see GraphicsDevice#isDisplayChangeSupported
43  * @see GraphicsDevice#getDisplayModes
44  * @see GraphicsDevice#setDisplayMode
45  * @author Michael Martak
46  * @since 1.4
47  */
48 
49 public final class DisplayMode {
50 
51     private Dimension size;
52     private int bitDepth;
53     private int refreshRate;
54 
55     /**
56      * Create a new display mode object with the supplied parameters.
57      * @param width the width of the display, in pixels
58      * @param height the height of the display, in pixels
59      * @param bitDepth the bit depth of the display, in bits per
60      *        pixel.  This can be {@code BIT_DEPTH_MULTI} if multiple
61      *        bit depths are available.
62      * @param refreshRate the refresh rate of the display, in hertz.
63      *        This can be {@code REFRESH_RATE_UNKNOWN} if the
64      *        information is not available.
65      * @see #BIT_DEPTH_MULTI
66      * @see #REFRESH_RATE_UNKNOWN
67      */
DisplayMode(int width, int height, int bitDepth, int refreshRate)68     public DisplayMode(int width, int height, int bitDepth, int refreshRate) {
69         this.size = new Dimension(width, height);
70         this.bitDepth = bitDepth;
71         this.refreshRate = refreshRate;
72     }
73 
74     /**
75      * Returns the height of the display, in pixels.
76      * @return the height of the display, in pixels
77      */
getHeight()78     public int getHeight() {
79         return size.height;
80     }
81 
82     /**
83      * Returns the width of the display, in pixels.
84      * @return the width of the display, in pixels
85      */
getWidth()86     public int getWidth() {
87         return size.width;
88     }
89 
90     /**
91      * Value of the bit depth if multiple bit depths are supported in this
92      * display mode.
93      * @see #getBitDepth
94      */
95     @Native public static final int BIT_DEPTH_MULTI = -1;
96 
97     /**
98      * Returns the bit depth of the display, in bits per pixel.  This may be
99      * {@code BIT_DEPTH_MULTI} if multiple bit depths are supported in
100      * this display mode.
101      *
102      * @return the bit depth of the display, in bits per pixel.
103      * @see #BIT_DEPTH_MULTI
104      */
getBitDepth()105     public int getBitDepth() {
106         return bitDepth;
107     }
108 
109     /**
110      * Value of the refresh rate if not known.
111      * @see #getRefreshRate
112      */
113     @Native public static final int REFRESH_RATE_UNKNOWN = 0;
114 
115     /**
116      * Returns the refresh rate of the display, in hertz.  This may be
117      * {@code REFRESH_RATE_UNKNOWN} if the information is not available.
118      *
119      * @return the refresh rate of the display, in hertz.
120      * @see #REFRESH_RATE_UNKNOWN
121      */
getRefreshRate()122     public int getRefreshRate() {
123         return refreshRate;
124     }
125 
126     /**
127      * Returns whether the two display modes are equal.
128      *
129      * @param  dm the display mode to compare to
130      * @return whether the two display modes are equal
131      */
equals(DisplayMode dm)132     public boolean equals(DisplayMode dm) {
133         if (dm == null) {
134             return false;
135         }
136         return (getHeight() == dm.getHeight()
137             && getWidth() == dm.getWidth()
138             && getBitDepth() == dm.getBitDepth()
139             && getRefreshRate() == dm.getRefreshRate());
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     @Override
equals(Object dm)146     public boolean equals(Object dm) {
147         if (dm instanceof DisplayMode) {
148             return equals((DisplayMode)dm);
149         } else {
150             return false;
151         }
152     }
153 
154     /**
155      * {@inheritDoc}
156      */
157     @Override
hashCode()158     public int hashCode() {
159         return getWidth() + getHeight() + getBitDepth() * 7
160             + getRefreshRate() * 13;
161     }
162 
163     /**
164      * {@inheritDoc}
165      */
166     @Override
toString()167     public String toString() {
168         return getWidth() + "x" + getHeight() + "x" +
169                (getBitDepth() > 0 ? getBitDepth() + "bpp": "[Multi depth]")
170                + "@" + (getRefreshRate() > 0 ? getRefreshRate() + "Hz" :
171                "[Unknown refresh rate]");
172     }
173 }
174