Solved

Java extracting image objects from another

Posted on 2014-12-15
1
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

628 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