【问题标题】:Boundary fill (flood fill) algorithm to construct an interactive map. Java边界填充(flood fill)算法构建交互式地图。爪哇
【发布时间】:2013-04-07 22:42:27
【问题描述】:

我正在构建一个美国交互式地图,它将响应用户对 JTable 的输入值。我已经这样做了,但没有洪水填充算法(每个州都有自己的 .png 图像)。现在我决定使用边界填充或种子填充......但它不工作......这是完整的代码:

import java.awt.Color;
import java.awt.Container;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;

public class MapTest extends JFrame {


private static JTable table;
private JTable tableS;
private String[] states = {"US STATES", "Alabama",  "Alaska",   "Arizona"   };
private JLabel map;

private String[][] statesPixel = { {    "alabama",  "300",  "300"   },
        {   "alaska",   "350",  "350"   },
        {   "arizona",  "400",  "400"   },
        {   "arkansas", "450",  "450"   }   };


public MapTest() throws InterruptedException
{
    createMap();
}
private void createMap() throws InterruptedException {
Container contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);

contentPane.setSize(1220,700);

tableS = new JTable(4,1);
tableS.setBounds(1000,16,120,800);
tableS.setRowHeight(12);

int i = 0;
while (i < states.length) {
    tableS.setValueAt(states[i], i, 0);
    i++;
}

contentPane.add(tableS);


table = new JTable(4,1);
table.setBounds(1120,16,50,800);
table.setRowHeight(12);
int j = 0;
while (j < states.length) {
    table.setValueAt("100", j, 0);
    j++;
}
table.setValueAt("VALUE",0,0);
contentPane.add(table);

ExcelAdapter excelTable = new ExcelAdapter(table);

map = new JLabel();
map.setIcon(new ImageIcon("map.png"));
map.setBounds(150,50,800,600);
contentPane.add(map);

setTitle("Map");
setSize(1220,700);
setVisible(true);
setLocationRelativeTo(null);

  //updates~~
   while (true) {
    for ( int k = 0; k < statesPixel.length; k++) {
        int fill = Integer.parseInt( (String) table.getValueAt(k+1, 0));
    boundaryFill4(Integer.parseInt(statesPixel[k][1]),Integer.parseInt(statesPixel[k][2]),statesPixel[k][0],fill+1,0);
    }

}


//*******************************************************************
}

    private void boundaryFill4 (int x, int y, String state, int fill, int boundary) {

       int current;

       current = getPixel (x, y);
       if ((current != boundary) && (current != fill)) {
       setPixel (x, y, fill);
       boundaryFill4 (x+1, y, state, fill, boundary);
       boundaryFill4 (x-1, y, state,fill, boundary);
       boundaryFill4 (x, y+1, state,fill, boundary);
       boundaryFill4 (x, y-1, state,fill, boundary) ;
     }
    }

    private int getPixel(int x, int y) {
    Image img = ((ImageIcon) map.getIcon()).getImage();
    BufferedImage buffered = new BufferedImage(img.getWidth(null),img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    buffered.getGraphics().drawImage(img, 0, 0, null);
    int current = buffered.getRGB(x, y);    
    return current;
    }

    private void setPixel(int x, int y, int fill) {
    Image img = ((ImageIcon) map.getIcon()).getImage();
    BufferedImage buffered = new BufferedImage(img.getWidth(null),img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    buffered.getGraphics().drawImage(img, 0, 0, null);
    int red = fill;
    int green = red;
    int blue = red;
    Color c = new Color(buffered.getRGB(x, y));
    c = new Color(red, green, blue);
    buffered.setRGB(x, y, c.getRGB());
}

    public static void main(String args[]) throws InterruptedException {
MapTest map = new MapTest();
map.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

 } 

我还使用 ExcelAdapter.java,它可以在线获得,只是为了启用复制/粘贴到 JTable。我的代码怎么不行……我调试了……好久了

【问题讨论】:

  • 您正在使用递归洪水填充算法,这对于大图像肯定会失败。扫描线算法最适合这里。查看 this 页面了解更多信息。该算法的实现可以在 here 找到
  • @Extreme Coders 我熟悉扫描线算法,但是,我的问题是:程序甚至没有显示任何更改,如果它忽略了我的代码...图像会加载,但没有完全发生颜色变化...但我没有收到任何错误...
  • @Extreme Coders ExcelAdapter 没有问题。我把它作为一个单独的课程。它完美地工作。我已经用 sysout 等进行了 100% 的检查......我用 ExcelAdapter 以不同的方式编写了这段代码......
  • 用断点来调试你的代码,特别是在方法boundaryFill4
  • @Extreme Coders 这里是 Excel 适配器的链接:dl.dropbox.com/u/41007907/ExcelAdapter.java

标签: java swing map colors flood-fill


【解决方案1】:

您的 setPixel 方法对新的 BufferedImage 进行操作,而不是您的实际图像,因此任何更改都会被丢弃。

boundaryFill4 方法还通过查看当前像素是否为黑色来检查边界,这意味着它永远不会更新地图中的任何黑色像素。此外,由于 setPixel 的更改被丢弃,它永远不会完成(可能)。

最后,由于您的 while (true) { 没有任何终止条件,它只会不断循环遍历所有图像像素。

此外,您的代码还有一些其他改进需要进行,例如每次要获取像素的颜色值时不创建新的 bufferedImage,并且确实切换到不需要堆栈大小的算法最坏情况下的宽度+高度。

【讨论】:

  • 有人告诉过我。我试图纠正它。但仍然有错误。你能推荐一个解决方案吗
  • 您需要修改正在显示的图像。因此,与其显示 png 文件,不如将文件加载到图像对象中,显示该图像,然后传递该图像以进行修改(如果需要,调用任何重绘或更新方法,我不知道 swing)。
  • 另一种选择是每次都使用新图像调用 map.setIcon,即map.SetIcon(new ImageIcon(bufferedImage))
【解决方案2】:

您的代码有很多问题。实际上,据我了解,您想制作一个洪水填充的测试用例,以便编写其他应用程序。如果你熟悉python,你可能会觉得下面的内容很有趣Making a weighted USA map based on state-level data有一个现成的代码,复制粘贴就可以修改了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-12
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 2013-07-25
    • 1970-01-01
    相关资源
    最近更新 更多