开发者

Add image from website to Frame in Java

开发者 https://www.devze.com 2022-12-21 05:06 出处:网络
I\'m trying to make a Frame from which I want to add an image from a website. Do I set the background as an image?

I'm trying to make a Frame from which I want to add an image from a website.

Do I set the background as an image?

import java.awt.*;
import java.net.*;

class NewFrame extends Frame {

  // Constructor.
  public NewFrame (int width, int height)
  {
  开发者_运维技巧  // Set the title and other frame parameters.
    this.setTitle ("Exercise 9.5");
    this.setResizable (true);
    this.setBackground (Color.cyan);
    this.setSize (width, height);

    // Show the frame.
    this.setVisible (true);
  }

  // Override paint():
  public void paint (Graphics g)
  {

  } 

} // End of class "NewFrame"

public class ex5 {

  public static void main (String[] argv)
  {
    NewFrame nf = new NewFrame (300, 250);
  }

}


You can use the following

 // load the image once
 BufferedImage bi = ImageIO.read(new URL(imageLocAsString));
 // now in paint(Graphics g) do
 g.drawImage(bi, 0, 0, null);

Look here for more information.

0

精彩评论

暂无评论...
验证码 换一张
取 消