Jak dodać tło lub bitmapę? JAVA!


(system) #1

Mam taki oto mój kod programu:

import java.awt.*;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.image.BufferedImage;

import java.util.Timer;

import java.util.TimerTask;

import javax.swing.JFrame;

import javax.swing.JPanel;



public class BufferedAnimate extends JFrame {


public BufferedAnimate()

{

	setTitle("Bitmapa");

	setBackground(Color.red);

	setSize(900,600);

	setVisible(true);

	go();	

}


  private static int DELAY = 300;

  Image buffer;

  Dimension oldSize;

  Insets insets;

  Color colors[] = { Color.BLACK, Color.DARK_GRAY, Color.BLACK, Color.DARK_GRAY,

      Color.BLACK, Color.DARK_GRAY, Color.BLACK, Color.DARK_GRAY, Color.BLACK, Color.DARK_GRAY,

      Color.BLACK, Color.DARK_GRAY, Color.BLACK, Color.DARK_GRAY, Color.BLACK, Color.DARK_GRAY, 

      Color.BLACK, Color.DARK_GRAY};


  public void paint(Graphics g) {


    if ((oldSize == null) || (oldSize != getSize())) {

      oldSize = getSize();

      buffer = new BufferedImage(getWidth(), getHeight(),

          BufferedImage.TYPE_INT_RGB);

    }

    if (insets == null) {

      insets = getInsets();

    }

    int x = insets.left;

    int y = insets.top;

    int width = getWidth() - insets.left - insets.right;

    int height = getHeight() - insets.top - insets.bottom;

    int start = 0;

    int steps = colors.length;

    int stepSize = 360 / steps;

    synchronized (colors) {

      Graphics bufferG = buffer.getGraphics();

      bufferG.setColor(Color.RED);

      bufferG.fillRect(x, y, width, height);

      for (int i = 0; i < steps; i++) {

        bufferG.setColor(colors[i]);

        bufferG.fillArc(x, y, width, height, start, stepSize);

        start += stepSize;

      }

    }

    g.drawImage(buffer, 100, 100, 200, 200, this);

    g.drawImage(buffer, 600, 100, 200, 200, Color.blue, this);


  }


  public void go() {


    TimerTask task = new TimerTask() {

      public void run() {

        Color c = colors[0];

        synchronized (colors) {

          System.arraycopy(colors, 1, colors, 0, colors.length - 1);

          colors[colors.length - 1] = c;

        }

        repaint();

      }


    };

    Timer timer = new Timer();

    timer.schedule(task, 0, DELAY);

    setLayout(null);

  }


  public static void main(String args[]) {

   new BufferedAnimate();


  }

}

I oto pytanie: Jak dodać do tego kodu bitmapę albo chociaż jakieś tło kolorowe :?:

Kiedy kompiluje wyskakuje mi okno aplikacji a tło jest przezroczyste i widzę okno Eclipse'a zamiast tła :?

Z góry dzięki za wszelką pomoc :slight_smile: