0

I am trying to make this gif active in my jpanel but anytime i run the program the panel just shows the image but not in motion. i have tried this in a separate occasion using frame instead of panels and it worked(not sure if they correlate). To recap the image shows, but as a still image not as a "gif"

update: i have the gif working now but it covers the buttons like "client" "help" and "owner" is it possible to make the gif all the way in the back and the buttons sit on top of the gif?

-Code

public void panel1() {
        ImagePanel panel = new ImagePanel(new ImageIcon("MenuBG.gif").getImage());
        firstPanel = new JPanel();
        firstPanel.setLayout(null);
        client = new JButton("Client");
        owner = new JButton("Owner");
        client.setSize(90, 20);
        client.setLocation(xPosAlignLeft, 50);
        owner.setSize(90, 20);
        owner.setLocation(xPosAlignLeft, 115);
        firstPanel.add(client);
        firstPanel.add(owner);
        createHelpButton();
        help.setSize(90, 20);
        help.setLocation(xPosAlignLeft, 180);
        firstPanel.add(help);
        firstPanel.add(panel);
    }
    class ImagePanel extends JPanel {
        
private Image img;
public ImagePanel(String img) {
    this(new ImageIcon(img).getImage());
}
public ImagePanel(Image 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,null);
}
    }
Jualston
  • 37
  • 6
  • You can't, easily do this through `paintComponent`, a `Label` will do it for you for free – MadProgrammer Oct 16 '20 at 08:59
  • 3
    `g.drawImage(img,0,0,null);` should be `g.drawImage(img,0,0,this);` **BTW** 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data to replace the DB. 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). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Oct 16 '20 at 11:25
  • 3
    .. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 16 '20 at 11:26
  • @AndrewThompson the reason why i left the code there is because of what happened when i changed my g.drawImage to the correct format, it now blocks the buttons in my panel. The image works fine with the information you gave but it is over my buttons not under it. – Jualston Oct 16 '20 at 17:55
  • *"left the code there"* I wasn't advising to remove the code, but to change it to code we could test. I'll put no further thought into it until there is code I can run. – Andrew Thompson Oct 17 '20 at 00:23
  • Does [this](https://stackoverflow.com/questions/2935232/show-animated-gif) q&a help ? – c0der Oct 19 '20 at 08:20
  • Not too sure on the credibility of this but, i found out that gifs would glitch my buttons out if i tried to hover the buttons over the actual jlabel gif. @c0der – Jualston Oct 20 '20 at 21:43
  • Please add an [mre] demonstrating it – c0der Oct 21 '20 at 03:39

0 Answers0