0

I've got this list of JPanels and I'd like to save them without displaying them make Jpanel -> Save it(without displaying it)

I tried using BufferedImage and painting it but the image returned is always black, the method I used for that is "save" and is displayed in the code below.

I'd like to know how to do fix this problem.

EDIT:

@camickr thanks to your suggestion and this thread

Why does the JTable header not appear in the image?

I was able to fix the problem, here is the fixed code:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class Solution extends JFrame
{
    int i;
    ArrayList <Individual> AKA;
    Container jgraph;
    JButton gauche;
    JButton droite;
    JMenuBar barre;
    JMenu fichier;
    JMenuItem enregistrer;
    JPanel jpgenerale ;
    Solution current;
    Component middle;

    public Solution(ArrayList <Individual> graph) throws HeadlessException 
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        barre = new JMenuBar();

        fichier = new JMenu("Fichier");

        enregistrer = new JMenuItem("Enregistrer");
        enregistrer.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK));

        fichier.add(enregistrer);
        barre.add(fichier);

        AKA = graph;
        current = this;
        gauche = new JButton("Gauche");
        droite = new JButton("Droite");
        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.PAGE_END;

        c.gridx = 1;
        c.gridy = 2;
        gauche.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(java.awt.event.ActionEvent e)
                {
                    i = (AKA.size() + (i - 1)) % AKA.size();
                    jpgenerale.removeAll();
                    jpgenerale.validate();

                    jpgenerale.add(AKA.get(i).chopper.getContentPane());

                    jpgenerale.revalidate();
                    jpgenerale.repaint();
                }
            });
        getContentPane().add(gauche, c);

        c.gridx = 3;
        c.gridy = 2;
        droite.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(java.awt.event.ActionEvent e)
                {
                    i = (i+1)% AKA.size();
                    jpgenerale.removeAll();
                    jpgenerale.validate();

                    jpgenerale.add(AKA.get(i).chopper.getContentPane());

                    jpgenerale.revalidate();
                    jpgenerale.repaint();
                }
            });
        getContentPane().add(droite, c);

        jpgenerale = new JPanel();
        c.anchor = GridBagConstraints.CENTER;
        jpgenerale.setLayout(new BoxLayout(jpgenerale,BoxLayout.LINE_AXIS));
        jpgenerale.setPreferredSize(new Dimension(400, 400));
        jpgenerale.add(AKA.get(0).chopper.getContentPane());

        c.ipady = 40;
        c.gridx = 2;
        c.gridy = 1;
        getContentPane().add(jpgenerale,c);

        setTitle("AGPM");// le titre on va change plutard
        setSize(600,600);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

        middle = AKA.get(0).chopper.getContentPane();
        scan(middle);
        middle = AKA.get(1).chopper.getContentPane();
        scan(middle);

    }


    private static void scan(Component pan1)
    {
        JPanel temp = new JPanel();
        temp.setBackground(Color.WHITE);//so that the image and JPanel background are unified
        temp.setPreferredSize(new Dimension(400, 400));
        temp.add(pan1);

        BufferedImage bi = ScreenImage.createImage(temp);

        try {
            ScreenImage.writeImage(bi, "image.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

I edited the thread's title so that it's clearer, if you have any suggestion they're welcomed.

Thanks again everyone:)

Abdelghani Bekka
  • 576
  • 9
  • 18
  • And where is the code we are supposed to review in order to tell you what the solution might be? –  Mar 25 '18 at 16:12
  • I'll post it right away – Abdelghani Bekka Mar 25 '18 at 16:30
  • @AbdelghaniBekka The code is too long to understand without the basis: what do you mean by save the JPanel? – Robo Mop Mar 25 '18 at 17:40
  • For some reason, you have added an empty ActionListener in `fichier`. Also, there is a huge block of commented out code. Have you left them there for any reason? If so, then tell us if we can ignore them, or should consider them. If not, please consider creating a more minimal version of your code (without the unnecessary bits). – Robo Mop Mar 25 '18 at 17:45
  • Made the code more compact, thank you for your feedback. – Abdelghani Bekka Mar 25 '18 at 20:04

1 Answers1

1

If the component hasn't been added to the visible frame, then the size of the component is (0, 0) so there is nothing to paint. Also, the components haven't been positioned on the panel since the layout manager hasn't been invoked.

So you first need to:

  1. set the size of the component equal to the preferred size
  2. invoke doLayout() on the component

Or, you can check out Screen Image which does the above for you and can be used to create an image of any component.

camickr
  • 321,443
  • 19
  • 166
  • 288