-1

I'm trying to build a simple GUI interface. I have added a background image to the JPanel using the paintComponent method.

The problem is when the output is built it shows only a small window as follows:

outputWindow

I have to resize the output window to show the full image. How can I make the image fit the window?

Here is my new source code:

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.*;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.*;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;


public class test extends JFrame {

public test(){
    super("Staff Management");

    this.setContentPane(new staff());
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setResizable(false);
    this.setLocationRelativeTo(null);

    this.pack();  
}

public class staff extends JPanel{
    private ImageIcon i;

public staff() {
    i = new ImageIcon("D:\\staff-directory.jpg");
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(i.getIconWidth(),i.getIconHeight());
}
     public void paintComponent(Graphics g){
         super.paintComponent(g);
         i.paintIcon(this,g,0,0);

     }

}
}
Charlie
  • 3,113
  • 3
  • 38
  • 60
  • Why are you doing custom painting? Just use a JLabel with an Icon. – camickr Mar 18 '15 at 14:46
  • Then I can't add buttons over the image – Charlie Mar 18 '15 at 15:07
  • `I can't add buttons over the image` Sure you can. Just set the layout manager of the label. This approach will work because you are painting the image at its actual size. You would need to do custom painting if you dynamically resize the image or scale the image. – camickr Mar 18 '15 at 15:10
  • It is just that almost all the answers on this website says to use custom painting of JPanel so I spent a day learning how to do this.. – Charlie Mar 18 '15 at 15:14
  • if you could show me how to do this with JLabel and Icon that would be a great help – Charlie Mar 18 '15 at 15:15
  • Also the border doesn't appear at every build...Just sometimes – Charlie Mar 19 '15 at 09:35

2 Answers2

2

When you paint component you need to override getPreferedSize() method to define size of your custom component. You can change your class like next:

public class staff extends JPanel{

    private ImageIcon i;

    public staff() {
        i = new ImageIcon("d:\\staff.jpg");
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(i.getIconWidth(),i.getIconHeight());
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        i.paintIcon(this, g, 0, 0);
    }

}

Also use just pack() instead of this.setSize(894,553);.

alex2410
  • 10,904
  • 3
  • 25
  • 41
  • one more problem...Now there is extra space on the left hand side and at the bottom of image – Charlie Mar 18 '15 at 09:12
  • Try to call it before `setLocationRelativeTo()` and `pack()` – alex2410 Mar 18 '15 at 09:30
  • @DeltaCharlie instead of using your panel as content pane(`setContentPane(new staff());`) just add it to `JFrame`. – alex2410 Mar 18 '15 at 10:02
  • Note that it needs to be `setResizable(false)` **then** `pack()` to work correctly, since the first method will change the size of the frame borders. More generally: 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 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). – Andrew Thompson Mar 18 '15 at 11:29
  • @AndrewThompson the commands are in the same order you suggested but the extra space is still there – Charlie Mar 18 '15 at 11:40
  • And ..where is the MCVE? – Andrew Thompson Mar 18 '15 at 12:19
  • That is as minimal as I can make the code and it still produces the same error – Charlie Mar 18 '15 at 14:10
  • *"That is as minimal as I can make the code.."* Your uncompilable code snippet may be minimal, but it also needs to be C, V, and an E! Follow the link to read about the 3 parts you missed. – Andrew Thompson Mar 19 '15 at 05:02
  • @AndrewThompson I have edited the code..Please check if it is compilable now – Charlie Mar 19 '15 at 09:32
  • It is now compilable, though to be 'complete' it needs a `main(String[])` to put it on-screen., – Andrew Thompson Mar 19 '15 at 10:05
  • I fixed the the extra space around the image by removing this.pack() and I used setSize() command with image dimensions to fit the image to the window – Charlie Mar 29 '15 at 17:55
2

It is just that almost all the answers on this website says to use custom painting of JPanel so I spent a day learning how to do this..

These answers usually suggest you just draw the image using the Graphics.drawImage(...) method. Even the answers you got in your last question suggest this, so I have no idea why you would now try to paint an Icon. There is no reason to create an Icon to hold the image.

if you could show me how to do this with JLabel and Icon that would be a great help

There is no trick. It is just like using a JPanel:

JLabel label = new JLabel( ... );
label.setLayout( new FlowLayout() );
label.add( new JButton("one") );
label.add( new JButton("two") );

Read the Swing tutorial on How to Use Icons if you don't know how to add an Icon to a JLabel.

You even got this advice in your question here: https://stackoverflow.com/a/29091847/131872, so why are you asking this question again?

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • I tried that way but its just not working for me...With the custom painting I get the output I want but with extra border at the side – Charlie Mar 19 '15 at 09:19