Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java extracting image objects from another

Posted on 2014-12-15
1
Medium Priority
?
135 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

704 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