/** PicturePanel.java -- show a picture inside a panel. Actually, there are * 2 pictures, one for a daytime view, and the other for a nighttime view. * By default we start off with the daytime picture, but whenever there is an * event, we'll reset the boolean variable daytime -- this variable will let * the program choose which picture to display. * Since the 2 pictures are not quite the same size, we'll set the preferred * size of this panel so the rest of the GUI doesn't get shifted up/down. */ import java.awt.*; import javax.swing.*; public class PicturePanel extends JPanel { private boolean daytime; private ImageIcon dayPix, nightPix; private JLabel imageLabel; public PicturePanel() { setPreferredSize(new Dimension(300,200)); dayPix = new ImageIcon("day.jpg"); nightPix = new ImageIcon("night.jpg"); // Initialize to the daytime image, and put it in the JPanel. daytime = true; imageLabel = new JLabel(dayPix); add(imageLabel); } public void paintComponent(Graphics g) { super.paintComponent(g); if (daytime) imageLabel.setIcon(dayPix); else imageLabel.setIcon(nightPix); } public void setDaytime(boolean isDaytime) { daytime = isDaytime; } }