Click here to Skip to main content
15,893,508 members
Articles / Mobile Apps / Android

Texture Atlas Maker

Rate me:
Please Sign up or sign in to vote.
4.94/5 (16 votes)
1 Apr 2012BSD6 min read 142.8K   7K   55  
A utility to create texture atlases for 2D OpenGL games
import java.awt.Frame;
import java.awt.event.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class OptionsDialog extends JDialog implements ActionListener, ItemListener
{
	private static final long serialVersionUID = 8668228916639817663L;
	private JTextField textTextToolDir;
    private JTextField textAlphaThresh;
    private JPanel panelMain, panelAlphaThresh;
    private JPanel panelPVRDir;
    private JPanel panelPVROptions;
    private JButton btnSelTextToolDir;
    private JButton btnClose;
    private JRadioButton btnPVR2bit, btnPVR4bit;
    private JCheckBox checkConvPVR;
    private ButtonGroup buttonGroupPVR; 
    private JFileChooser fc = null;
    private Properties properties;
    public OptionsDialog(Frame owner, Properties mainProperties)
	{
		super(owner, "Options");
		properties = mainProperties;
        panelAlphaThresh = new JPanel();
        panelPVRDir = new JPanel();
        panelPVROptions = new JPanel();
        
        //Add various widgets to the sub panels.
        addWidgets();
        checkPVR();

        fc = new JFileChooser(".");
		setContentPane(panelMain);
        pack();
        setVisible(false);
	}


    private void addWidgets()
    {
    	textTextToolDir = new JTextField(40);
    	textAlphaThresh = new JTextField(3);
    	btnSelTextToolDir = new JButton("...");
    	btnClose = new JButton("Close");
    	textTextToolDir.setEnabled(false);
    	textAlphaThresh.setEnabled(true);
    	textAlphaThresh.setToolTipText("Pixels below this threshold are considered transparent and will be trimmed");
    	btnSelTextToolDir.addActionListener(this);
    	btnClose.addActionListener(this);
    	btnPVR2bit = new JRadioButton("2 bit");
    	btnPVR4bit = new JRadioButton("4 Bit");
    	checkConvPVR = new JCheckBox("Convert PVRs");
    	checkConvPVR.addItemListener(this);
    	
    	String last = properties.getProperty("lastAlphaThresh");
        if (last != null)
        	textAlphaThresh.setText(last);
        else textAlphaThresh.setText("2");
              
        last = properties.getProperty("PVRType");
        if (last == null)
        	btnPVR2bit.setSelected(true);
        else 
        {
        	if (last.equals("2bit"))
        		btnPVR2bit.setSelected(true);
        	else if (last.equals("4bit"))
        		btnPVR4bit.setSelected(true);
        }

        last = properties.getProperty("TextToolDir");
        if (last != null)
        	textTextToolDir.setText(last);

        //Add a border around the select panel.
        panelAlphaThresh.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("Alpha Threshold"),
            BorderFactory.createEmptyBorder(5,5,5,5)));
        
        panelPVRDir.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Location of PVRTexTool"),
                BorderFactory.createEmptyBorder(5,5,5,5)));

        panelPVROptions.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("PVR Options"),
                BorderFactory.createEmptyBorder(5,5,5,5)));
        
        panelAlphaThresh.add(textAlphaThresh);
        panelPVRDir.add(textTextToolDir);
        panelPVRDir.add(btnSelTextToolDir);
        panelPVROptions.add(checkConvPVR);
        panelPVROptions.add(btnPVR2bit);
        panelPVROptions.add(btnPVR4bit);

        buttonGroupPVR = new ButtonGroup();
        buttonGroupPVR.add(btnPVR2bit);
        buttonGroupPVR.add(btnPVR4bit);
        
        panelMain = new JPanel();
        panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.PAGE_AXIS));
        panelMain.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

        //Add the select and display panels to the main panel.
        panelMain.add(panelAlphaThresh);
        panelMain.add(panelPVRDir);
        panelMain.add(panelPVROptions);
        panelMain.add(btnClose);
    }
    
    public void actionPerformed(ActionEvent event)
	{
        if (event.getSource() == btnSelTextToolDir)
        {
            fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            fc.setDialogType(JFileChooser.SAVE_DIALOG);
            File file = new File(textTextToolDir.getText());
       
            if (file.isDirectory())
            	fc.setCurrentDirectory(file);
        	int returnVal = fc.showOpenDialog(null);
        	if (returnVal != JFileChooser.APPROVE_OPTION) return;
            file = fc.getSelectedFile();
            String strDir = file.getPath();
            if (file.isFile())
            {
	            int pos = strDir.lastIndexOf(File.separatorChar);
	            if (pos != -1)
	            {
	            	strDir = strDir.substring(0, pos+1);
	            }
            }
            if (!strDir.endsWith(File.separator)) strDir += File.separator;

            textTextToolDir.setText(strDir);
            properties.setProperty("TextToolDir", strDir);
        	try
        	{
				properties.store(new FileOutputStream(AtlasFrame.savePlace), null);
			} catch (IOException e)
			{
                System.out.println("error: "+e.toString());
			}
		}
        if (event.getSource() == btnClose)
        {
        	setVisible(false);
            properties.setProperty("lastAlphaThresh", textAlphaThresh.getText());

            if (btnPVR2bit.isSelected())
                properties.setProperty("PVRType", "2bit");
            else 
                properties.setProperty("PVRType", "4bit");
            
        	try
        	{
    			properties.store(new FileOutputStream(AtlasFrame.savePlace), null);
    		} catch (IOException ex)
    		{
                System.out.println("error: "+ex.toString());
    		}
    		
        }
		
	}

	public String getTextToolDir()
	{
		return textTextToolDir.getText();
	}
	public boolean is4Bit()
	{
		return btnPVR4bit.isSelected();
	}
	public int getAlphaThresh()
	{
    	int alphaThresh = Integer.parseInt(textAlphaThresh.getText());
		return alphaThresh;
	}

	public boolean convPVR()
	{
		return checkConvPVR.isSelected();
	}
	
    private void checkPVR()
    {
    	btnPVR2bit.setEnabled(checkConvPVR.isSelected());
    	btnPVR4bit.setEnabled(checkConvPVR.isSelected());
    }
    public void itemStateChanged(ItemEvent ie) 
    {
    	checkPVR();
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer Astronautz
Spain Spain
After working in the software industry for many years, I've started my own games company that specialises in strategy games for mobile platforms.

Comments and Discussions