csc2700lect13

csc2700lect13 - image = filename public void...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSc 2700: Lecture # 13 Displaying Images October 14, 2008 1 Displaying An Image An icon is a small picture that can be displayed on a component and Java’s Icon interface declares the following methods for working with icons: int getIconHeight() , int getIconWidth() , and void paintIcon(Component c, Graphics g, int x, int y) , where (x,y) denote a position in component c. The ImageIcon class, found in Swing, implements the Icon interface. The constructor ImageIcon(String filename) creates an icon from the speciFed image Fle. The following example displays a vintage US Army photo of two women manually programming the ENIAC, one of the world’s Frst computers. Need- less to say, they did not use Java. The image is stored in the Fle ”eniac.gif.” 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Program 1. import java.awt.*; import javax.swing.*; public class PicturePanel extends JPanel { private String image; // a filename public PicturePanel(String filename)
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { image = filename; } public void paintComponent(Graphics g) { super.paintComponent(g); ImageIcon picture = new ImageIcon(image); picture.paintIcon(this, g, 0,0); // this means "this panel" } } import javax.swing.*; public class ShowPicture extends JFrame { public ShowPicture() { super("Two women programming the Eniac "); setBounds(0,0,650,450); PicturePanel picPanel = new PicturePanel("eniac.gif"); add(picPanel); setVisible(true); } public static void main(String args) { JFrame frame = new ShowPicture(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } Figure 1: Ancient Programming 2 Unlike a JButton or JLabel object, which is added to a panel that then paints itself, an IconImage object paints itself onto a panel. You can also place an IconImage on a button or label directly, and add the button or label to a panel or frame. 3...
View Full Document

{[ snackBarMessage ]}

Page1 / 3

csc2700lect13 - image = filename public void...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online