0

I am trying to display two different images on my screen. one of which is a banner that goes at the top of my JFrame, and another that I just placed randomly below the banner for testing purposes. The issue I am having is that while I can display a single image on the screen by adding an object of class WindowStructure to my window, I am not able to display more than one image at a time. Only the last image added to the window is displayed on the screen:

Here is the window class:

import javax.swing.JFrame;

public class Window extends JFrame {
    public Window(String name) {
    super(name);
    setSize(1200, 700);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    WindowStructure banner = new WindowStructure("Beatles Logo.jpg", 0, 0, getWidth(), 75);
    WindowStructure fireball = new WindowStructure("fireball.png", 100, 100, 100, 100);
    add(banner); //banner
    add(fireball);

    setVisible(true);

    while(true){
        repaint();
    }
}

public void paint(Graphics g

) {
        super.paintComponents(g);
    }
}

Here's the actual class that creates the image:

public class WindowStructure extends JPanel {
    ImageIcon imageIcon;
    int xLoc, yLoc, xSize, ySize;

    public WindowStructure(String bannerImg, int xLoc, int yLoc, int xSize, int ySize){
        URL bannerImgURL = getClass().getResource(bannerImg);
        imageIcon = new ImageIcon(bannerImgURL);
        this.xLoc = xLoc;
        this.yLoc = yLoc;
        this.xSize = xSize;
        this.ySize = ySize;
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(imageIcon.getImage(), xLoc, yLoc, xSize, ySize, null);
    }
}
2
  • Add constraints. JFrame uses BorderLayout by default, so add(banner, BorderLayout.NORTH) to get your banner at the top. Gotta add constraints to both of them. Might wanna look into layouts Commented Mar 10, 2015 at 19:45
  • Get rid of the "repaint" loop. Also God rid of the paint method in the frame, this is screwing with how the paint chain works Commented Mar 10, 2015 at 20:42

2 Answers 2

2

The default layout manager for JFrame is BorderLayout. As the documentation says: "BorderLayout interprets the absence of a string specification the same as the constant CENTER". For instance:

add(banner);  // Same as p.add(banner, BorderLayout.CENTER); 
add(fireball);  // Same as p.add(fireball, BorderLayout.CENTER);

You can fix this if you specify the location as a second argument to add():

add(banner, BorderLayout.NORTH);
add(fireball, BorderLayout.CENTER);

Or you can use another layout manager for the JFrame by invoking setLayout(LayoutManager) in your Window class constructor.

public class Window extends JFrame {
    public Window(String name) {
    super(name);
    setLayout(new FlowLayout()); // or another the layout that best fit your needs...
    ...

Guide about layout managers: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Sign up to request clarification or add additional context in comments.

Comments

0

The JFrame javadocs state that the default layout manager used is BorderLayout. To add multiple components, you have to specify a different place in the layout to put each one (NORTH, SOUTH, EAST, WEST, CENTER). By default it's BorderLayout.CENTER if not specified, which is why you only see the last one added.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.