private BufferedImage scale(BufferedImage source,double ratio) { int w = (int) (source.getWidth() * ratio); int h = (int) (source.getHeight() * ratio); BufferedImage bi = getCompatibleImage(w, h); Graphics2D g2d = bi.createGraphics(); double xScale = (double) w / source.getWidth(); double yScale = (double) h / source.getHeight(); AffineTransform at = AffineTransform.getScaleInstance(xScale,yScale); g2d.drawRenderedImage(source, at); g2d.dispose(); return bi; } private BufferedImage getCompatibleImage(int w, int h) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); BufferedImage image = gc.createCompatibleImage(w, h); return image; }
public void setLocationAndPosition() { if (this.settings.loadWindowPos(this)) { return; } GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); DisplayMode mode = gd.getDisplayMode(); int w = mode.getWidth(); int h = mode.getHeight(); setLocation((int) (w * BORDER_RATIO), (int) (h * BORDER_RATIO)); setSize((int) (w * WINDOW_RATIO), (int) (h * WINDOW_RATIO)); }
final JFrame frame = new JFrame("Display Mode"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setUndecorated(true); @Override public void actionPerformed(ActionEvent e) { device.setFullScreenWindow(frame); @Override public void actionPerformed(ActionEvent e) { device.setFullScreenWindow(null);
@Override public void windowClosing(WindowEvent evt) { // If required, restore the previous display mode if (isDisplayModeModified) { gd.setDisplayMode(previousDisplayMode); } // If required, get back to the windowed mode if (gd.getFullScreenWindow() == frame) { gd.setFullScreenWindow(null); } windowCloseRequest.set(true); } @Override
public static DisplayMode[] getDisplayModes () { GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice device = genv.getDefaultScreenDevice(); java.awt.DisplayMode desktopMode = device.getDisplayMode(); java.awt.DisplayMode[] displayModes = device.getDisplayModes(); ArrayList<DisplayMode> modes = new ArrayList<DisplayMode>(); int idx = 0; for (java.awt.DisplayMode mode : displayModes) { boolean duplicate = false; for (int i = 0; i < modes.size(); i++) { if (modes.get(i).width == mode.getWidth() && modes.get(i).height == mode.getHeight() && modes.get(i).bitsPerPixel == mode.getBitDepth()) { duplicate = true; break; } } if (duplicate) continue; if (mode.getBitDepth() != desktopMode.getBitDepth()) continue; modes.add(new LwjglApplicationConfigurationDisplayMode(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode .getBitDepth())); } return modes.toArray(new DisplayMode[modes.size()]); } }
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); for (int j = 0; j < gs.length; j++) { GraphicsDevice gd = gs[j]; gd.toString() + " " + gd.getIDstring())); GraphicsConfiguration[] gc = gd.getConfigurations(); for (int i = 0; i < gc.length; i++) { sb.append( getOutputWidgetForContent(sb.toString())); String[] fonts = ge.getAvailableFontFamilyNames(); JPanel fontTable = new JPanel(new BorderLayout(3, 1)); JPanel fontNameCol = new JPanel(new GridLayout(0, 1, 2, 2)); JPanel fontExampleCol = new JPanel(new GridLayout(0, 1, 2, 2)); fontTable.add(fontNameCol, BorderLayout.WEST); fontTable.add(fontExampleCol, BorderLayout.CENTER); for (int ii = 0; ii < fonts.length; ii++) { fontNameCol.add(new JLabel(fonts[ii]));
final JFrame jframe = new JFrame("Focus-O-Meter"); jframe.addWindowListener(new WindowAdapter() GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); int width = gd.getDisplayMode().getWidth(); int height = gd.getDisplayMode().getHeight(); jframe.getContentPane().add(glcanvas, BorderLayout.CENTER); jframe.setSize(100, height); jframe.setLocation(width - 100, 0);
private static BufferedImage ALTERED; private static final GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); ORIGINAL = ImageIO.read( ImageManipulationDemo.class.getResource("../resources/whitefro1.jpg")); ALTERED = config.createCompatibleImage( ORIGINAL.getWidth(), ORIGINAL.getHeight() + 20); Graphics2D g2 = ALTERED.createGraphics(); g2.setColor(Color.WHITE); g2.fillRect(0, 0, ALTERED.getWidth(), 20); final JFrame frame = new JFrame("Image Manipulation Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setBackground(Color.BLUE.darker()); frame.getContentPane().setLayout(new FlowLayout()); frame.getContentPane().add(new JLabel(new ImageIcon(ORIGINAL))); frame.getContentPane().add(new JLabel(new ImageIcon(ALTERED))); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
private static final int FRAMES = 24; private final Timer timer = new Timer(20, this); private final Rectangle rect = new Rectangle(); private BufferedImage background; private int index; double theta = 2 * Math.PI * index++ / 64; g.setColor(Color.blue); rect.setRect( (int) (Math.sin(theta) * w / 3 + w / 2 - RADIUS), (int) (Math.cos(theta) * h / 3 + h / 2 - RADIUS), Dimension d = field.getPreferredSize(); field.setBounds(e.getX(), e.getY(), d.width, d.height); add(field); GraphicsEnvironment.getLocalGraphicsEnvironment(); private final GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); private final Random r = new Random(); int w = getWidth(); int h = getHeight(); background = gc.createCompatibleImage(w, h, Transparency.OPAQUE); Graphics2D g = background.createGraphics(); g.clearRect(0, 0, w, h); g.setColor(Color.green.darker());
import java.awt.*; import javax.swing.*; public class FrameInfo { public static void main(String[] args) { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); Rectangle bounds = env.getMaximumWindowBounds(); System.out.println("Screen Bounds: " + bounds ); GraphicsDevice screen = env.getDefaultScreenDevice(); GraphicsConfiguration config = screen.getDefaultConfiguration(); System.out.println("Screen Size : " + config.getBounds()); JFrame frame = new JFrame("Frame Info"); System.out.println("Frame Insets : " + frame.getInsets() ); frame.setSize(200, 200); System.out.println("Frame Insets : " + frame.getInsets() ); frame.setVisible( true ); System.out.println("Frame Size : " + frame.getSize() ); System.out.println("Frame Insets : " + frame.getInsets() ); System.out.println("Content Size : " + frame.getContentPane().getSize() ); } }
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try { GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); } catch (HeadlessException e) { type = BufferedImage.TYPE_INT_ARGB; bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); Graphics g = bimage.createGraphics(); return bimage.getColorModel().hasAlpha();
public static Rectangle getPrimaryMonitorScreenRect() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gds = ge.getScreenDevices(); return gds[0].getConfigurations()[0].getBounds(); }
public static void main(String[] args) { GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); GraphicsDevice[] screenDevices = ge.getScreenDevices(); for (GraphicsDevice gd : screenDevices) { System.out.println(gd + (gd == ge.getDefaultScreenDevice() ? " (default)" : "")); GraphicsConfiguration[] gc = gd.getConfigurations(); // Might want to loop to find largets or bests, instead of taking first one GraphicsConfiguration g = gc[0]; JFrame jf = new JFrame(gd.getDefaultConfiguration()); Canvas c = new Canvas(g); Rectangle gcBounds = g.getBounds(); System.out.println("SIZE: " + gcBounds); jf.getContentPane().add(c); jf.setSize(gcBounds.width, gcBounds.height); jf.setVisible(true); } } }
private int scale = 1; private GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); return config.createCompatibleImage(width, height, alpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE); frame = new JFrame(); frame.addWindowListener(new FrameClose()); frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); frame.setSize(width * scale, height * scale); frame.setVisible(true); frame.add(canvas, 0); backgroundGraphics = (Graphics2D) background.getGraphics(); long fpsWait = (long) (1.0 / 30 * 1000); main: while (isRunning) {
/** * Helps client code place components on the center of the screen. It * handles multiple monitor configuration correctly * * @param gconf the GraphicsConfiguration of the monitor * @param componentSize the size of the component * @return bounds of the centered component */ private static Rectangle findCenterBounds(GraphicsConfiguration gconf, Dimension componentSize) { if (gconf == null) { gconf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); } Rectangle bounds = gconf.getBounds(); return new Rectangle( bounds.x + ((bounds.width - componentSize.width) / 2), bounds.y + ((bounds.height - componentSize.height) / 2), componentSize.width, componentSize.height ); }
public void run() { JFrame frame = new JFrame(pTitle); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationByPlatform(true); JPanel pane = new JPanel(new BorderLayout()); GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); BufferedImageIcon icon = new BufferedImageIcon(ImageUtil.accelerate(pImage, gc)); JScrollPane scroll = new JScrollPane(new JLabel(icon)); scroll.setBorder(null); pane.add(scroll); frame.setContentPane(pane); frame.pack(); frame.setVisible(true); } });
final JFrame frame = new JFrame(); frame.setTitle("Frame"); frame.setSize(800, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); device.setFullScreenWindow(frame); device.setDisplayMode(new DisplayMode(800, 600, 32, 60)); frame.setVisible(true); JPanel panel = new JPanel(); panel.add(btn); frame.add(panel); public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(frame.getContentPane(), "JOptionPane");
JFrame f = new JFrame("LowerRightFrame"); f.add(new JPanel() { f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice defaultScreen = ge.getDefaultScreenDevice(); Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds(); int x = (int) rect.getMaxX() - f.getWidth(); int y = (int) rect.getMaxY() - f.getHeight(); f.setLocation(x, y); f.setVisible(true);
private JFrame f = new JFrame("FullScreenTest"); private Action exit = new AbstractAction(EXIT) { this.add(b); f.getRootPane().setDefaultButton(b); this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), EXIT); this.getActionMap().put(EXIT, exit); GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice dev = env.getDefaultScreenDevice(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setBackground(Color.darkGray); f.setResizable(false); f.setUndecorated(true); f.add(this); f.pack(); dev.setFullScreenWindow(f);
public BackgroundTiles(Gpu gpu) { super(); gfxConfig = GraphicsEnvironment. getLocalGraphicsEnvironment().getDefaultScreenDevice(). getDefaultConfiguration(); img = gfxConfig.createCompatibleImage(272 * 2, 272 * 2); this.gpu = gpu; }