[英]Displaying an image within an applet
我有以下代碼。 當它正確顯示線條,矩形,橢圓形和字符串時,圖像不會加載。 圖片位於正確的目錄中,只是無法弄清楚為什么它不顯示...
import java.awt.*; // for Graphics, Image, and Color classes
import java.applet.Applet;
public class GraphicsDemo extends Applet
{
public void paint (Graphics g)
{
Image image;
image = this.getImage(getDocumentBase (), "flower.jpg");
// display smaller complete image in upper left corner of window
g.drawImage(image, 0, 0, 427, 284, // destination topL, botR
0, 0, 640, 427, this); // source topL, botR
// establish color of all lines to be drawn
g.setColor(Color.BLUE);
// draw rectangle around region to be expanded
g.drawRect(200, 60, 120, 120); // topL, width & height
// draw lines between corners of rectangles
g.drawLine(200, 60, 240, 240); // upper left
g.drawLine(320, 60, 600, 240); // upper right
g.drawLine(200, 180, 240, 600); // lower left
g.drawLine(320, 180, 600, 600); // lower right
// display expanded part of original image
g.drawImage(image, 240, 240, 600, 600, // destination topL, botR
300, 90, 480, 270, this); // source topL, botR
// draw rectangle around expanded part of image
g.drawRect(240, 240, 360, 360); // topL, width & height
// create BLUE colored oval and write name on it
g.fillOval(520, 380, 45, 30); // topL, width & height
g.setColor(Color.WHITE); // change color for text
g.drawString("Max", 530, 400); // string & start position
} // end main
} // end class GraphicsDemo
該圖像很可能不存在於文檔庫中。 其余代碼在功能上都可以。 添加一個init方法,如下所示:
public void init() {
System.out.println(getDocumentBase());
}
並將圖像復制到顯示的位置。
相關注意事項:
您從paint
錯過了這句話
super.paint(g);
也將建議移動此聲明
image = this.getImage(getDocumentBase (), "flower.jpg");
到applet的init
方法,這樣就不會在每次繪制applet時加載圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.