0

I'm trying to make a Swing program, I currently have an image stretched across the whole screen that acts as a background, and now I just want an smaller image to be drawn on top of this image

My video constructor and draws the background and foreground images:

    public Video(BufferedImage foreground, BufferedImage background){
        pane = this.getContentPane(); //get the content pane to place components
        pane.setLayout(null); //use absolute positioning (using Insets)
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.setVisible(true);
        this.setBounds(0,0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
        DrawImage panel = new DrawImage(background); 
        add(panel);
        this.image = foreground;
        DrawForeground fg = new DrawForeground(image); 
        add(fg);
        fg.setVisible(true);//don't make image visible until showImageAt() gives it a place to be


    }

My drawImage method ( draw foreground is very similar but with a different size)

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JPanel;


class DrawImage extends JPanel {

    private BufferedImage img;


    public DrawImage(BufferedImage img) {
        this.img = img;
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
    }

    public void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0,this.getWidth(),this.getHeight(), null);
    }

}
Jon Sherman
  • 107
  • 2
  • 11
  • 1
    And what is exactly the question? – Ilario Aug 22 '15 at 00:37
  • 2
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) `public void paintComponent(Graphics g) { g.drawImage(img, 0, 0,this.getWidth(),this.getHeight(), null);` The first line of the overridden method should be `super.paintComponent(g);`. Use `this` instead of `null` as the `ImageObserver` when drawing images. – Andrew Thompson Aug 22 '15 at 02:59
  • Try the approach shown [here](http://stackoverflow.com/q/2658554/230513). – trashgod Aug 22 '15 at 03:06

0 Answers0