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…
e666.CreatingaBufferedImageAbufferedimageisatypeofimagewhosepixelscanbemodified.Forexample,youcandrawonabufferedimageandthendrawtheresultingbufferedimageonthescreenorsaveittoafile.Abufferedimagesupportsmanyformatsforstoringpixels.Althoughabufferedimageofanyformatcanbedrawnonthescreen,itisbesttochooseaformatthatisthemostcompatiblewiththescreentoallowefficientdrawing.Thisexampledemonstratesseveralwaysofcreatingabufferedimage.Ifthebufferedimagewillnotbedrawn,itcansimplybeconstructedwithaspecificformat;TYPE_INT_RGBandTYPE_INT_ARGBaretypicallyused.SeeBufferedImageforalistofavailableformats.Seealsoe667CreatingaBufferedImagefromanImage.intwidth=100;intheight=100;// Create buffered image that does not support transparencyBufferedImagebimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);// Create a buffered image that supports transparencybimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);Theseexamplescreatebufferedimagesthatarecompatiblewiththescreen:GraphicsEnvironmentge=GraphicsEnvironment.getLocalGraphicsEnvironment();GraphicsDevicegs=ge.getDefaultScreenDevice();GraphicsConfigurationgc=gs.getDefaultConfiguration();// Create an image that does not support transparencybimage=gc.createCompatibleImage(width,height,Transparency.OPAQUE);// Create an image that supports transparent pixelsbimage=gc.createCompatibleImage(width,height,Transparency.BITMASK);// Create an image that supports arbitrary levels of transparencybimage=gc.createCompatibleImage(width,height,Transparency.TRANSLUCENT);Ascreencompatiblebufferedimagecanalsobecreatedfromagraphicscontext:publicvoidpaint(Graphicsg){Graphics2Dg2d=(Graphics2D)g;intwidth=100;intheight=100;// Create an image that does not support transparencyBufferedImagebimage=g2d.getDeviceConfiguration().createCompatibleImage(width,height,Transparency.OPAQUE);// Create an image that supports transparent pixelsbimage=g2d.getDeviceConfiguration().createCompatibleImage(width,height,Transparency.BITMASK);// Create an image that supports arbitrary levels of transparencybimage=g2d.getDeviceConfiguration().createCompatibleImage(width,height,Transparency.TRANSLUCENT);}OnelastwayofcreatingabufferedimageisusingComponent.createImage().Thismethodcanbeusedonlyifthecomponentisvisibleonthescreen.Also,thismethodreturnsbufferedimagesthatdonotsupporttransparentpixels.BufferedImagebimage=(BufferedImage)component.createImage(width,height);if(bimage==null){// The component is not visible on the screen}
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 classesimportjavax.imageio.ImageIO;importjava.io.File;// e aí na hora de salvarImageIO.write(meuBufferdImageQueDesenhei,"png",newFile("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...