/** * 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 class Foo() { private static final Color COLOR_BACKGROUND = Color.WHITE; static { /* too late ! */ System.setProperty("java.awt.headless", "true"); System.out.println(java.awt.GraphicsEnvironment.isHeadless()); /* ---> prints false */ } public static void main() {} }
public static Rectangle[] getScreenBounds() { GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment(); final GraphicsDevice[] screenDevices = graphicsEnvironment.getScreenDevices(); Rectangle[] screenBounds = new Rectangle[screenDevices.length]; for (int i = 0; i < screenDevices.length; i++) { GraphicsDevice screenDevice = screenDevices[i]; final GraphicsConfiguration defaultConfiguration = screenDevice.getDefaultConfiguration(); screenBounds[i] = defaultConfiguration.getBounds(); } return screenBounds; }
public static void registerBundledFonts() { GraphicsEnvironment grEnv = GraphicsEnvironment.getLocalGraphicsEnvironment(); if (Utils.FONT_HACK != null) { grEnv.registerFont(Utils.FONT_HACK); } }
protected String[] getFontFamilies() { if (fontFamilyNames == null) { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); fontFamilyNames = env.getAvailableFontFamilyNames(); } return fontFamilyNames; }
public static boolean hasFont(String fontName) { Font[] systemFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(); for (Font systemFont : systemFonts) { if (systemFont.getName().equalsIgnoreCase(fontName)) { return true; } } return false; }
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);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); for (int j = 0; j < gs.length; j++) { GraphicsDevice gd = gs[j]; 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])); JFrame f = new JFrame("Properties"); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.getContentPane().add(p, BorderLayout.CENTER); f.pack(); f.setMinimumSize(f.getPreferredSize()); f.setSize(600, 500);
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);
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); 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);
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()); System.out.println(Toolkit.getDefaultToolkit().getScreenSize()); JFrame frame = new JFrame("Frame Info"); frame.setSize(200, 200); 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() ); } }
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); } });
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);
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);
private int width = 450; private int height = 300; private GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); private StyledDocument doc; private MutableAttributeSet mas; jsp = new JScrollPane(jta); jsp.setPreferredSize(new Dimension(height, width)); frm = new JFrame("awesome"); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setLayout(new BorderLayout()); frm.add(jsp, BorderLayout.CENTER); frm.setLocation(100, 100); frm.pack(); frm.setVisible(true); jta.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); fnt = ge.getAvailableFontFamilyNames(); mas = jta.getInputAttributes(); new Thread(this).start();
import javax.swing.*; import java.awt.*; public class Keyboard { public static void main(String[] args) { JFrame frame = new JFrame("Keyboard"); frame.setLayout(new BorderLayout()); MyPanel displayPanel = new MyPanel(); frame.add(displayPanel, BorderLayout.CENTER); frame.addKeyListener(displayPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width, java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height); frame.setVisible(true); } }
private void initializeComponents () { getContentPane().setLayout(new GridBagLayout()); JPanel leftSidePanel = new JPanel(); leftSidePanel.setLayout(new GridBagLayout()); getContentPane().add(leftSidePanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()); fontList = new JList(); fontScroll.setViewportView(fontList); getContentPane().add(rightSidePanel, new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
private int initMonitors() { if (!SX.isHeadless()) { genv = GraphicsEnvironment.getLocalGraphicsEnvironment(); gdevs = genv.getScreenDevices(); nMonitors = gdevs.length; if (nMonitors == 0) { Rectangle currentBounds; for (int i = 0; i < nMonitors; i++) { currentBounds = new Rectangle(gdevs[i].getDefaultConfiguration().getBounds()); if (null != allMonitors) { allMonitors = allMonitors.union(currentBounds); } else { allMonitors = currentBounds; if (currentBounds.contains(new Point())) { if (mainMonitor < 0) { mainMonitor = i;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); Rectangle bounds = gc.getBounds(); JFrame f = new JFrame(); f.setUndecorated(true); f.setBounds(bounds); f.setVisible(true);