org.newdawn.slick.tools.packulike.Packer Maven / Gradle / Ivy
The newest version!
package org.newdawn.slick.tools.packulike;
import java.awt.Component;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.ListSelectionModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileFilter;
/**
* A simple GUI on the front of the packing tool
*
* @author kevin
*/
public class Packer extends JFrame {
/** The panel showing the currently generated sprite sheet */
private SheetPanel sheetPanel;
/** The sprites currently displayed */
private DefaultListModel sprites = new DefaultListModel();
/** The visual list of the sprites */
private JList list = new JList(sprites);
/** The width of the texture being generated */
private int twidth;
/** The height of the texture being generated */
private int theight;
/** The width texture sizes model */
private DefaultComboBoxModel sizes = new DefaultComboBoxModel();
/** The height texture sizes model */
private DefaultComboBoxModel sizes2 = new DefaultComboBoxModel();
/** The visual selection for texture width */
private JComboBox widths = new JComboBox(sizes);
/** The visual selection for texture height */
private JComboBox heights = new JComboBox(sizes2);
/** The visual selection for border size */
private JSpinner border = new JSpinner(new SpinnerNumberModel(0,0,50,1));
/** The chooser used to select sprites */
private JFileChooser chooser = new JFileChooser(".");
/** The chooser used to save the sprite sheet */
private JFileChooser saveChooser = new JFileChooser(".");
/** The packing tool */
private Pack pack = new Pack();
/**
* Create the new GUI for packer
*/
public Packer() {
super("Pack-U-Like");
saveChooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
return (f.getName().endsWith(".png"));
}
public String getDescription() {
return "PNG Images (*.png)";
}
});
chooser.setMultiSelectionEnabled(true);
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
return (f.getName().endsWith(".png") ||
f.getName().endsWith(".jpg") ||
f.getName().endsWith(".gif"));
}
public String getDescription() {
return "Images (*.jpg, *.png, *.gif)";
}
});
sizes.addElement(new Integer(64));
sizes.addElement(new Integer(128));
sizes.addElement(new Integer(256));
sizes.addElement(new Integer(512));
sizes.addElement(new Integer(1024));
sizes.addElement(new Integer(2048));
sizes2.addElement(new Integer(64));
sizes2.addElement(new Integer(128));
sizes2.addElement(new Integer(256));
sizes2.addElement(new Integer(512));
sizes2.addElement(new Integer(1024));
sizes2.addElement(new Integer(2048));
JMenuBar bar = new JMenuBar();
JMenu file = new JMenu("File");
bar.add(file);
JMenuItem save = new JMenuItem("Save");
file.add(save);
file.addSeparator();
JMenuItem quit = new JMenuItem("Quit");
file.add(quit);
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
save();
}
});
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setJMenuBar(bar);
JPanel panel = new JPanel();
panel.setLayout(null);
sheetPanel = new SheetPanel(this);
JScrollPane pane = new JScrollPane(sheetPanel);
pane.setBounds(5,5,530,530);
JScrollPane listScroll = new JScrollPane(list);
listScroll.setBounds(540,5,200,350);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
Object[] values = list.getSelectedValues();
ArrayList sprites = new ArrayList();
for (int i=0;i