2011-09-03 7 views
3

Java 애플릿에서 어떻게 백그라운드 이미지를 설정합니까?Java 애플릿의 배경 이미지

Java 애플릿 클래스에서 background.gif를 배경으로하고 싶지만 어떻게해야합니까?

+0

* "Java 애플릿에서 배경 이미지는 어떻게 설정합니까?"* 일단 이미지가 있으면 'JPanel'에서 이미지를 만들면됩니다. –

+0

BTW - 애니메이션 GIF입니까? –

답변

4

나는이 일을하는 기능이 없다고 생각합니다. 그러나 Panel (컴포넌트의 단순한 컨테이너 역할을 할 수 있음)을 확장하고 paint 메소드를 오버라이드하여 백그라운드에서 이미지를 그릴 수 있습니다.

다음은 샘플 프로그램입니다. 희망이 도움이됩니다.

import java.applet.*; 
import java.awt.*; 
import java.net.*; 
import java.io.IOException.*; 

public class BackgroundApplet extends Applet { 
    Image backGround; 

    public void init() { 

      // set the size of the applet to the size of the background image. 
      // Resizing the applet may cause distortion of the image. 
      setSize(300, 300); 

      // Set the image name to the background you want. Assumes the image 
      // is in the same directory as the class file is 
      backGround = getImage(getCodeBase(), "save.GIF"); 
      BackGroundPanel bgp = new BackGroundPanel(); 
      bgp.setLayout(new FlowLayout()); 
      bgp.setBackGroundImage(backGround); 

      // Add the components you want in the Applet to the Panel 
      bgp.add(new Button("Button 1")); 
      bgp.add(new TextField("isn't this cool?")); 
      bgp.add(new Button("Useless Button 2")); 

      // set the layout of the applet to Border Layout 
      setLayout(new BorderLayout()); 

      // now adding the panel, adds to the center 
      // (by default in Border Layout) of the applet 
      add(bgp); 
    } 
} 

class BackGroundPanel extends Panel { 
    Image backGround; 

    BackGroundPanel() { 
      super(); 
    } 

    public void paint(Graphics g) { 

      // get the size of this panel (which is the size of the applet), 
      // and draw the image 
      g.drawImage(getBackGroundImage(), 0, 0, 
       (int)getBounds().getWidth(), (int)getBounds().getHeight(), this); 
    } 

    public void setBackGroundImage(Image backGround) { 
      this.backGround = backGround;  
    } 

    private Image getBackGroundImage() { 
      return backGround;  
    } 
} 
+2

AWT? (수표 시계 - 깨닫지 않는 깨달음) 천년기는 무엇입니까? –

+1

'setSize (300,300);'이것은 브라우저에서 안정적으로 작동하지 않습니다. 애플릿의 크기는 HTML에 의해 설정되어야합니다. –