Created
January 4, 2016 20:02
-
-
Save AnnaBoro/f6fd0a3b9555c001f177 to your computer and use it in GitHub Desktop.
JFrame + image
This file contains hidden or 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 lesson5_8; | |
import javax.imageio.ImageIO; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.image.ImageObserver; | |
import java.io.File; | |
import java.io.IOException; | |
public class JFrameImage extends JPanel{ | |
private final static String IMAGE_NAME = "face.jpeg"; | |
private Image myImage; | |
public JFrameImage() { | |
JFrame f = new JFrame("Frame with Image"); | |
f.setMinimumSize(new Dimension(900, 600)); | |
f.setLocation(100, 200); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
f.getContentPane().add(this); | |
f.pack(); | |
try { | |
myImage = ImageIO.read(new File(IMAGE_NAME)); | |
} | |
catch (IOException e) { | |
System.err.print("Can't find image " + IMAGE_NAME); | |
} | |
f.setVisible(true); | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
g.drawImage(myImage, 100, 100, new ImageObserver() { | |
@Override | |
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { | |
return false; | |
} | |
}); | |
} | |
public static void main(String[] args) { | |
new JFrameImage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment