Solved

Java extracting image objects from another

Posted on 2014-12-15
1
129 Views
Last Modified: 2014-12-16
Hi

I found code to extract an image object from another.
here
I'd like to dissect this RTS tileset in code into new image objects. (the precice individual tiles) from the attached
The tiles are on the right of the attached image. I'll modify it to be my own scenery.
What might it look like to display the individual dissected tiles?
Thanks
RTStileset.jpg
0
Comment
Question by:beavoid
1 Comment
 

Accepted Solution

by:
beavoid earned 0 total points
ID: 40504044
I solved this on my own, after all.
Code below

import javax.imageio.ImageIO;  
import javax.swing.*;

import java.awt.image.BufferedImage;  
import java.io.*;  
import java.awt.*;  
  
public class ImageSplitTest extends JFrame{  
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public BufferedImage imgs[];
	
	public ImageSplitTest() {

		this.setSize(500, 500);
		extract();
		this.setVisible(true);
		
		Graphics G = this.getGraphics();
		G.setColor(Color.WHITE);
		G.fillRect(0, 0, 500, 500);
		G.drawImage(imgs[4], 200, 200, 200,200,this);
	}
	
	public void extract()  {  
		try {

	        System.out.println("Extract start");
        File file = new File("RTStileset.jpg"); // I have bear.jpg in my working directory  
        FileInputStream fis = new FileInputStream(file);  
        BufferedImage image = ImageIO.read(fis); //reading the image file  
  
        int rows = 4; //You should decide the values for rows and cols variables  
        int cols = 4;  
        int chunks = rows * cols;  
  
        int chunkWidth = image.getWidth() / cols; // determines the chunk width and height  
        int chunkHeight = image.getHeight() / rows;  
        int count = 0;  
        				imgs = new BufferedImage[chunks]; //Image array to hold image chunks  
        for (int x = 0; x < rows; x++) {  
            for (int y = 0; y < cols; y++) {  
                //Initialize the image array with image chunks  
                imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());  
  
                // draws the image chunk  
                Graphics2D gr = imgs[count++].createGraphics();  
                gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null);  
                gr.dispose();  
            }  
        }  
        System.out.println("Extract end");
		} catch (Exception e) {
	        System.out.println("ImageSplitTest ex : "+e);
			e.fillInStackTrace();
			
		}
        
       
  
        
    } 
	
	public void paint(Graphics G) {

		System.out.println("paint");
		//Graphics G = this.getGraphics();
		G.setColor(Color.white);
		G.fillRect(0, 0, 500, 500);
	}

	public static void main(String[] args) throws IOException {
		
		new ImageSplitTest();
		
		
		
		
		
	}
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question