asked in 電腦與網際網路程式設計 · 1 decade ago

java 2D 語法問題

import java.awt.*;

import java.awt.event.*;

import java.awt.image.*;

import java.net.URL;

public class RightSizer

extends ApplicationFrame {

private Image mImage;

private MyImgOb imgob;

public static void main(String[] args) throws Exception {

String url = \"http://java.oreilly.com/\" +

\"news/knudsen/graphics/bite-size_banner.gif\";

if (args.length > 0) url = args[0];

new RightSizer(new URL(url)); <-------------------------這一行,該怎麼解釋?

}

public RightSizer(URL url) {

super(\"RightSizer v1.0\");

mImage = (Toolkit.getDefaultToolkit()).getImage(url);

imgob=new MyImgOb(this);

rightSize();

}

protected void rightSize() {

int width = mImage.getWidth(imgob);

int height = mImage.getHeight(imgob);

System.out.println(\"Width= \" + width);

if (width == -1 || height == -1) return;

addNotify();

Insets insets = getInsets();

setSize(width + insets.left + insets.right,

height + insets.top + insets.bottom);

center();

setVisible(true);

}

public void update(Graphics g) {

paint(g);

}

public void paint(Graphics g) {

Insets insets = getInsets();

g.drawImage(mImage, insets.left, insets.top, imgob);

}

}

1 Answer

Rating
  • Ahiru
    Lv 7
    1 decade ago
    Favorite Answer

    因為這個程式需要一張圖片( "http://java.oreilly.com/" +

    "news/knudsen/graphics/bite-size_banner.gif";)

    所以他在開始run的時候, 會建立一個URL物件, 也就是 new URL(url), 傳給這個程式, 讓程式知道到哪裡去download這張圖

    Source(s): me
Still have questions? Get your answers by asking now.