Hi Guys
I have a program thats supposed to generate 9 random objects made from lines and then allow me to save the panel showing the state of all 9 objects.
Currently all 9 objects are being generated and when i click save the panel is being saved to a png file. However it seems that the png file and the panel are in two different states. Ive attached pictures at the end.
code for my current GUI where the save button is located:
`import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class GUI extends JFrame{
Random rGen;
BiomorphObject[] morphs = new BiomorphObject[9];
double[] genes;
//Create containers to hold the components
JPanel biomorphPanel = new JPanel(new BorderLayout());
JPanel buttonsPanel = new JPanel(new BorderLayout());
JPanel HOF = new JPanel(new BorderLayout());
public GUI(){
JFrame mainFrame = new JFrame("Biomorph");
BiomorphObject object1 = new BiomorphObject();
BiomorphObject object2 = new BiomorphObject();
BiomorphObject object3 = new BiomorphObject();
BiomorphObject object4 = new BiomorphObject();
BiomorphObject object5 = new BiomorphObject();
BiomorphObject object6 = new BiomorphObject();
BiomorphObject object7 = new BiomorphObject();
BiomorphObject object8 = new BiomorphObject();
BiomorphObject object9 = new BiomorphObject();
JButton hof = new JButton("Hall of Fame");
//Create the buttons
JButton randomButton = new JButton("Random Biomorph");
JButton saveButton = new JButton("Save");
JButton loadButton = new JButton("Load");
JButton printButton = new JButton("Print?");
JButton exitButton = new JButton("Exit");
//Set the properties of the components
randomButton.setToolTipText("Create New Random Biomorph");
randomButton.setPreferredSize(new Dimension(150, 40));
randomButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
biomorphPanel.repaint();
}
});
saveButton.setToolTipText("Save the current Biomorph");
saveButton.setPreferredSize(new Dimension(150, 40));
saveButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
BufferedImage bi = new BufferedImage(biomorphPanel.getSize().width, biomorphPanel.getSize().height, BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
biomorphPanel.paint(g);
g.dispose();
try{ImageIO.write(bi,"png",new File("test.png"));}catch (Exception e1) {}
}
});
loadButton.setToolTipText("Load previously made Biomorph");
loadButton.setPreferredSize(new Dimension(150, 40));
printButton.setToolTipText("Print Biomorph");
printButton.setPreferredSize(new Dimension(150, 40));
exitButton.setToolTipText("Close the Application");
exitButton.setPreferredSize(new Dimension(150, 40));
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//add the buttons to the panel.
buttonsPanel.add(randomButton, BorderLayout.WEST);
buttonsPanel.add(saveButton, BorderLayout.CENTER);
buttonsPanel.add(loadButton, BorderLayout.CENTER);
buttonsPanel.add(printButton, BorderLayout.CENTER);
buttonsPanel.add(exitButton, BorderLayout.EAST);
buttonsPanel.setLayout(new FlowLayout());
biomorphPanel.add(object1);
biomorphPanel.add(object2);
biomorphPanel.add(object3);
biomorphPanel.add(object4);
biomorphPanel.add(object5);
biomorphPanel.add(object6);
biomorphPanel.add(object7);
biomorphPanel.add(object8);
biomorphPanel.add(object9);
biomorphPanel.setLayout(new GridLayout(3,3));
HOF.add(hof,BorderLayout.CENTER);
mainFrame.getContentPane().add(HOF, BorderLayout.EAST);
mainFrame.getContentPane().add(biomorphPanel, BorderLayout.CENTER);
mainFrame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
mainFrame.setVisible(true);
mainFrame.setSize(800,400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setResizable(false);
}
}
This is what the panel looks like:
http://s16.postimg.org/iqt2xpc2d/panel.png
and this is what gets saved:
http://s15.postimg.org/dprm8n95n/test.png
If you need more information then please ask
any help will be appreciated.