FileDocCategorySizeDatePackage
ImageEditorPanel.javaAPI DocAndroid 1.5 API44865Wed May 06 22:41:10 BST 2009com.android.draw9patch.ui

ImageEditorPanel

public class ImageEditorPanel extends JPanel

Fields Summary
private static final String
EXTENSION_9PATCH
private static final int
DEFAULT_ZOOM
private static final float
DEFAULT_SCALE
private String
name
private BufferedImage
image
private boolean
is9Patch
private ImageViewer
viewer
private StretchesViewer
stretchesViewer
private JLabel
xLabel
private JLabel
yLabel
private TexturePaint
texture
private List
patches
private List
horizontalPatches
private List
verticalPatches
private List
fixed
private boolean
verticalStartWithPatch
private boolean
horizontalStartWithPatch
private Pair
horizontalPadding
private Pair
verticalPadding
Constructors Summary
ImageEditorPanel(MainFrame mainFrame, BufferedImage image, String name)

    

          
        this.image = image;
        this.name = name;

        setTransferHandler(new ImageTransferHandler(mainFrame));

        checkImage();

        setOpaque(false);
        setLayout(new BorderLayout());

        loadSupport();
        buildImageViewer();
        buildStatusPanel();
    
Methods Summary
private voidbuildImageViewer()

        viewer = new ImageViewer();

        JSplitPane splitter = new JSplitPane();
        splitter.setContinuousLayout(true);
        splitter.setResizeWeight(0.8);
        splitter.setBorder(null);

        JScrollPane scroller = new JScrollPane(viewer);
        scroller.setOpaque(false);
        scroller.setBorder(null);
        scroller.getViewport().setBorder(null);
        scroller.getViewport().setOpaque(false);

        splitter.setLeftComponent(scroller);
        splitter.setRightComponent(buildStretchesViewer());

        add(splitter);
    
private voidbuildStatusPanel()

        JPanel status = new JPanel(new GridBagLayout());
        status.setOpaque(false);

        JLabel label = new JLabel();
        label.setForeground(Color.WHITE);
        label.setText("Zoom: ");
        label.putClientProperty("JComponent.sizeVariant", "small");
        status.add(label, new GridBagConstraints(0, 0, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                new Insets(0, 6, 0, 0), 0, 0));

        label = new JLabel();
        label.setForeground(Color.WHITE);
        label.setText("100%");
        label.putClientProperty("JComponent.sizeVariant", "small");
        status.add(label, new GridBagConstraints(1, 0, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 0), 0, 0));

        JSlider zoomSlider = new JSlider(1, 16, DEFAULT_ZOOM);
        zoomSlider.setSnapToTicks(true);
        zoomSlider.putClientProperty("JComponent.sizeVariant", "small");
        zoomSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent evt) {
                viewer.setZoom(((JSlider) evt.getSource()).getValue());
            }
        });
        status.add(zoomSlider, new GridBagConstraints(2, 0, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 0), 0, 0));

        JLabel maxZoomLabel = new JLabel();
        maxZoomLabel.setForeground(Color.WHITE);
        maxZoomLabel.putClientProperty("JComponent.sizeVariant", "small");
        maxZoomLabel.setText("800%");
        status.add(maxZoomLabel, new GridBagConstraints(3, 0, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 0), 0, 0));

        label = new JLabel();
        label.setForeground(Color.WHITE);
        label.setText("Patch scale: ");
        label.putClientProperty("JComponent.sizeVariant", "small");
        status.add(label, new GridBagConstraints(0, 1, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                new Insets(0, 6, 0, 0), 0, 0));

        label = new JLabel();
        label.setForeground(Color.WHITE);
        label.setText("2x");
        label.putClientProperty("JComponent.sizeVariant", "small");
        status.add(label, new GridBagConstraints(1, 1, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 0), 0, 0));

        zoomSlider = new JSlider(200, 600, (int) (DEFAULT_SCALE * 100.0f));
        zoomSlider.setSnapToTicks(true);
        zoomSlider.putClientProperty("JComponent.sizeVariant", "small");
        zoomSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent evt) {
                stretchesViewer.setScale(((JSlider) evt.getSource()).getValue() / 100.0f);
            }
        });
        status.add(zoomSlider, new GridBagConstraints(2, 1, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 0), 0, 0));

        maxZoomLabel = new JLabel();
        maxZoomLabel.setForeground(Color.WHITE);
        maxZoomLabel.putClientProperty("JComponent.sizeVariant", "small");
        maxZoomLabel.setText("6x");
        status.add(maxZoomLabel, new GridBagConstraints(3, 1, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 0), 0, 0));

        JCheckBox showLock = new JCheckBox("Show lock");
        showLock.setOpaque(false);
        showLock.setForeground(Color.WHITE);
        showLock.setSelected(true);
        showLock.putClientProperty("JComponent.sizeVariant", "small");
        showLock.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                viewer.setLockVisible(((JCheckBox) event.getSource()).isSelected());
            }
        });
        status.add(showLock, new GridBagConstraints(4, 0, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                new Insets(0, 12, 0, 0), 0, 0));

        JCheckBox showPatches = new JCheckBox("Show patches");
        showPatches.setOpaque(false);
        showPatches.setForeground(Color.WHITE);
        showPatches.putClientProperty("JComponent.sizeVariant", "small");
        showPatches.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                viewer.setPatchesVisible(((JCheckBox) event.getSource()).isSelected());
            }
        });
        status.add(showPatches, new GridBagConstraints(4, 1, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                new Insets(0, 12, 0, 0), 0, 0));

        JCheckBox showPadding = new JCheckBox("Show content");
        showPadding.setOpaque(false);
        showPadding.setForeground(Color.WHITE);
        showPadding.putClientProperty("JComponent.sizeVariant", "small");
        showPadding.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                stretchesViewer.setPaddingVisible(((JCheckBox) event.getSource()).isSelected());
            }
        });
        status.add(showPadding, new GridBagConstraints(5, 0, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                new Insets(0, 12, 0, 0), 0, 0));

        status.add(Box.createHorizontalGlue(), new GridBagConstraints(6, 0, 1, 1, 1.0f, 1.0f,
                GridBagConstraints.LINE_START, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

        label = new JLabel("X: ");
        label.setForeground(Color.WHITE);
        label.putClientProperty("JComponent.sizeVariant", "small");
        status.add(label, new GridBagConstraints(7, 0, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 0), 0, 0));

        xLabel = new JLabel("0px");
        xLabel.setForeground(Color.WHITE);
        xLabel.putClientProperty("JComponent.sizeVariant", "small");
        status.add(xLabel, new GridBagConstraints(8, 0, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 6), 0, 0));

        label = new JLabel("Y: ");
        label.setForeground(Color.WHITE);
        label.putClientProperty("JComponent.sizeVariant", "small");
        status.add(label, new GridBagConstraints(7, 1, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 0), 0, 0));

        yLabel = new JLabel("0px");
        yLabel.setForeground(Color.WHITE);
        yLabel.putClientProperty("JComponent.sizeVariant", "small");
        status.add(yLabel, new GridBagConstraints(8, 1, 1, 1, 0.0f, 0.0f,
                GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 6), 0, 0));

        add(status, BorderLayout.SOUTH);
    
