I have come across a problem while storing multiple images in an image array and displaying it thereafter.The problem i encounter is somewhat irritating.while displaying the images the last image gets displayed even after mentioning the index.I have check the size and it was ok.Heres the sample code i have tried
import marvin.image.*;
import marvin.io.*;
import marvin.gui.*;
import java.awt.FlowLayout;
import java.awt.Image;
import javax.swing.JFrame;
public class apples {
public static int WORLD_WIDTH = 500;
public static int WORLD_HEIGHT = 300;
public static void main(String[] args){
JFrame worldFrame = new JFrame("world");
worldFrame.getContentPane();
worldFrame.add(new world(WORLD_WIDTH, WORLD_HEIGHT));
worldFrame.setVisible(true);
worldFrame.setSize(WORLD_WIDTH , WORLD_HEIGHT+30);
worldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
segmentObjects segment = new segmentObjects();
}
}
heres the world class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class world extends JPanel{
static int SPACEX;
static int SPACEY ;
public static Image world;
static int movex = 100;
static int movey = 240;
public world(int WORLD_WIDTH, int WORLD_HEIGHT) {
SPACEX = WORLD_WIDTH;
SPACEY = WORLD_HEIGHT;
}
public void paint(Graphics g) {
BufferedImage worldB = new BufferedImage(SPACEX,SPACEY,BufferedImage.TYPE_BYTE_BINARY);
Graphics worldG = worldB.getGraphics();
worldG.setColor(Color.WHITE);
worldG.fillRect(0, 0,SPACEX, SPACEY); // draw the space
worldG.setColor(Color.BLACK);
//worldG.fillRect(movex, movey,50, 50); //draw the agent
worldG.fillOval(300, 100, 50, 50);
//worldG.fillOval(395, 100, 50, 50);
for(int x =220;x<300;x++){
worldG.drawLine(200, 200, x, 250);}
world = worldB;
g.drawImage(world, 0, 0, this);
}
}
heres the segment class:
import static marvin.MarvinPluginCollection.floodfillSegmentation;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.JFrame;
import static marvin.MarvinPluginCollection.*;
import marvin.gui.MarvinImagePanel;
import marvin.image.MarvinBlob;
import marvin.image.MarvinBlobSegment;
import marvin.image.MarvinContour;
import marvin.image.MarvinImage;
import marvin.image.MarvinSegment;
import marvin.io.MarvinImageIO;
import marvin.math.Point;
public class segmentObjects extends JFrame{
static MarvinImage original = new MarvinImage((BufferedImage) world.world);
static public int x1;
static public int y1;
static public int width;
static public int height;
static MarvinImage memory = new MarvinImage();
static ArrayList<Point> countorPoints = new ArrayList<Point>();
static int counter;
static MarvinImage[] ImgSeg=new MarvinImage[10];
public segmentObjects(){
super("segments");
MarvinImage image = original.clone();
MarvinSegment[] segments = floodfillSegmentation(image);
for(int i=1; i<segments.length; i++){
MarvinSegment seg = segments[i];
x1 =seg.x1;
y1 = seg.y1;
width = seg.width;
height =seg.height;
crop(original,memory,x1,y1,width,height);
ImgSeg[i-1]=memory;
//System.out.println(width+" "+height);
//System.out.println(ImgSeg.size());
counter =segments.length-1;
//contour(i);
}
System.out.println(ImgSeg[1].getWidth());
MarvinImagePanel imagePanel = new MarvinImagePanel();
imagePanel.setImage(ImgSeg[0]);
add(imagePanel);
setSize(400,630);
setVisible(true);
}
}
Sorry if there is any minor errors like missing curl braces since i had to cut paste from my main project
Here's your problem:
The method
cropis saving the cropped image into thememoryvariable. Then you are assigning it to the(i-1)thelement of theImgSegarray. In the next iteration, you are changing the contents ofmemory, but still the previous element of theImgSegarray is pointing to this object, which means now it will have the new contents you just saved intomemory. Thus, in the end (the last iteration),memorywill have the contents of the last image, and all elements ofImgSegare pointing to the samememoryobject.To solve it, create a new
MarvingImageinside the loop, save the cropped image in this object then assign it to the array's(i-1)thelement.Remove this line
static MarvinImage memory = new MarvinImage();