public class Main extends JPanel{
private static final long serialVersionUID = 1L;
static BufferedImage image;
private static JPanel imagePanel;
private static JFrame frame;
private static String dir = "image\\pic.jpg";
public Main(){
try{
image = ImageIO.read(new File(dir));
}catch (IOException e){
// If it cannot read from the directory inform the client
System.out.println("Error reading from directory: " + e.getMessage());
}
}
//We set our preffered size if we succeed in loading the image
public Dimension getPrefferedSize(){
if(image == null){
return new Dimension(100, 100);
}else{
return new Dimension(image.getWidth(null), image.getHeight(null));
}
}
//Draw our image on the screen with Graphic's "drawImage()" method
public void paint(Graphics g){
g.drawImage(image, 0, 0, null);
}
public static void main(String[] args){
frame = new JFrame("Loading image from file example");
imagePanel = new JPanel();
//Release the resource window handle as we close the frame
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
try{
image = ImageIO.read(new File(dir));
if(image.getWidth(null) != 0){
System.out.println(image.getWidth(null));
System.out.println(image.getHeight(null));
}
}catch(Exception e){
e.printStackTrace();
}
imagePanel.add(new Main());
frame.add(imagePanel);
//frame.pack();
frame.setSize(200, 300);
frame.setVisible(true);
}
/*
public static BufferedImage enlarge(BufferedImage image, int n){
int w = n * image.getWidth();
int h = n * image.getHeight();
BufferedImage enlargedImage = new BufferedImage(w, h, image.getType());
for (int y=0;y<h;y++){
for(int x=0;x<w;x++){
enlargedImage.setRGB(x, y, image.getRGB(x/n, y/n));
}
}
return enlargedImage;
}*/
}
I'm only getting the top-left 8x8 pixels of the image I provided ("pic.jpg"). Could anyone explain this?