Created
December 13, 2012 00:05
-
-
Save sixhat/4272874 to your computer and use it in GitHub Desktop.
Java Background Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package imageBG; | |
import java.awt.Graphics; | |
import java.awt.Image; | |
import javax.swing.JComponent; | |
public class ImagePanel extends JComponent { | |
private Image image; | |
public ImagePanel(Image image) { | |
this.image = image; | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
g.drawImage(image, 0, 0, null); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package imageBG; | |
import java.awt.image.BufferedImage; | |
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import javax.imageio.ImageIO; | |
import javax.swing.JFrame; | |
public class Main { | |
public Main() { | |
try { | |
BufferedImage img = ImageIO.read(new URL( | |
"http://static.sixhat.net/nimages/2012/10/bicicleta-na-praca-castelo-em-turim.jpg")); | |
JFrame janela = new JFrame("Janela com Background"); | |
janela.setSize(800, 600); | |
janela.setContentPane(new ImagePanel(img)); | |
janela.setVisible(true); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
new Main(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment