use of java.awt.GraphicsEnvironment in project jdk8u_jdk by JetBrains.
the class ServiceUI method printDialog.
/**
* Presents a dialog to the user for selecting a print service (printer).
* It is displayed at the location specified by the application and
* is modal.
* If the specification is invalid or would make the dialog not visible it
* will be displayed at a location determined by the implementation.
* The dialog blocks its calling thread and is application modal.
* <p>
* The dialog may include a tab panel with custom UI lazily obtained from
* the PrintService's ServiceUIFactory when the PrintService is browsed.
* The dialog will attempt to locate a MAIN_UIROLE first as a JComponent,
* then as a Panel. If there is no ServiceUIFactory or no matching role
* the custom tab will be empty or not visible.
* <p>
* The dialog returns the print service selected by the user if the user
* OK's the dialog and null if the user cancels the dialog.
* <p>
* An application must pass in an array of print services to browse.
* The array must be non-null and non-empty.
* Typically an application will pass in only PrintServices capable
* of printing a particular document flavor.
* <p>
* An application may pass in a PrintService to be initially displayed.
* A non-null parameter must be included in the array of browsable
* services.
* If this parameter is null a service is chosen by the implementation.
* <p>
* An application may optionally pass in the flavor to be printed.
* If this is non-null choices presented to the user can be better
* validated against those supported by the services.
* An application must pass in a PrintRequestAttributeSet for returning
* user choices.
* On calling the PrintRequestAttributeSet may be empty, or may contain
* application-specified values.
* <p>
* These are used to set the initial settings for the initially
* displayed print service. Values which are not supported by the print
* service are ignored. As the user browses print services, attributes
* and values are copied to the new display. If a user browses a
* print service which does not support a particular attribute-value, the
* default for that service is used as the new value to be copied.
* <p>
* If the user cancels the dialog, the returned attributes will not reflect
* any changes made by the user.
*
* A typical basic usage of this method may be :
* <pre>{@code
* PrintService[] services = PrintServiceLookup.lookupPrintServices(
* DocFlavor.INPUT_STREAM.JPEG, null);
* PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
* if (services.length > 0) {
* PrintService service = ServiceUI.printDialog(null, 50, 50,
* services, services[0],
* null,
* attributes);
* if (service != null) {
* ... print ...
* }
* }
* }</pre>
* <p>
* @param gc used to select screen. null means primary or default screen.
* @param x location of dialog including border in screen coordinates
* @param y location of dialog including border in screen coordinates
* @param services to be browsable, must be non-null.
* @param defaultService - initial PrintService to display.
* @param flavor - the flavor to be printed, or null.
* @param attributes on input is the initial application supplied
* preferences. This cannot be null but may be empty.
* On output the attributes reflect changes made by the user.
* @return print service selected by the user, or null if the user
* cancelled the dialog.
* @throws HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
* @throws IllegalArgumentException if services is null or empty,
* or attributes is null, or the initial PrintService is not in the
* list of browsable services.
*/
public static PrintService printDialog(GraphicsConfiguration gc, int x, int y, PrintService[] services, PrintService defaultService, DocFlavor flavor, PrintRequestAttributeSet attributes) throws HeadlessException {
int defaultIndex = -1;
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
} else if ((services == null) || (services.length == 0)) {
throw new IllegalArgumentException("services must be non-null " + "and non-empty");
} else if (attributes == null) {
throw new IllegalArgumentException("attributes must be non-null");
}
if (defaultService != null) {
for (int i = 0; i < services.length; i++) {
if (services[i].equals(defaultService)) {
defaultIndex = i;
break;
}
}
if (defaultIndex < 0) {
throw new IllegalArgumentException("services must contain " + "defaultService");
}
} else {
defaultIndex = 0;
}
// For now we set owner to null. In the future, it may be passed
// as an argument.
Window owner = null;
Rectangle gcBounds = (gc == null) ? GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds() : gc.getBounds();
ServiceDialog dialog;
if (owner instanceof Frame) {
dialog = new ServiceDialog(gc, x + gcBounds.x, y + gcBounds.y, services, defaultIndex, flavor, attributes, (Frame) owner);
} else {
dialog = new ServiceDialog(gc, x + gcBounds.x, y + gcBounds.y, services, defaultIndex, flavor, attributes, (Dialog) owner);
}
Rectangle dlgBounds = dialog.getBounds();
// get union of all GC bounds
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
gcBounds = gcBounds.union(gs[j].getDefaultConfiguration().getBounds());
}
// if portion of dialog is not within the gc boundary
if (!gcBounds.contains(dlgBounds)) {
// put in the center relative to parent frame/dialog
dialog.setLocationRelativeTo(owner);
}
dialog.show();
if (dialog.getStatus() == ServiceDialog.APPROVE) {
PrintRequestAttributeSet newas = dialog.getAttributes();
Class dstCategory = Destination.class;
Class amCategory = SunAlternateMedia.class;
Class fdCategory = Fidelity.class;
if (attributes.containsKey(dstCategory) && !newas.containsKey(dstCategory)) {
attributes.remove(dstCategory);
}
if (attributes.containsKey(amCategory) && !newas.containsKey(amCategory)) {
attributes.remove(amCategory);
}
attributes.addAll(newas);
Fidelity fd = (Fidelity) attributes.get(fdCategory);
if (fd != null) {
if (fd == Fidelity.FIDELITY_TRUE) {
removeUnsupportedAttributes(dialog.getPrintService(), flavor, attributes);
}
}
}
return dialog.getPrintService();
}
use of java.awt.GraphicsEnvironment in project jdk8u_jdk by JetBrains.
the class JMenu method getPopupMenuOrigin.
/**
* Computes the origin for the <code>JMenu</code>'s popup menu.
* This method uses Look and Feel properties named
* <code>Menu.menuPopupOffsetX</code>,
* <code>Menu.menuPopupOffsetY</code>,
* <code>Menu.submenuPopupOffsetX</code>, and
* <code>Menu.submenuPopupOffsetY</code>
* to adjust the exact location of popup.
*
* @return a <code>Point</code> in the coordinate space of the
* menu which should be used as the origin
* of the <code>JMenu</code>'s popup menu
*
* @since 1.3
*/
protected Point getPopupMenuOrigin() {
int x;
int y;
JPopupMenu pm = getPopupMenu();
// Figure out the sizes needed to caclulate the menu position
Dimension s = getSize();
Dimension pmSize = pm.getSize();
// the size has not yet been initiated
if (pmSize.width == 0) {
pmSize = pm.getPreferredSize();
}
Point position = getLocationOnScreen();
Toolkit toolkit = Toolkit.getDefaultToolkit();
GraphicsConfiguration gc = getGraphicsConfiguration();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();
for (int i = 0; i < gd.length; i++) {
if (gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
GraphicsConfiguration dgc = gd[i].getDefaultConfiguration();
if (dgc.getBounds().contains(position)) {
gc = dgc;
break;
}
}
}
if (gc != null) {
screenBounds = gc.getBounds();
// take screen insets (e.g. taskbar) into account
Insets screenInsets = toolkit.getScreenInsets(gc);
screenBounds.width -= Math.abs(screenInsets.left + screenInsets.right);
screenBounds.height -= Math.abs(screenInsets.top + screenInsets.bottom);
position.x -= Math.abs(screenInsets.left);
position.y -= Math.abs(screenInsets.top);
}
Container parent = getParent();
if (parent instanceof JPopupMenu) {
// We are a submenu (pull-right)
int xOffset = UIManager.getInt("Menu.submenuPopupOffsetX");
int yOffset = UIManager.getInt("Menu.submenuPopupOffsetY");
if (SwingUtilities.isLeftToRight(this)) {
// First determine x:
// Prefer placement to the right
x = s.width + xOffset;
if (position.x + x + pmSize.width >= screenBounds.width + screenBounds.x && // popup doesn't fit - place it wherever there's more room
screenBounds.width - s.width < 2 * (position.x - screenBounds.x)) {
x = 0 - xOffset - pmSize.width;
}
} else {
// First determine x:
// Prefer placement to the left
x = 0 - xOffset - pmSize.width;
if (position.x + x < screenBounds.x && // popup doesn't fit - place it wherever there's more room
screenBounds.width - s.width > 2 * (position.x - screenBounds.x)) {
x = s.width + xOffset;
}
}
// Then the y:
// Prefer dropping down
y = yOffset;
if (position.y + y + pmSize.height >= screenBounds.height + screenBounds.y && // popup doesn't fit - place it wherever there's more room
screenBounds.height - s.height < 2 * (position.y - screenBounds.y)) {
y = s.height - yOffset - pmSize.height;
}
} else {
// We are a toplevel menu (pull-down)
int xOffset = UIManager.getInt("Menu.menuPopupOffsetX");
int yOffset = UIManager.getInt("Menu.menuPopupOffsetY");
if (SwingUtilities.isLeftToRight(this)) {
// First determine the x:
// Extend to the right
x = xOffset;
if (position.x + x + pmSize.width >= screenBounds.width + screenBounds.x && // popup doesn't fit - place it wherever there's more room
screenBounds.width - s.width < 2 * (position.x - screenBounds.x)) {
x = s.width - xOffset - pmSize.width;
}
} else {
// First determine the x:
// Extend to the left
x = s.width - xOffset - pmSize.width;
if (position.x + x < screenBounds.x && // popup doesn't fit - place it wherever there's more room
screenBounds.width - s.width > 2 * (position.x - screenBounds.x)) {
x = xOffset;
}
}
// Then the y:
// Prefer dropping down
y = s.height + yOffset;
if (position.y + y + pmSize.height >= screenBounds.height + screenBounds.y && // popup doesn't fit - place it wherever there's more room
screenBounds.height - s.height < 2 * (position.y - screenBounds.y)) {
// Otherwise drop 'up'
y = 0 - yOffset - pmSize.height;
}
}
return new Point(x, y);
}
use of java.awt.GraphicsEnvironment in project jdk8u_jdk by JetBrains.
the class UnmanagedDrawImagePerformance method makeVI.
private static VolatileImage makeVI(final int type) {
final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice gd = ge.getDefaultScreenDevice();
final GraphicsConfiguration gc = gd.getDefaultConfiguration();
return gc.createCompatibleVolatileImage(SIZE, SIZE, type);
}
use of java.awt.GraphicsEnvironment in project jdk8u_jdk by JetBrains.
the class IncorrectDestinationOffset method main.
public static void main(final String[] args) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(SIZE, SIZE);
BufferedImage bi = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_ARGB);
for (double scale : SCALES) {
while (true) {
// initialize Volatile Image
vi.validate(gc);
Graphics2D g2d = vi.createGraphics();
g2d.setColor(Color.green);
g2d.fillRect(0, 0, SIZE, SIZE);
g2d.dispose();
if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
continue;
}
// Draw the VolatileImage to BI with scale and offsets
Graphics2D g = bi.createGraphics();
g.setComposite(AlphaComposite.Src);
g.setColor(Color.RED);
g.fillRect(0, 0, SIZE / 2, SIZE / 2);
g.setColor(Color.BLUE);
g.fillRect(SIZE / 2, 0, SIZE / 2, SIZE / 2);
g.setColor(Color.ORANGE);
g.fillRect(0, SIZE / 2, SIZE / 2, SIZE / 2);
g.setColor(Color.MAGENTA);
g.fillRect(SIZE / 2, SIZE / 2, SIZE / 2, SIZE / 2);
int point2draw = (int) (100 * scale);
int size2draw = (int) (SIZE * scale);
g.drawImage(vi, point2draw, point2draw, size2draw, size2draw, null);
g.dispose();
if (vi.contentsLost()) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
continue;
}
validate(bi, point2draw, size2draw);
break;
}
}
}
use of java.awt.GraphicsEnvironment in project jdk8u_jdk by JetBrains.
the class IncorrectOffset method main.
public static void main(final String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(width, height);
BufferedImage bi = new BufferedImage(width / 4, height / 4, BufferedImage.TYPE_INT_ARGB);
while (true) {
vi.validate(gc);
Graphics2D g2d = vi.createGraphics();
g2d.setColor(Color.black);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.green);
g2d.fillRect(width / 4, height / 4, width / 2, height / 2);
g2d.dispose();
if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
continue;
}
Graphics2D g = bi.createGraphics();
g.setComposite(AlphaComposite.Src);
// Scale part of VI to BI. Only green area should be copied.
g.drawImage(vi, 0, 0, width / 4, height / 4, width / 4, height / 4, width / 4 + width / 2, height / 4 + height / 2, null);
g.dispose();
if (vi.contentsLost()) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
continue;
}
for (int x = 0; x < width / 4; ++x) {
for (int y = 0; y < height / 4; ++y) {
if (bi.getRGB(x, y) != Color.green.getRGB()) {
throw new RuntimeException("Test failed.");
}
}
}
break;
}
}
Aggregations