public class TestIcon extends JFrame implements Icon {
private int width;
private int height;
public TestIcon ( ) {
}
public TestIcon ( int width, int height) {
this . width = width;
this . height = height;
}
public void init ( ) {
TestIcon testIcon = new TestIcon ( 15 , 15 ) ;
JLabel label = new JLabel ( "icontest" , testIcon, SwingConstants . CENTER) ;
Container container = getContentPane ( ) ;
container. add ( label) ;
setBounds ( 400 , 400 , 800 , 600 ) ;
setVisible ( true ) ;
this . setDefaultCloseOperation ( WindowConstants . EXIT_ON_CLOSE) ;
}
@Override
public void paintIcon ( Component c, Graphics g, int x, int y) {
g. fillOval ( x, y, width, height) ;
}
@Override
public int getIconWidth ( ) {
return this . width;
}
@Override
public int getIconHeight ( ) {
return this . height;
}
public static void main ( String [ ] args) {
new TestIcon ( ) . init ( ) ;
}
}
public class ImageIconDemo extends JFrame {
public ImageIconDemo ( ) {
JLabel label = new JLabel ( "ImageIcon" ) ;
URL url = ImageIconDemo . class . getResource ( "002.jpg" ) ;
ImageIcon imageIcon = new ImageIcon ( url) ;
label. setHorizontalAlignment ( SwingConstants . CENTER) ;
label. setIcon ( imageIcon) ;
Container container = getContentPane ( ) ;
container. add ( label) ;
setVisible ( true ) ;
setDefaultCloseOperation ( WindowConstants . EXIT_ON_CLOSE) ;
setBounds ( 400 , 400 , 800 , 600 ) ;
}
public static void main ( String [ ] args) {
new ImageIconDemo ( ) ;
}
}