Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  TextureFiller  (Read 911 times)
0 Members and 1 Guest are viewing this topic.
Offline roland
« Posted 2011-11-06 22:16:26 »

Hi, at the moment I'm making a 2d game where I load maps from image files. If someone wanted to save space, this is a good utility for making cool maps with tiny images. What you do is make a simple image (it can be huge though eg 10000x10000) of only a few seperate colours. And then you map each colour to a small seamless texture, and then when you load the map in game, it goes through the huge image and converts the simple colours into the image you mapped each colour too. The huge 10000x10000 image will be really small because it only contains a few plain colours (so like 50kb instead of megabytes), and the seamless textures can be 128x128 or 256x256 or whatever, so they will be small too.

Even though I decided not to take this approach, I don't have photoshop, and it makes it easy to map textures onto plain images.

This code isn't very optimised, but it is fast enough to do the job (eg, takes 1 second to map textures to an average sized image)
If you want to you can optimise it and post a new version here Smiley

Instructions:
1. Click on the menu and click "load image". Choose an image of your choice that has plain colours
2. Go to the menu and click add colour->texture.
    type in the R,G,B value of the colour and press ok, then select the seamless texture you want to map to that colour.
3. the colour that you chose on your image will now be flood filled with the texture you chose.
4. you can save the image as a .png in the menu  Smiley

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();
   }
}

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (216 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!