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:)