📄 displayimage.java
字号:
import java.awt.*;
import javax.swing.*;
import java.net.*;
public class DisplayImage extends JApplet
{
public void init()
{
Image image = null;
try
{
// Image from a file specified by a URL
image = getImage(new URL(getCodeBase(),"Images/wrox_logo.gif"));
}
catch(MalformedURLException e)
{
System.out.println("Failed to create URL:\n" + e);
}
int imageWidth = image.getWidth(this); // Get its width
int imageHeight = image.getHeight(this); // and its height
if(imageWidth != -1 && imageHeight != -1) // If they are available
resize(imageWidth,imageHeight); // set applet size to fit
ImagePanel imagePanel = new ImagePanel(image); // Create panel showing the image
getContentPane().add(imagePanel); // Add the panel to the content pane
}
public boolean imageUpdate(Image img, // Reference to the image
int flags, // Flags identifying what is available
int x, // x coordinate
int y, // y coordinate
int width, // Image width
int height) // Image height
{
if((flags & WIDTH) > 0 && (flags & HEIGHT) > 0)
{
resize(width,height); // Set applet size to fit the image
repaint(); // Repaint the applet in its new size
return true;
}
else
return false; // More info required
}
// Class representing a panel displaying an image
class ImagePanel extends JPanel
{
public ImagePanel(Image image)
{
this.image = image;
}
public void paint(Graphics g)
{
g.drawImage(image, 0, 0, this); // Display the image
}
Image image; // The image
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -