import java.awt.Color; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class ScaledImageOnJFrame { private JFrame frame; private JLabel labelPic; // Resize the "pizza" picture to be 400 by 100 pixels; note that the size of // the JLabel that contains the picture needs to also be at least that size // to avoid having parts of the picture cut off private Image scaledPizza = new ImageIcon("data_files/pizza.gif").getImage().getScaledInstance(400, 100, Image.SCALE_DEFAULT); private ImageIcon pizzaPic = new ImageIcon(scaledPizza); public static void main(String[] args) { new ScaledImageOnJFrame(); } public ScaledImageOnJFrame() { frame = new JFrame(); frame.setSize(500, 400); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(null); frame.setTitle("Scaled Image on JFrame"); labelPic = new JLabel(pizzaPic); labelPic.setSize(400, 100); labelPic.setLocation(30, 120); frame.getContentPane().setBackground(Color.BLACK); frame.add(labelPic); frame.setVisible(true); } }