java - 无法在图像数组中存储和显示多个图像

标签 java arrays swing marvin-framework marvinproject

我在图像数组中存储多个图像并随后显示它时遇到了问题。我遇到的问题有点令人恼火。显示图像时,即使提到索引后,最后一个图像也会显示。我已经检查了大小没问题。这是我尝试过的示例代码

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();

}
}

这是世界级的:

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);
}
}

这是段类:

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);
}  
}

很抱歉,如果有任何小错误,例如缺少大括号,因为我必须从主项目中剪切粘贴

最佳答案

这是你的问题:

crop(original,memory,x1,y1,width,height);
ImgSeg[i-1]=memory;

方法crop将裁剪后的图像保存到内存变量中。然后将其分配给 ImgSeg 数组的第 (i-1)th 元素。在下一次迭代中,您将更改内存的内容,但 ImgSeg 数组的前一个元素仍然指向该对象,这意味着现在它将具有新的您刚刚保存到内存中的内容。因此,最终(最后一次迭代),内存将具有最后一个图像的内容,并且ImgSeg的所有元素都指向相同的内存 对象。

要解决这个问题,请在循环内创建一个新的 MarvingImage,将裁剪后的图像保存在该对象中,然后将其分配给数组的第 (i-1)th 元素。

for(int i=1; i<segments.length; i++){
    MarvinImage memory = new MarviImage();
    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);
}

删除此行static MarvinImage memory = new MarvinImage();

关于java - 无法在图像数组中存储和显示多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42932473/

相关文章:

c# - 将数组添加到对象列表

c - 如何将枚举变量分配给数组索引的指针

Java:弹出窗口并不总是可见

java - 如何将 jPanel 中的所有 jPanel 向左对齐?

java - Spring - SOAP Web 服务集成

Java无限相对鼠标拖动事件

java - 有没有一种优雅的方法可以使用 Optional 初始化和返回可空字段的值

php - 在 Twig 中打印数组

java - 使用 swing 的桌面应用程序中的数据库

java - 上次运行的 tomcat 类仍然存在