Imagem .gif

4 respostas
B

Boa Tarde…

Gstaria de gerar uma imagem .gif para mostrar em uma pagina web a imagem seria um grafico de barras… Alguem sabe utilizando a API java2D criar uma imagem? nao posso usar applets… gostaria mesmo eh de gerar uma imagem .gif p/ depois mostrar na web…

Obrigado a todos…

4 Respostas

RodrigoSol

Direto do javaalmanac.com

e666. Creating a Buffered Image
A buffered image is a type of image whose pixels can be modified. For example, you can draw on a buffered image and then draw the resulting buffered image on the screen or save it to a file. A buffered image supports many formats for storing pixels. Although a buffered image of any format can be drawn on the screen, it is best to choose a format that is the most compatible with the screen to allow efficient drawing. This example demonstrates several ways of creating a buffered image. 
If the buffered image will not be drawn, it can simply be constructed with a specific format; TYPE_INT_RGB and TYPE_INT_ARGB are typically used. See BufferedImage for a list of available formats. 

See also e667 Creating a Buffered Image from an Image. 

    int width = 100;
    int height = 100;
    
    // Create buffered image that does not support transparency
    BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    
    // Create a buffered image that supports transparency
    bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

These examples create buffered images that are compatible with the screen: 
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gs.getDefaultConfiguration();
    
    // Create an image that does not support transparency
    bimage = gc.createCompatibleImage(width, height, Transparency.OPAQUE);
    
    // Create an image that supports transparent pixels
    bimage = gc.createCompatibleImage(width, height, Transparency.BITMASK);
    
    // Create an image that supports arbitrary levels of transparency
    bimage = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);

A screen compatible buffered image can also be created from a graphics context: 
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        int width = 100;
        int height = 100;
    
        // Create an image that does not support transparency
        BufferedImage bimage = g2d.getDeviceConfiguration().createCompatibleImage(
            width, height, Transparency.OPAQUE);
    
        // Create an image that supports transparent pixels
        bimage = g2d.getDeviceConfiguration().createCompatibleImage(
            width, height, Transparency.BITMASK);
    
        // Create an image that supports arbitrary levels of transparency
        bimage = g2d.getDeviceConfiguration().createCompatibleImage(
            width, height, Transparency.TRANSLUCENT);
    }

One last way of creating a buffered image is using Component.createImage(). This method can be used only if the component is visible on the screen. Also, this method returns buffered images that do not support transparent pixels. 
    BufferedImage bimage = (BufferedImage)component.createImage(width, height);
    if (bimage == null) {
        // The component is not visible on the screen
    }

http://www.javaalmanac.com/egs/java.awt.image/CreateBuf.html

B

Vlw

Rodrigo… mas nao compreendi em que momento eu gravo a imagem em um diretorio por exemplo /imagens/teste.gif ?

Mas muito obrigado pela ajuda…
:oops: Nunca trabalhei com imagens…

dudaskank

Bem, no código acima não tem nenhum lugar que grava as imagens mesmo...

Para salvar, tente algo como:

// importando as classes
import javax.imageio.ImageIO;
import java.io.File;

// e aí na hora de salvar
ImageIO.write(meuBufferdImageQueDesenhei, "png", new File("teste.png"));

Eu não testei isso, só pra informar, mas é tão simples que é capaz de funcionar...

Para desenhar no BufferdImage, você pode escrever nele modificando diretamente os pixels, mas é mais fácil usar os métodos da classe Graphics, como drawString, etc...

Falou

^__^

B

Obrigadao galera…

:smiley: :smiley: :smiley: :smiley: :smiley: :smiley:

Criado 12 de abril de 2004
Ultima resposta 14 de abr. de 2004
Respostas 4
Participantes 3