Solved

Java extracting image objects from another

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to get statistic's from a competitors website?! 1 50
FTP File permissions 1 31
SQL Injected data? 8 86
Java Timer (static) 9 16
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now