private javax.swing.JComponentbuildStretchesViewer()

        stretchesViewer = new StretchesViewer();
        JScrollPane scroller = new JScrollPane(stretchesViewer);
        scroller.setBorder(null);
        scroller.getViewport().setBorder(null);
        scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        return scroller;
    
private voidcheckImage()

        is9Patch = name.endsWith(EXTENSION_9PATCH);
        if (!is9Patch) {
            convertTo9Patch();
        } else {
            ensure9Patch();
        }
    
java.io.FilechooseSaveFile()

        if (is9Patch) {
            return new File(name);
        } else {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileFilter(new PngFileFilter());
            int choice = chooser.showSaveDialog(this);
            if (choice == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                if (!file.getAbsolutePath().endsWith(EXTENSION_9PATCH)) {
                    String path = file.getAbsolutePath();
                    if (path.endsWith(".png")) {
                        path = path.substring(0, path.lastIndexOf(".png")) + EXTENSION_9PATCH;
                    } else {
                        path = path + EXTENSION_9PATCH;
                    }
                    name = path;
                    is9Patch = true;
                    return new File(path);
                }
                is9Patch = true;
                return file;
            }
        }
        return null;
    
private voidconvertTo9Patch()

        BufferedImage buffer = GraphicsUtilities.createTranslucentCompatibleImage(
                image.getWidth() + 2, image.getHeight() + 2);

        Graphics2D g2 = buffer.createGraphics();
        g2.drawImage(image, 1, 1, null);
        g2.dispose();

        image = buffer;
        name = name.substring(0, name.lastIndexOf('.")) + ".9.png";
    
private voidensure9Patch()

        int width = image.getWidth();
        int height = image.getHeight();
        for (int i = 0; i < width; i++) {
            int pixel = image.getRGB(i, 0);
            if (pixel != 0 && pixel != 0xFF000000) {
                image.setRGB(i, 0, 0);
            }
            pixel = image.getRGB(i, height - 1);
            if (pixel != 0 && pixel != 0xFF000000) {
                image.setRGB(i, height - 1, 0);
            }
        }
        for (int i = 0; i < height; i++) {
            int pixel = image.getRGB(0, i);
            if (pixel != 0 && pixel != 0xFF000000) {
                image.setRGB(0, i, 0);
            }
            pixel = image.getRGB(width - 1, i);
            if (pixel != 0 && pixel != 0xFF000000) {
                image.setRGB(width - 1, i, 0);
            }
        }
    
java.awt.image.RenderedImagegetImage()

        return image;
    
private voidloadSupport()

        try {
            URL resource = getClass().getResource("/images/checker.png");
            BufferedImage checker = GraphicsUtilities.loadCompatibleImage(resource);
            texture = new TexturePaint(checker, new Rectangle2D.Double(0, 0,
                    checker.getWidth(), checker.getHeight()));
        } catch (IOException e) {
            e.printStackTrace();
        }