【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了Barrier.java提供大家参考,希望对大家有所帮助!
/* 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);
}
}
上一篇:
BarProxy.java
下一篇:
台湾海峡台风浪的数值模拟