/** * Create possibly volatile scratch image for fast painting. A scratch image can become * invalidated, when this happens any actions involving it are ignored, and it needs to be * recreated. Use isScratchImageValid() to check this. */ public static Image createScratchImage(int width, int height) { try { Image img = (Image) tryMethod( output_comp, "createVolatileImage", new Object[] {new Integer(width), new Integer(height)}); if (img == null) { // no such method -> create regular image return output_comp.createImage(width, height); } // if (img.validate(output_comp.getGraphicsConfiguration()) // == VolatileImage.IMAGE_INCOMPATIBLE) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); Integer valid = (Integer) tryMethod(img, "validate", new Object[] {gc}); // output_comp.getGraphicsConfiguration() }); if (valid.intValue() == 2) { // I checked, IMAGE_INCOMPATIBLE=2 // Hmm, somehow it didn't work. Create regular image. return output_comp.createImage(width, height); } return img; } catch (java.security.AccessControlException e) { // we're not allowed to do this (we're probably an applet) return output_comp.createImage(width, height); } }
/** Create empty image with given alpha mode that should be efficient on this display */ public static BufferedImage createCompatibleImage(int width, int height, int transparency) { // try { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); // always use bitmask transparency BufferedImage bimage = gc.createCompatibleImage(width, height, transparency); return bimage; // } catch (HeadlessException e) { // this exception is not in 1.2 // The system does not have a screen // e.printStackTrace(); // return null; // } }
public Point getPreferredLocation() { if (!IJ.isJava14()) return new Point(0, 0); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Rectangle maxBounds = ge.getMaximumWindowBounds(); int ijX = Prefs.getInt(IJ_X, -99); int ijY = Prefs.getInt(IJ_Y, -99); if (ijX >= 0 && ijY > 0 && ijX < (maxBounds.x + maxBounds.width - 75)) return new Point(ijX, ijY); Dimension tbsize = toolbar.getPreferredSize(); int ijWidth = tbsize.width + 10; double percent = maxBounds.width > 832 ? 0.8 : 0.9; ijX = (int) (percent * (maxBounds.width - ijWidth)); if (ijX < 10) ijX = 10; return new Point(ijX, maxBounds.y); }
// This method returns a buffered image with the contents of an image // Source: http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html public static BufferedImage toBufferedImage(Image image) { if (image instanceof BufferedImage) { return (BufferedImage) image; } // This code ensures that all the pixels in the image are loaded image = new ImageIcon(image).getImage(); // Determine if the image has transparent pixels; for this method's // implementation, see Determining If an Image Has Transparent Pixels boolean hasAlpha = hasAlpha(image); // Create a buffered image with a format that's compatible with the screen BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try { // Determine the type of transparency of the new buffered image int transparency = Transparency.OPAQUE; if (hasAlpha) { transparency = Transparency.BITMASK; } // Create the buffered image GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); } catch (HeadlessException e) { // The system does not have a screen } if (bimage == null) { // Create a buffered image using the default color model int type = BufferedImage.TYPE_INT_RGB; if (hasAlpha) { type = BufferedImage.TYPE_INT_ARGB; } bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); } // Copy image to buffered image Graphics g = bimage.createGraphics(); // Paint the image onto the buffered image g.drawImage(image, 0, 0, null); g.dispose(); return bimage; }
public void generarFondo(Component componente) { boolean dibujarFondo = false; Rectangle med = this.getBounds(); Rectangle areaDibujo = this.getBounds(); BufferedImage tmp; GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); if (Principal.fondoBlur) { dibujarFondo = true; } if (dibujarFondo) { JRootPane root = SwingUtilities.getRootPane(this); blurBuffer = GraphicsUtilities.createCompatibleImage(Principal.sysAncho, Principal.sysAlto); Graphics2D g2 = blurBuffer.createGraphics(); g2.setClip(med); blurBuffer = blurBuffer.getSubimage(med.x, med.y, med.width, med.height); ((Escritorio) Principal.getEscritorio()).getFrameEscritorio().paint(g2); g2.dispose(); backBuffer = blurBuffer; // blurBuffer = toGrayScale(blurBuffer); blurBuffer = GraphicsUtilities.createThumbnailFast(blurBuffer, getWidth() / 2); blurBuffer = new GaussianBlurFilter(4).filter(blurBuffer, null); g2 = (Graphics2D) blurBuffer.getGraphics(); g2.setColor(new Color(0, 0, 0, 195)); g2.fillRect(0, 0, Principal.sysAncho, Principal.sysAlto); listo = true; } }
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 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 GamePanel() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); // Create an image that does not support transparency backBufferImage = gc.createCompatibleImage( TopDownShooter_Main.screenWidth, TopDownShooter_Main.screenHeight, Transparency.OPAQUE); backBuffer = backBufferImage.getGraphics(); player = new Player(new Point(750, 750)); map = Map.getInstance(); map.loadWorld("World 1"); viewScreen = ViewScreen.getInstance(); collisionManager = CollisionManager.getInstance(); SwingUtilities.invokeLater( new Runnable() { // The GUI needs to be added to the JPanel at a later time, since there is // no way to know the size of this panel yet. public void run() { viewScreen.setSize( new Rectangle( new Point(0, 0), new Dimension( GamePanel.this.getWidth(), GamePanel.this .getHeight()))); // Size the ViewScreen to the actual dimensions of the // viewable area now that we know them viewScreen.CenterOnObject(player.getBoundingBox()); } }); this.setFocusable(true); this.addKeyListener(player); new Timer(16, this).start(); // Master game timer }
public void updateGC() { int scrn = getScreenImOn(); if (screenLog.isLoggable(PlatformLogger.Level.FINER)) { log.finer("Screen number: " + scrn); } // get current GD Win32GraphicsDevice oldDev = (Win32GraphicsDevice) winGraphicsConfig.getDevice(); Win32GraphicsDevice newDev; GraphicsDevice devs[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); // Occasionally during device addition/removal getScreenImOn can return // a non-existing screen number. Use the default device in this case. if (scrn >= devs.length) { newDev = (Win32GraphicsDevice) GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); } else { newDev = (Win32GraphicsDevice) devs[scrn]; } // Set winGraphicsConfig to the default GC for the monitor this Window // is now mostly on. winGraphicsConfig = (Win32GraphicsConfig) newDev.getDefaultConfiguration(); if (screenLog.isLoggable(PlatformLogger.Level.FINE)) { if (winGraphicsConfig == null) { screenLog.fine("Assertion (winGraphicsConfig != null) failed"); } } // if on a different display, take off old GD and put on new GD if (oldDev != newDev) { oldDev.removeDisplayChangedListener(this); newDev.addDisplayChangedListener(this); } AWTAccessor.getComponentAccessor() .setGraphicsConfiguration((Component) target, winGraphicsConfig); }
public void generarFondo(Component componente) { Rectangle areaDibujo = this.getBounds(); BufferedImage tmp; GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); tmp = gc.createCompatibleImage(areaDibujo.width, areaDibujo.height, BufferedImage.TRANSLUCENT); Graphics2D g2d = (Graphics2D) tmp.getGraphics(); g2d.setColor(new Color(55, 55, 255, 165)); g2d.fillRect(0, 0, areaDibujo.width, areaDibujo.height); fondo = tmp; }
BufferedImage createImageFromComponent(Component comp) { BufferedImage retImage = null; if (comp == null) return retImage; try { GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = genv.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); java.awt.image.ColorModel cm = gc.getColorModel(); boolean hasAlpha = cm.hasAlpha(); int cw = comp.getSize().width; int ch = comp.getSize().height; if (hasAlpha) { retImage = gc.createCompatibleImage(cw, ch); } else { retImage = new BufferedImage(cw, ch, BufferedImage.TYPE_INT_ARGB); } if (retImage == null) return retImage; Graphics og = retImage.getGraphics(); comp.paint(og); og.dispose(); } catch (Throwable t) { } return retImage; }
public static void main(String[] args) { try { if (args.length != 2) { System.err.println("USAGE: java RenderMap map.txt image.png"); System.exit(1); } Game game = new Game(args[0], 100, 0); if (game.Init() == 0) { System.err.println("Error while loading map " + args[0]); System.exit(1); } ArrayList<Color> colors = new ArrayList<Color>(); colors.add(new Color(106, 74, 60)); colors.add(new Color(74, 166, 60)); colors.add(new Color(204, 51, 63)); colors.add(new Color(235, 104, 65)); colors.add(new Color(237, 201, 81)); Color bgColor = new Color(188, 189, 172); Color textColor = Color.BLACK; Font planetFont = new Font("Sans Serif", Font.BOLD, 11); Font fleetFont = new Font("Sans serif", Font.PLAIN, 7); GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); BufferedImage image = gc.createCompatibleImage(640, 480); Graphics2D _g = (Graphics2D) image.createGraphics(); // Turn on AA/Speed _g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); _g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED); _g.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); game.Render(640, 480, 0.0, null, colors, bgColor, textColor, planetFont, fleetFont, _g); File file = new File(args[1]); ImageIO.write(image, "png", file); } catch (Exception e) { System.err.println(e); } }
public GraphicsConfiguration getDeviceConfiguration() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); return (gd.getDefaultConfiguration()); }