《Java2图形设计卷II:Swing》配套光盘源码

源代码在线查看: testapplet.java

软件大小: 4504 K
上传用户: guigong
关键词: Java2 Swing 图形 光盘
下载地址: 免注册下载 普通下载 VIP

相关代码

				import javax.swing.*;
				import java.awt.*;
				
				public class TestApplet extends JApplet {
					public void init() {
						Container contentPane = getContentPane();
						ImageCanvas imagePanel = new ImageCanvas(
													"sphere.gif",
													"a blue sphere");
						imagePanel.setBorder(
							BorderFactory.createTitledBorder("ImageCanvas"));
				
						contentPane.setLayout(new FlowLayout());
						contentPane.add(imagePanel);
					}
				}
				class ImageCanvas extends JPanel {
					ImageIcon icon;
				
					public ImageCanvas(String imageName, String description) {
						icon = new ImageIcon(imageName, description);
					}
					public void paintComponent(Graphics g) {
						Insets insets = getInsets();
						super.paintComponent(g);
						icon.paintIcon(this, g, insets.left, insets.top);
					}
					public Dimension getPreferredSize() {
						Insets insets = getInsets();
						return new Dimension(
							icon.getIconWidth() + insets.left + insets.right, 
							icon.getIconHeight() + insets.top + insets.bottom);
					}
				}
							

相关资源