How to copy a part of an Image into another Image?

Hi!

I am lost a bit here. I have a strip of sprites (a gif file).

I load it into an Image and i need to create an

Array of Images based on that strip. I just cannot

find a way to copy a pary of that strip into another

Image object.

Help!

Thanks,

Artem

[315 byte] By [Artem] at [2007-9-26 1:19:12]
# 1
Anybody can help?
Artem at 2007-6-29 0:51:18 > top of Java-index,Archived Forums,Java Programming...
# 2

You could use a java.awt.image.BufferedImage which offers a method getSubImage(int x, int y, int w, int h) that creates a new BufferedImage object. By using a little "typecasting" you can store those BufferedImage objects in an array of Images.

Don't hesitate to ask if you have any further questions!

Tobias

tbraun at 2007-6-29 0:51:18 > top of Java-index,Archived Forums,Java Programming...
# 3

Hi!

Now i use the code like this:

Image pic;

BufferedImage bi;

// load pic here using Mediatracker

bi=(BufferedImage)pic;

and i get this:

Hit uncaught exception java.lang.ClassCastException

It seems as usual typecasting does not work.

So, another problem came up. How to

interwork Image and BufferedImage. How to make

and Image from BufferedImage and how to make

BufferedImage from Image?

Please, help!

Regards,

Artem

Artem at 2007-6-29 0:51:18 > top of Java-index,Archived Forums,Java Programming...
# 4

I've written a small applet for clearification, you really can't typecast that simply:

import java.awt.*;

import java.awt.image.*;

import java.net.*;

/*

* ImagerApplet.java

*

* Created on July 23, 2001, 5:44 PM

*/ /**

*

* @author tb

* @version

*/

public class ImagerApplet extends java.applet.Applet {Image img[] = new Image[10];/** Initializes the applet ImagerApplet */

public void init () {

initComponents ();

MediaTracker tracker = new MediaTracker (this);

Image image = null;

try {

System.out.println(this.getCodeBase().toString());

image = this.getImage(new URL(this.getCodeBase() +

"de_home_title.gif"));

}

catch (MalformedURLException e) { System.out.println(e); }

tracker.addImage(image, 0);

try {

//Start downloading the images. Wait until they're loaded.

tracker.waitForAll();

} catch (InterruptedException e) {}

BufferedImage bufImg = new BufferedImage(image.getWidth(this),

image.getHeight(this),

BufferedImage.TYPE_3BYTE_BGR);

Graphics g = bufImg.getGraphics();

g.drawImage (image, 0, 0, this);

for(int i=0; i<10; i++)

img = (Image) bufImg.getSubimage(i*20, 0, 20, 20); g = this.getGraphics(); for(int i=0; i<10; i++) {

g = this.getGraphics();

g.drawImage(img, i*12, 20, this);

}

this.repaint();

}

public void paint (Graphics g) {

for (int i=0; i<10; i++) {

if(img != null) {

g.drawImage(img, i*12, 20, this);

}

}

}/** This method is called from within the init() method to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the FormEditor.

*/

private void initComponents() {//GEN-BEGIN:initComponents

panel1 = new java.awt.Panel();

panel2 = new java.awt.Panel();

setLayout(new java.awt.BorderLayout());

panel1.setFont(new java.awt.Font ("Dialog", 0, 11));

panel1.setName("panel7");

panel1.setBackground(new java.awt.Color (204, 204, 204));

panel1.setForeground(java.awt.Color.black);

add(panel1, java.awt.BorderLayout.NORTH);

panel2.setFont(new java.awt.Font ("Dialog", 0, 11));

panel2.setName("panel8");

panel2.setBackground(new java.awt.Color (204, 204, 204));

panel2.setForeground(java.awt.Color.black);

add(panel2, java.awt.BorderLayout.SOUTH);

}//GEN-END:initComponents// Variables declaration - do not modify//GEN-BEGIN:variables

private java.awt.Panel panel1;

private java.awt.Panel panel2;

// End of variables declaration//GEN-END:variables }

tbraun at 2007-6-29 0:51:18 > top of Java-index,Archived Forums,Java Programming...
# 5
Ugh, that looks really ugly... I can mail you the source if you prefer...Anyway, happy coding :-)Tobias
tbraun at 2007-6-29 0:51:18 > top of Java-index,Archived Forums,Java Programming...
# 6
No need. The code is self explanatory. Thanks a lot!
Artem at 2007-6-29 0:51:18 > top of Java-index,Archived Forums,Java Programming...