/** * Creates the image to cache. This returns a translucent image. * * @param c Component painting to * @param w Width of image to create * @param h Height to image to create * @param config GraphicsConfiguration that will be rendered to, this may be null. */ @Override protected Image createImage(Component c, int w, int h, GraphicsConfiguration config) { if (config == null) { return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE); } return config.createCompatibleImage(w, h, Transparency.TRANSLUCENT); }
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 = GraphicsUtilities.createThumbnailFast(blurBuffer, getWidth() / 2); blurBuffer = new GaussianBlurFilter(5).filter(blurBuffer, null); } else { tmp = gc.createCompatibleImage(areaDibujo.width, areaDibujo.height, BufferedImage.TRANSLUCENT); Graphics2D g2d = (Graphics2D) tmp.getGraphics(); g2d.setColor(new Color(55, 55, 255, 145)); g2d.fillRect(0, 0, areaDibujo.width, areaDibujo.height); fondo = tmp; } }
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 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; }
/** 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; // } }
// 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 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 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 }
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; }