Adicionar componentes em cima de uma imagem no Frame
4 respostas
R
Rumblefish
Olá companheiros!
Estou começando a brincar com interface gráfica no java, elaborei uma janelinha e gostaria de incluir botoes em cima da imagem que inseri no frame. Ja pesquisei bastante no google, em fórums, mas até agora não encontrei uma forma de faze-lo.
Use o componente JImagePanel que voce encontra aqui mesmo no forum.
ele é um paindel normal so que com uma imagem de fundo
R
Rumblefish
É padrão do java?
OU algum membro do fórum que elaborou?
M
mateusviccari
Não é classe padrão do java, foi um JPanel com implementações feitas por um usuario aqui do forum.
No java vem pouca coisa em relação a componentes por padrão, mas existe muitas classes que as pessoas "modificam" pra implementar funções especificas, ja que é facil fazer essas implementações.
Segue o codigo do componente:
importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.Paint;importjava.awt.TexturePaint;importjava.awt.geom.Rectangle2D;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;importjavax.imageio.ImageIO;importjavax.swing.JPanel;/** * A panel that contains a background image. The background image is * automatically sized to fit in the panel. */publicclassJImagePanelextendsJPanel{privateBufferedImageimage=null;privateFillTypefillType=FillType.RESIZE;/** * Creates a new panel with the given background image. * * @param img The background image. */publicJImagePanel(){}/** * Changes the image panel image. * * @param img The new image to set. */publicfinalvoidsetImage(BufferedImageimg){if(img==null)thrownewNullPointerException("Buffered image cannot be null!");this.image=img;invalidate();}/** * Changes the image panel image. * * @param img The new image to set. * @throws IOException If the file does not exist or is invalid. */publicvoidsetImage(Fileimg)throwsIOException{setImage(ImageIO.read(img));}/** * Changes the image panel image. * * @param img The new image to set. * @throws IOException If the file does not exist or is invalid. */publicvoidsetImage(StringfileName)throwsIOException{setImage(newFile(fileName));}/** * Returns the image associated with this image panel. * * @return The associated image. */publicBufferedImagegetImage(){returnimage;}@OverrideprotectedvoidpaintComponent(Graphicsg){super.paintComponent(g);Graphics2Dg2d=(Graphics2D)g.create();fillType.drawImage(this,g2d,image);g2d.dispose();}/** * Returns the way this image fills itself. * * @return The fill type. */publicFillTypegetFillType(){returnfillType;}/** * Changes the fill type. * * @param fillType The new fill type * @throws IllegalArgumentException If the fill type is null. */publicvoidsetFillType(FillTypefillType){if(fillType==null)thrownewIllegalArgumentException("Invalid fill type!");this.fillType=fillType;invalidate();}publicstaticenumFillType{/** * Make the image size equal to the panel size, by resizing it. */RESIZE{@OverridepublicvoiddrawImage(JPanelpanel,Graphics2Dg2d,BufferedImageimage){if(image!=null){g2d.drawImage(image,0,0,panel.getWidth(),panel.getHeight(),null);}}},/** * Centers the image on the panel. */CENTER{@OverridepublicvoiddrawImage(JPanelpanel,Graphics2Dg2d,BufferedImageimage){if(image!=null){intleft=(panel.getHeight()-image.getHeight())/2;inttop=(panel.getWidth()-image.getWidth())/2;g2d.drawImage(image,top,left,null);}}},/** * Makes several copies of the image in the panel, putting them side by * side. */SIDE_BY_SIDE{@OverridepublicvoiddrawImage(JPanelpanel,Graphics2Dg2d,BufferedImageimage){if(image!=null){Paintp=newTexturePaint(image,newRectangle2D.Float(0,0,image.getWidth(),image.getHeight()));g2d.setPaint(p);g2d.fillRect(0,0,panel.getWidth(),panel.getHeight());}}};publicabstractvoiddrawImage(JPanelpanel,Graphics2Dg2d,BufferedImageimage);}}