Java tutorial
/** * Copyright 2014/2015 JeremGamer * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package io.github.jeremgamer.editor.panels; import io.github.jeremgamer.editor.Editor; import io.github.jeremgamer.resources.images.ImageGetter; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.filechooser.FileNameExtensionFilter; import org.apache.commons.io.IOUtils; public class IconFrame extends JDialog { /** * */ private static final long serialVersionUID = 280152408976762529L; JButton browse128 = new JButton(UIManager.getIcon("FileView.directoryIcon")); JButton remove128 = new JButton(""); IconPanel x128 = new IconPanel(128) { /** * */ private static final long serialVersionUID = -2428919737259933550L; @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (new File("projects/" + Editor.getProjectName() + "/128.png").exists()) { try { g.drawImage(ImageIO.read(new File("projects/" + Editor.getProjectName() + "/128.png")), 0 + 10, 0 + 20, 128, 128, null); } catch (IOException e) { e.printStackTrace(); } } } }; JButton browse64 = new JButton(UIManager.getIcon("FileView.directoryIcon")); JButton remove64 = new JButton(""); IconPanel x64 = new IconPanel(64) { /** * */ private static final long serialVersionUID = -2866537951922141500L; @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (new File("projects/" + Editor.getProjectName() + "/64.png").exists()) { try { g.drawImage(ImageIO.read(new File("projects/" + Editor.getProjectName() + "/64.png")), 32 + 10, 32 + 20, 64, 64, null); } catch (IOException e) { e.printStackTrace(); } } } }; JButton browse32 = new JButton(UIManager.getIcon("FileView.directoryIcon")); JButton remove32 = new JButton(""); IconPanel x32 = new IconPanel(32) { /** * */ private static final long serialVersionUID = 9156735975137299432L; @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (new File("projects/" + Editor.getProjectName() + "/32.png").exists()) { try { g.drawImage(ImageIO.read(new File("projects/" + Editor.getProjectName() + "/32.png")), 48 + 10, 48 + 20, 32, 32, null); } catch (IOException e) { e.printStackTrace(); } } } }; JButton browse16 = new JButton(UIManager.getIcon("FileView.directoryIcon")); JButton remove16 = new JButton(""); IconPanel x16 = new IconPanel(16) { /** * */ private static final long serialVersionUID = -5297467300427934673L; @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (new File("projects/" + Editor.getProjectName() + "/16.png").exists()) { try { g.drawImage(ImageIO.read(new File("projects/" + Editor.getProjectName() + "/16.png")), 56 + 10, 56 + 20, 16, 16, null); } catch (IOException e) { e.printStackTrace(); } } } }; public void copyImage(File file, String name) { try { FileOutputStream fos = new FileOutputStream( new File("projects/" + Editor.getProjectName() + "/" + name)); FileInputStream fis = new FileInputStream(file.getAbsolutePath()); IOUtils.copy(fis, fos); fos.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } public IconFrame(JFrame parent) { this.setModal(true); this.setResizable(false); ArrayList<BufferedImage> icons = new ArrayList<BufferedImage>(); try { icons.add(ImageIO.read(ImageGetter.class.getResource("icon16.png"))); icons.add(ImageIO.read(ImageGetter.class.getResource("icon32.png"))); icons.add(ImageIO.read(ImageGetter.class.getResource("icon64.png"))); icons.add(ImageIO.read(ImageGetter.class.getResource("icon128.png"))); } catch (IOException e1) { e1.printStackTrace(); } this.setIconImages((List<? extends Image>) icons); this.setTitle("Icnes"); this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); JPanel content = new JPanel(); content.setLayout(new BoxLayout(content, BoxLayout.LINE_AXIS)); content.setBorder(BorderFactory.createTitledBorder("")); try { remove128.setIcon(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png")))); remove64.setIcon(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png")))); remove32.setIcon(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png")))); remove16.setIcon(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png")))); } catch (IOException e) { e.printStackTrace(); } browse128.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { String path = null; JFileChooser chooser = new JFileChooser(Editor.lastPath); FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "jpeg", "bmp"); chooser.setFileFilter(filter); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int option = chooser.showOpenDialog(null); if (option == JFileChooser.APPROVE_OPTION) { path = chooser.getSelectedFile().getAbsolutePath(); Editor.lastPath = chooser.getSelectedFile().getParent(); copyImage(new File(path), "128.png"); try { x128.repaint(); x128.getGraphics().drawImage(ImageIO.read(new File(path)), 0 + 10, 0 + 20, 128, 128, null); } catch (IOException e) { e.printStackTrace(); } } } }); remove128.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { File file = new File("projects/" + Editor.getProjectName() + "/128.png"); file.delete(); x128.repaint(); } }); browse64.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { String path = null; JFileChooser chooser = new JFileChooser(Editor.lastPath); FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "jpeg", "bmp"); chooser.setFileFilter(filter); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int option = chooser.showOpenDialog(null); if (option == JFileChooser.APPROVE_OPTION) { path = chooser.getSelectedFile().getAbsolutePath(); Editor.lastPath = chooser.getSelectedFile().getParent(); copyImage(new File(path), "64.png"); try { x64.repaint(); x64.getGraphics().drawImage(ImageIO.read(new File(path)), 32 + 10, 32 + 20, 64, 64, null); } catch (IOException e) { e.printStackTrace(); } } } }); remove64.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { File file = new File("projects/" + Editor.getProjectName() + "/64.png"); file.delete(); x64.repaint(); } }); browse32.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { String path = null; JFileChooser chooser = new JFileChooser(Editor.lastPath); FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "jpeg", "bmp"); chooser.setFileFilter(filter); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int option = chooser.showOpenDialog(null); if (option == JFileChooser.APPROVE_OPTION) { path = chooser.getSelectedFile().getAbsolutePath(); Editor.lastPath = chooser.getSelectedFile().getParent(); copyImage(new File(path), "32.png"); try { x32.repaint(); x32.getGraphics().drawImage(ImageIO.read(new File(path)), 48 + 10, 48 + 20, 32, 32, null); } catch (IOException e) { e.printStackTrace(); } } } }); remove32.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { File file = new File("projects/" + Editor.getProjectName() + "/32.png"); file.delete(); x32.repaint(); } }); browse16.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { String path = null; JFileChooser chooser = new JFileChooser(Editor.lastPath); FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "jpeg", "bmp"); chooser.setFileFilter(filter); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int option = chooser.showOpenDialog(null); if (option == JFileChooser.APPROVE_OPTION) { path = chooser.getSelectedFile().getAbsolutePath(); Editor.lastPath = chooser.getSelectedFile().getParent(); copyImage(new File(path), "16.png"); try { x16.repaint(); x16.getGraphics().drawImage(ImageIO.read(new File(path)), 56 + 10, 56 + 20, 16, 16, null); } catch (IOException e) { e.printStackTrace(); } } } }); remove16.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { File file = new File("projects/" + Editor.getProjectName() + "/16.png"); file.delete(); x16.repaint(); } }); content.add(x128); content.add(x64); content.add(x32); content.add(x16); this.setContentPane(content); this.pack(); this.setLocationRelativeTo(parent); this.setVisible(true); } private class IconPanel extends JPanel { /** * */ private static final long serialVersionUID = 1608357876105365305L; IconPanel(int size) { this.setLayout(new BorderLayout()); this.setPreferredSize(new Dimension(148, 197)); this.setMaximumSize(new Dimension(148, 197)); JPanel buttons = new JPanel(); buttons.setLayout(new BorderLayout()); switch (size) { case 128: this.setBorder(BorderFactory.createTitledBorder("Grande(128x128)")); buttons.add(browse128, BorderLayout.WEST); buttons.add(remove128, BorderLayout.EAST); break; case 64: this.setBorder(BorderFactory.createTitledBorder("Moyenne(64x64)")); buttons.add(browse64, BorderLayout.WEST); buttons.add(remove64, BorderLayout.EAST); break; case 32: this.setBorder(BorderFactory.createTitledBorder("Petite(32x32)")); buttons.add(browse32, BorderLayout.WEST); buttons.add(remove32, BorderLayout.EAST); break; case 16: this.setBorder(BorderFactory.createTitledBorder("Trs petite(16x16)")); buttons.add(browse16, BorderLayout.WEST); buttons.add(remove16, BorderLayout.EAST); break; } this.add(buttons, BorderLayout.SOUTH); } } }