Solved

Java extracting image objects from another

Posted on 2014-12-15
1
127 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Desingning Refactoring existing code 2 26
How to determine if a string is a valid SHA value 7 35
eclipse console opening separately 2 29
Domain Service Not Responding 14 30
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

860 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