public static DisplayDeviceArea[] getPresentationAndImageDeviceAreas() { DisplayDeviceArea[] displayDeviceAreas = null; GraphicsDevice[] gs = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); if (gs.length == 1) { DisplayMode dm = gs[0].getDisplayMode(); int width = dm.getWidth(); int height = dm.getHeight(); float presentationAspectRatio = 5f / 4; // usual screen for presentations float imageAspectRatio = 3f / 4 * 2; // pair of portrait monitors float presentationHorizontalProportion = 0.33f; int presentationWidth = (int) (width * presentationHorizontalProportion); int presentationHeight = (int) (presentationWidth / presentationAspectRatio); if (presentationHeight > height) { presentationHeight = height; } int presentationX = 0; int presentationY = height - presentationHeight; int imageWidth = width - presentationWidth; int imageHeight = (int) (imageWidth / imageAspectRatio); if (imageHeight > height) { imageHeight = height; } int imageX = presentationWidth; int imageY = height - imageHeight; displayDeviceAreas = new DisplayDeviceArea[2]; displayDeviceAreas[0] = new DisplayDeviceArea( gs[0], presentationX, presentationY, presentationWidth, presentationHeight); displayDeviceAreas[1] = new DisplayDeviceArea(gs[0], imageX, imageY, imageWidth, imageHeight); } else if (gs.length == 2) { DisplayMode dm1 = gs[0].getDisplayMode(); DisplayMode dm2 = gs[1].getDisplayMode(); int width1 = dm1.getWidth(); int width2 = dm2.getWidth(); int height1 = dm1.getHeight(); int height2 = dm2.getHeight(); GraphicsDevice presentationDevice; GraphicsDevice imageDevice; if (width1 * height1 > width2 * height2) { presentationDevice = gs[1]; imageDevice = gs[0]; } else { presentationDevice = gs[0]; imageDevice = gs[1]; } displayDeviceAreas = new DisplayDeviceArea[2]; displayDeviceAreas[0] = new DisplayDeviceArea(presentationDevice); displayDeviceAreas[1] = new DisplayDeviceArea(imageDevice); } return displayDeviceAreas; }
public static void main(String[] args) { System.out.println("It is program My"); // получение списка шрифтов String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for (int i = 0; i < fontNames.length; i++) System.out.println(fontNames[i]); MyFrame frame = new MyFrame(); // условие закрытия окна frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); }
public static BufferedImage tileImage(BufferedImage im, int width, int height) { GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); int transparency = Transparency.OPAQUE; // Transparency.BITMASK; BufferedImage compatible = gc.createCompatibleImage(width, height, transparency); Graphics2D g = (Graphics2D) compatible.getGraphics(); g.setPaint(new TexturePaint(im, new Rectangle(0, 0, im.getWidth(), im.getHeight()))); g.fillRect(0, 0, width, height); return compatible; }
public void paintComponent(Graphics g) { // необходиом чтобы текст коректно отрисовывался в окне super.paintComponent(g); // рисуем текст в окне Graphics2D g2 = (Graphics2D) g; AffineTransform t = g2.getTransform(); g.drawString("It is text", 5, 5); // создание шрифта Font f = new Font("SanasSerif", Font.ITALIC, 20); g2.setFont(f); g2.drawString("It is new text", 5, 33); String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for (int i = 5; i < 20; i++) { g2.rotate(-0.05); g2.setColor( new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255))); Font f1 = new Font(fontNames[i], Font.BOLD, 20); g2.setFont(f1); g2.drawString(fontNames[i], 5, 20 * i); } // текст в центре g2.setTransform(t); // возращение к кординатам, которые запонилив начале Font f2 = new Font("SanasSerif", Font.ITALIC, 20); g2.setFont(f2); String s = "It is center!"; FontRenderContext context = g2.getFontRenderContext(); Rectangle2D r = f2.getStringBounds(s, context); double x1 = (getWidth() - r.getWidth()) / 2; double y1 = (getHeight() - r.getHeight()) / 2; double ascent = -r.getY(); // узнаем высоту текста double y2 = y1 + ascent; Rectangle2D rect = new Rectangle2D.Double(x1, y1, r.getWidth(), r.getHeight()); g2.setColor(Color.YELLOW); g2.fill(rect); g2.setColor(Color.red); g2.drawString(s, (int) x1, (int) y2); g2.setColor(Color.blue); g2.draw(new Line2D.Double(x1, y2, x1 + r.getWidth(), y2)); g2.draw(rect); }
public GraphicsConfiguration getDeviceConfiguration() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); return (gd.getDefaultConfiguration()); }