import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JPanel; import com.sun.image.codec.jpeg.ImageFormatException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageDecoder; /** * Superclass PitchPanel. Carries out all rendering of pitch * jpegs and defines the high level repaint functionality. * * @author Adrien Martel * */ public class PitchPanel extends JPanel{ private String pitchFilename = ""; private BufferedImage myImage; protected PitchPanel pp; protected JFrame f; protected Graphics2D g2d; /** * Constructor PitchPanel. Instantiates a new pitch and * appends it to a panel so that its subclasses can manipulate * them accordingly. * * @param pitchFilename - the path of the pitch jpeg to be used * */ public PitchPanel(String pitchFilename){ super(); this.pitchFilename = pitchFilename; f = new JFrame(); f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); //hidden for later display at half/full time f.setVisible(true); f.setResizable(false); } /** * Method BuggeredImage returns the currently loaded pitch * jpeg. * * @return the currently loaded pitch jpeg * */ protected BufferedImage getMyImage() { return myImage; } /** * Method loadArrowImage. Loads the pitch.jpg graphic. * */ protected void loadImage() { try { String filename = pitchFilename; FileInputStream in = new FileInputStream(filename); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in); myImage = decoder.decodeAsBufferedImage(); in.close(); } catch (ImageFormatException ife) { System.out.println("Image source file invalid."); } catch (FileNotFoundException fnfe) { System.out.println("Could not locate image file."); } catch (IOException ioe) { System.out.println("Error reading image file."); } } /** * * Method paintComponent. Loads the jpeg then draws the current line reference by * the curIndex field. * */ public void paintComponent(Graphics g) { loadImage(); BufferedImage bi = getMyImage(); super.paintComponent(g); g2d = (Graphics2D) g; g2d.drawImage(bi, null, 0, 0); } }