barrier.java
来自「Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套」· Java 代码 · 共 56 行
JAVA
56 行
/* Pylon objects display an image like a plastic highway barrier/cone. Author: David Riley -- Jan, 2005 */ /** class invariant * getWidth() >= 36 AND getHeight() >= 36 */import java.awt.Toolkit.*;import java.awt.*;import javax.imageio.ImageIO;import java.io.*;import javax.swing.JComponent;public class Barrier extends JComponent implements java.awt.image.ImageObserver { private java.awt.Image content; /** pre: a proper JPEG image file must be in file /graphics/Pylon.jpg in the * same folder as the current .class file * post: an pylon image is instantiated * and getX() == 0 and getY() == 0 * and getWidth() == 36 AND getHeight() == 36 */ public Barrier() { super(); setBounds(0, 0, 36, 36); setImage("graphics/Barrier.jpg"); } /** post: Upon repaint the image displayed will be given by the * file with complete pathname specified as s * (Note that the file should be a gif or jpeg encoding.) */ private void setImage(String s) { java.net.URL url = getClass().getResource(s); // for applets if (url == null) { url = getClass().getResource("/"+s); if (url == null) try { // for applications content = ImageIO.read(new File(s)); System.out.println("hereh"); } catch(IOException ioe) { ioe.printStackTrace(); } else content = getToolkit().getImage(url); } else content = getToolkit().getImage(url); } public void paint(Graphics g) { g.drawImage(content, 0, 0, getWidth(), getHeight(), this); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?