1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
| import java.awt.Color; 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.util.HashMap; import java.util.Map;
import javax.imageio.ImageIO; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.filechooser.FileFilter;
public class Main implements ActionListener { private Map<Color,BufferedImage> m_mapColourToImage; private BufferedImage m_image; private JFrame m_frame; private DrawingSurface surface; private static final int width = 800; private static final int height = 600; public static void main(String[] args) { new Main(); } private Main() { m_mapColourToImage = new HashMap<Color, BufferedImage>(); m_frame = new JFrame("TextureFiller"); m_frame.setSize(width,height); surface = new DrawingSurface(); surface.setVisible(true); surface.setOpaque(true); surface.setSize(width, height); m_frame.add(surface); JMenuBar menuBar; menuBar = new JMenuBar(); AddMenu(menuBar, "Menu","Menu","Load image","Add colour->texture", "Save image"); m_frame.setJMenuBar(menuBar); m_frame.setVisible(true); m_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); while(true) { m_frame.repaint(); } } private void AddMenu(JMenuBar menuBar, String name, String tooltip, String... menuItems) { JMenu menu = new JMenu(name); menu.setToolTipText(tooltip); for (String item: menuItems) { JMenuItem menuItem = new JMenuItem(item); menuItem.addActionListener(this); menu.add(menuItem); } menuBar.add(menu); } private class DrawingSurface extends JComponent { private static final long serialVersionUID = 1L; public void paintComponent(Graphics g) { super.paintComponent(g); if (m_image != null) g.drawImage(m_image, 0, 0, m_frame.getWidth(), m_frame.getHeight()-50, null); } } private BufferedImage LoadImage() { BufferedImage bi = null; JFileChooser fc = new JFileChooser(".");
fc.setFileFilter(new FileFilter() { @Override public String getDescription() { return "image file"; } @Override public boolean accept(File file) { return true; } }); fc.showOpenDialog(m_frame);
if (fc.getSelectedFile() != null) { File file = fc.getSelectedFile(); if (file == null) { return null; } try { Image img = ImageIO.read(file); bi = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_ARGB); bi.getGraphics().drawImage(img, 0,0,bi.getWidth(),bi.getHeight(),null); } catch(Exception ex) { JOptionPane.showMessageDialog(m_frame, "Load failed: " + ex.getMessage()); } } return bi; } private void TextureImage() { if (m_image == null) return; long time = System.currentTimeMillis();
for (int x = 0; x < m_image.getWidth(); x++) { for (int y = 0; y < m_image.getHeight(); y++) { int rgb = m_image.getRGB(x, y); if (rgb != 0) { Color pixelColour = new Color(rgb); BufferedImage image = m_mapColourToImage.get(pixelColour); if (image != null) { m_image.setRGB(x, y, image.getRGB(x%image.getWidth(), y%image.getHeight())); } } } } System.out.println("Texturing took " + (float)(System.currentTimeMillis()-time)/1000 + "s"); }
@Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "Load image") { m_image = LoadImage(); } else if (e.getActionCommand() == "Add colour->texture") { AddColour(); } else if (e.getActionCommand() == "Save image") { SaveImage(); } }
private void SaveImage() { if (m_image == null) { JOptionPane.showMessageDialog(m_frame, "Please load an image first!"); return; } JFileChooser fc = new JFileChooser(".");
fc.setFileFilter(new FileFilter() { @Override public String getDescription() { return "image file"; } @Override public boolean accept(File file) { return true; } }); fc.showSaveDialog(m_frame); if (fc.getSelectedFile() != null) { File file = fc.getSelectedFile(); if (file == null) { return; } String filename = file.getName(); if (filename == null) return; if (!filename.toLowerCase().endsWith(".png")) filename += ".png"; try { ImageIO.write(m_image, "PNG", new File(filename)); } catch(Exception ex) { JOptionPane.showMessageDialog(m_frame, "Load failed: " + ex.getMessage()); }
} }
private void AddColour() { if (m_image == null) { JOptionPane.showMessageDialog(m_frame, "Please load an image first!"); return; } String col = JOptionPane.showInputDialog("Enter colour (R,G,B) example: 255,255,255 for white"); int[] colour = new int[3]; for (int i = 0; i < 3; i++) { int index = 0; if (i < 2) index = col.indexOf(","); else index = col.length(); if (index == -1) { JOptionPane.showMessageDialog(m_frame, "R,G,B format please"); return; } String s = col.substring(0,index); colour[i] = Integer.parseInt(s); if (i < 2) col = col.substring(index+1); } BufferedImage image = LoadImage(); if (image != null) { m_mapColourToImage.put(new Color(colour[0],colour[1],colour[2]), image); } TextureImage(); } } |